springmvc配置的代码
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
<context:component-scan base-package="com.kiddie.controler" />
<context:component-scan base-package="com.kiddie.service" />
<!-- 把标记了@Controller注解的类转换为bean -->
<context:annotation-config />
<!--避免IE执行AJAX时,返回JSON出现下载文件 -->
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" /> <!-- JSON转换器 -->
</list>
</property>
</bean>
<!-- 定义跳转的文件的前后缀 ,视图模式配置 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
<property name="prefix" value="/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 默认编码 -->
<property name="defaultEncoding" value="utf-8" />
<!-- 文件大小最大值 -->
<property name="maxUploadSize" value="10485760000" />
<!-- 内存中的最大值 -->
<property name="maxInMemorySize" value="40960" />
</bean>
</beans>
上传页面的代码
<body>
<form action="./user/user.do" method="post" enctype="multipart/form-data">
<input type="hidden" name="id" value="123"/>
用户名:<input type="text" name="username"/><br/>
密码:<input type="password" name="password"/><br/>
年龄:<input type="text" name="age"/><br/>
文件:<input type="file" name="file"/><br/>
文件:<input type="file" name="file"/><br/>
文件:<input type="file" name="file"/><br/>
<input type="submit" value="注册"/>
</form>
</body>
controller代码
@Controller
@RequestMapping("/user")
public class UserControler {
@Autowired
private UserService userService;
@RequestMapping(value = "/add")
public String add(User user) {
return "add";
}
@RequestMapping("/user")
public String user(User user, String id, @RequestParam MultipartFile file[]) {
System.out.println(id);
System.out.println(user);
for (MultipartFile f : file) {
if (!f.isEmpty()) {
try {
f.transferTo(new File("e:/kiddie/"
+ f.getOriginalFilename()));
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return "user";
}
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
}
注意:springmvc会默认把参数注入到你的请求函数的参数,如果是String注入,是否配置@RequestParam没有关系;但是要注入bean,如上述代码User,就不要配置@RequestParam,如果是表单有上传文件,MultipartFile也一定要配置好@RequestParam,否则会报错。也要注意表单enctype="multipart/form-data"