通过示例项目,学习SpringMVC的注解以及参数封装
创建web项目以及导入jar包
项目结构如图
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Springmvc02</display-name>
<filter>
<filter-name>characterEncoding</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>characterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 默认加载方式
默认加载必须规范:
* 文件命名:servlet-name-servlet.xml====springmvc-servlet.xml
* 路径规范:必须在WEB-INF目录下面
-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
springmvc.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"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<!-- 扫描注解 -->
<context:component-scan base-package="com.zyj"></context:component-scan>
<!-- 配置注解处理器映射器
负责寻找Controller
-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
<!-- 配置注解处理器处理器
负责执行Controller
-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
<!-- 配置视图解析器
后台返回逻辑试图:index
视图解析器解析出真正物理视图:前缀+逻辑试图+后缀====/WEB-INF/jsps/index.jsp
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsps/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
User.java
public class User {
private int id;
private String username;// 用户姓名
private String sex;// 性别
private Date birthday;// 生日
private String address;// 地址
//省略getter和setter方法
}
UserCustom.java
// 用户包装类
public class UserCustom {
// 用户
private User user;
// List集合
private List<User> userList;
// Map集合
private Map<String, Object> map = new HashMap<String, Object>();
//省略getter和setter方法
}
UserController.java
@Controller
@RequestMapping("/user")//配置根路径,目的是解决多个Controller中出现相同的RequestMapping的冲突
public class UserController {
// 跳转页面
@RequestMapping("toAddUser")
public String toAddUser() {
return "addUser";
}
// 接收int类型和string类型参数
@RequestMapping("receiveIntAndString")
public String receiveIntAndString(int id, String username) {
System.out.println(id+","+username);
return "success";
}
// 接收po类型参数
@RequestMapping("receiveUser")
public String receiveUser(User user) {
System.out.println(user);
return "success";
}
// 接收包装类型参数
@RequestMapping("receiveUserCustom")
public String receiveUserCustom(UserCustom userCustom) {
System.out.println(userCustom.getUser());
return "success";
}
// 接收数组类型参数
@RequestMapping("receiveArray")
public String receiveArray(Integer[] ids) {
for (Integer integer : ids) {
System.out.println(integer);
}
return "success";
}
// 接收list集合类型参数
@RequestMapping("receiveUserCustomList")
public String receiveUserCustomList(UserCustom userCustom) {
System.out.println(userCustom.getUserList());
return "success";
}
// 接收map集合类型参数
@RequestMapping("receiveUserCustomMap")
public String receiveUserCustomMap(UserCustom userCustom) {
System.out.println(userCustom.getMap());
return "success";
}
}
addUser.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/user/receiveIntAndString.do" method="POST">
ID:<input type="text" name="id" />
用户名:<input type="text" name="username" />
<input type="submit" value="提交多个普通类型参数"/>
</form>
<hr color="grey" size="10" />
<form action="${pageContext.request.contextPath}/user/receiveUser.do" method="POST">
ID:<input type="text" name="id" />
用户名:<input type="text" name="username" />
性别:<input type="text" name="sex" />
生日:<input type="text" name="birthday" />
地址:<input type="text" name="address" />
<input type="submit" value="提交po类型参数"/>
</form>
<hr color="grey" size="10" />
<form action="${pageContext.request.contextPath}/user/receiveUserCustom.do" method="POST">
ID:<input type="text" name="user.id" />
用户名:<input type="text" name="user.username" />
性别:<input type="text" name="user.sex" />
生日:<input type="text" name="user.birthday" />
地址:<input type="text" name="user.address" />
<input type="submit" value="提交包装类型参数"/>
</form>
<hr color="grey" size="10" />
<form action="${pageContext.request.contextPath}/user/receiveArray.do" method="POST">
ID:<input type="checkbox" name="ids" value="1"/>
ID:<input type="checkbox" name="ids" value="2"/>
ID:<input type="checkbox" name="ids" value="3"/>
ID:<input type="checkbox" name="ids" value="4"/>
ID:<input type="checkbox" name="ids" value="5"/>
<input type="submit" value="提交数组类型参数"/>
</form>
<hr color="grey" size="10" />
<form action="${pageContext.request.contextPath}/user/receiveUserCustomList.do" method="POST">
ID:<input type="text" name="userList[0].id" />
用户名:<input type="text" name="userList[0].username" />
ID:<input type="text" name="userList[1].id" />
用户名:<input type="text" name="userList[1].username" />
<input type="submit" value="提交List类型参数"/>
</form>
<hr color="grey" size="10" />
<form action="${pageContext.request.contextPath}/user/receiveUserCustomMap.do" method="POST">
ID:<input type="text" name="map['id']" />
用户名:<input type="text" name="map['username']" />
<input type="submit" value="提交Map类型参数"/>
</form>
</body>
</html>
页面
RequestMapping
RequestMapping(“hello”)
(推荐,不需要设置后缀名)RequestMapping(“/hello.do”)
RequestMapping(value=”/hello.do”)
RequestMapping(value=”/hello.do”,method=RequestMethod.GET)
RequestMapping(value=”/hello.do”,method=RequestMethod.POST)
浏览器直接访问,a标签都是get请求
表单提交(指定post),ajax指定post提交,post提交。RequestMapping(value=”/hello.do”,method={RequestMethod.POST,
RequestMethod.GET})
RequestMaping根路径
解决多个Controller中出现相同的RequestMapping时的冲突
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping("save")
public String save() {
return "success";
}
}
@Controller
@RequestMapping("/orders")
public class OrdersController {
@RequestMapping("save")
public String save() {
return "success";
}
}
访问:
http://localhost:8080/项目名/user/save.do
http://localhost:8080/项目名/orders/save.do
上一篇:《SpringMVC(一)之入门》
下一篇:《SpringMVC(三)之数据回显、url模板映射、转发和重定向》