首页 > 代码库 > springmvc文件上传及表单数据封装
springmvc文件上传及表单数据封装
补充:
form表单需要提交时间,springmvc封装到实体类的Date字段时,丢失时分秒,可以在controller中添加
@InitBinder public void initBinder(WebDataBinder binder) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); dateFormat.setLenient(false); binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); } @RequestMapping(value=http://www.mamicode.com/"/activity",method = RequestMethod.POST)>友情链接:个人博客
转载博文:springmvc文件上传
这是用的是SpringMVC-3.1.1、commons-fileupload-1.2.2和io-2.0.1
首先是web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>upload</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>upload</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <filter> <filter-name>SpringCharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>SpringCharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>/WEB-INF/jsp/user/add.jsp</welcome-file> </welcome-file-list> </web-app>
接下来是SpringMVC的配置文件upload-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="com.jadyer"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value=http://www.mamicode.com/"/WEB-INF/jsp/"/>>
下面是用于上传的表单页面//WEB-INF//jsp//user//add.jsp
<%@ page language="java" pageEncoding="UTF-8"%> <form action="<%=request.getContextPath()%>/user/add" method="POST" enctype="multipart/form-data"> username: <input type="text" name="username"/><br/> nickname: <input type="text" name="nickname"/><br/> password: <input type="password" name="password"/><br/> yourmail: <input type="text" name="email"/><br/> yourfile: <input type="file" name="myfiles"/><br/> yourfile: <input type="file" name="myfiles"/><br/> yourfile: <input type="file" name="myfiles"/><br/> <input type="submit" value=http://www.mamicode.com/"添加新用户"/>>
下面是上传成功后打印用户信息的页面//WEB-INF//jsp//user//list.jsp
<%@ page language="java" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <c:forEach items="${users}" var="user"> ${user.value.username}----${user.value.nickname}----${user.value.password}----${user.value.email} <a href=http://www.mamicode.com/"/user/${user.value.username}">查看>
下面是上传文件内容过大时的提示页面//WEB-INF//jsp//error_fileupload.jsp
<%@ page language="java" pageEncoding="UTF-8"%> <h1>文件过大,请重新选择</h1>
接下来是用到的实体类User.java
package com.jadyer.model; /** * User * @author 宏宇 * @create May 12, 2012 1:24:43 AM */ public class User { private String username; private String nickname; private String password; private String email; /*==四个属性的getter()、setter()略==*/ public User() {} public User(String username, String nickname, String password, String email) { this.username = username; this.nickname = nickname; this.password = password; this.email = email; } }
最后是核心的UserController.java
package com.jadyer.controller; import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.apache.commons.io.FileUtils; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; import com.jadyer.model.User; /** * SpringMVC中的文件上传 * @see 第一步:由于SpringMVC使用的是commons-fileupload实现,故将其组件引入项目中 * @see 这里用到的是commons-fileupload-1.2.2.jar和commons-io-2.0.1.jar * @see 第二步:在####-servlet.xml中配置MultipartResolver处理器。可在此加入对上传文件的属性限制 * @see 第三步:在Controller的方法中添加MultipartFile参数。该参数用于接收表单中file组件的内容 * @see 第四步:编写前台表单。注意enctype="multipart/form-data"以及<input type="file" name="****"/> * @author 宏宇 * @create May 12, 2012 1:26:21 AM */ @Controller @RequestMapping("/user") public class UserController { private final static Map<String,User> users = new HashMap<String,User>(); //模拟数据源,构造初始数据 public UserController(){ users.put("张起灵", new User("张起灵", "闷油瓶", "02200059", "menyouping@yeah.net")); users.put("李寻欢", new User("李寻欢", "李探花", "08866659", "lixunhuan@gulong.cn")); users.put("拓拔野", new User("拓拔野", "搜神记", "05577759", "tuobaye@manhuang.cc")); users.put("孙悟空", new User("孙悟空", "美猴王", "03311159", "sunhouzi@xiyouji.zh")); } @RequestMapping("/list") public String list(Model model){ model.addAttribute("users", users); return "user/list"; } @RequestMapping(value=http://www.mamicode.com/"/add", method=RequestMethod.GET)>
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。