目录
第二节 SpringMVC的注解开发【重要】2.1 注解开发入门案例第一步:修改springmvc的配置文件第二步:修改UserController第三步:测试
2.2 RequestMapping的使用用于配置请求路径的映射自定义根路径
2.3 接收请求参数封装参数分析1. 接收int、String、Date、数组类型2. 接收pojo类型3. 接收包装类型参数4. 接收集合List类型参数5. 接收集合Map类型参数
第三节 页面回显1. 配置一个列表方法2. 显示列表数据3. 添加一个edit方法,来修改数据4. 修改页面
第四节 URL模版映射1. 修改url格式2. 配置接收url模版映射3. 测试4. 在web.xml中配置rest路径
第五节 转发和重定向1. 转发到同一个控制器的方法2. 转发到不同控制器的方法3. 重定向,只需要把forward改成redirect即可
第六节 RequestParam参数描述
第二节 SpringMVC的注解开发【重要】
2.1 注解开发入门案例
第一步:修改springmvc的配置文件
复制day01的项目,修改DispatchServlet-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"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<context:component-scan base-package="com.it.web.controller"/>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
第二步:修改UserController
package com
.it
.web
.controller
;
import org
.springframework
.stereotype
.Controller
;
import org
.springframework
.web
.bind
.annotation
.RequestMapping
;
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping("/list")
public String
list(){
return "/user/userList";
}
}
第三步:测试
2.2 RequestMapping的使用
用于配置请求路径的映射
方式有多种
package com
.it
.web
.controller
;
import org
.springframework
.stereotype
.Controller
;
import org
.springframework
.web
.bind
.annotation
.RequestMapping
;
import org
.springframework
.web
.bind
.annotation
.RequestMethod
;
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping("list")
public String
list(){
return "/user/userList";
}
@RequestMapping("/list1")
public String
list1(){
return "/user/userList";
}
@RequestMapping("/list2.do")
public String
list2(){
return "/user/userList";
}
@RequestMapping(value
= "/list3.do")
public String
list3(){
return "/user/userList";
}
@RequestMapping(value
= "/list4",method
= RequestMethod
.POST
)
public String
list4(){
return "/user/userList";
}
@RequestMapping(value
= "/list5",method
= RequestMethod
.GET
)
public String
list5(){
return "/user/userList";
}
}
访问指定post方式,会报不支持get方式
自定义根路径
就是在类上添加一个RequestMapping 访问的时候加上即可
2.3 接收请求参数
封装参数分析
可以接收的参数类型有:基本类型、Pojo类、包装类、List集合、Map集合
Struts2参数封装:基于属性封装,在action会添加属性,提供set方法。SpringMVC参数封装:基于方法进行封装。
1. 接收int、String、Date、数组类型
在views/user中提供一个form表单:register.jsp
<form action="${pageContext.request.contextPath}/user/register.do" method="post">
用户名:
<input type="text" name="username"><br>
密码:
<input type="text" name="password"><br>
性别:
<input type="text" name="gender"><br>
年龄:
<input type="text" name="age"><br>
生日:
<input type="text" name="birthday"><br>
爱好:
<input type="checkbox" name="hobbyIds" value="1">写代码
<input type="checkbox" name="hobbyIds" value="2">赚钱
<input type="checkbox" name="hobbyIds" value="3">买房
<br>
<input type="submit">
</form>
在UserController中提供一个用户注册的方法(springmvc会把请求参数注入到方法中)
@RequestMapping("/register")
public String
register(String username
, String password
, int age
, String gender
,
Date birthday
,String
[] hobbyIds
){
System
.out
.println(username
);
System
.out
.println(password
);
System
.out
.println(age
);
System
.out
.println(gender
);
System
.out
.println(birthday
);
for (String hobbyId
: hobbyIds
) {
System
.out
.println(hobbyId
);
}
return "/user/info";
}
执行效果
页面上取不到信息是因为没有使用user对象 后台可以接收到数据
2. 接收pojo类型
创建一个pojo User类型,并提供get/set、toString 配置一个注册方法
@RequestMapping("/register2")
public String
register2(User user
){
System
.out
.println(user
);
return "/user/info";
}
修改表单提交的去向 测试
由于是user对象,所以info.jsp中能取出信息 后台打印的user对象
3. 接收包装类型参数
写一个User的扩展类UserExt,并把User写成该类的一个属性,提供get/set、toString(这种方式类似模型中有模型) 添加一个注册方法 修改form表单 测试
4. 接收集合List类型参数
在User的扩展类UserExt中添加一个List集合,并提供get/set 添加一个注册方法 修改表单
<hr>
同时注册多个用户(使用List来接收)
<form action="${pageContext.request.contextPath}/user/register4.do" method="post">
用户名:
<input type="text" name="userList[0].username"><br>
密码:
<input type="text" name="userList[0].password"><br>
=============================================
<br>
用户名:
<input type="text" name="userList[1].username"><br>
密码:
<input type="text" name="userList[1].password"><br>
<input type="submit">
</form>
测试
5. 接收集合Map类型参数
在User的扩展类UserExt中添加一个Map集合,并提供get/set 添加注册方法 修改表单
<hr>
使用Map来接收
<form action="${pageContext.request.contextPath}/user/register5.do" method="post">
用户名:
<input type="text" name="infos['username']"><br>
密码:
<input type="text" name="infos['password']"><br>
<input type="submit">
</form>
测试
第三节 页面回显
1. 配置一个列表方法
在这之前,先给User模型提供一个id属性,并提供get/set,无参构造、有参构造 配置一个列表方法(这里模拟从数据库查数据)
@RequestMapping("/list")
public String
list(Model model
){
List
<User> userList
= new ArrayList<User>();
User user1
= new User(1,"shu01","123","男",20,new Date());
User user2
= new User(2,"shu02","456","男",21,new Date());
User user3
= new User(3,"shu03","789","男",22,new Date());
userList
.add(user1
);
userList
.add(user2
);
userList
.add(user3
);
model
.addAttribute("userList",userList
);
return "/user/userList";
}
2. 显示列表数据
在这之前考虑到要使用到jstl表达式,先导入一下jar包
在UserList.jsp页面中显示数据
<%--
Created by IntelliJ IDEA.
User: shuyy
Date: 2020/10/5
Time: 20:14
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>用户列表
</title>
</head>
<body>
用户列表界面
<br>
<table border="1">
<tr>
<td>id
</td>
<td>用户名
</td>
<td>密码
</td>
<td>年龄
</td>
<td>生日
</td>
</tr>
<c:forEach items="${userList}" var="user">
<tr>
<td>${user.id}
</td>
<td>${user.username}
</td>
<td>${user.password}
</td>
<td>${user.age}
</td>
<td>${user.birthday}
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
运行效果
3. 添加一个edit方法,来修改数据
@RequestMapping("/edit")
public String
edit(int id
,Model model
){
User user1
= new User(1,"shu01","123","男",20,new Date());
model
.addAttribute("user",user1
);
return "/user/userEdit";
}
4. 修改页面
添加修改
<td><a href="${pageContext.request.contextPath}/user/edit.do?id=${user.id}">修改
</a></td>
添加一个修改信息页面
<form action="${pageContext.request.contextPath}/user/update.do" method="post">
id:${user.id}
<br>
用户名:
<input type="text" name="username" value="${user.username}"><br>
密码:
<input type="text" name="password" value="${user.password}"><br>
性别:
<input type="text" name="gender" value="${user.gender}"><br>
生日:
<input type="text" name="birthday" value="${user.birthday}"><br>
<input type="submit">
</form>
效果
第四节 URL模版映射
url模版映射可以使用restful软件架构方式
1. 修改url格式
2. 配置接收url模版映射
{}:匹配接受页面Url路径参数@Pathariable:{}里面参数注入后面参数里面
3. 测试
4. 在web.xml中配置rest路径
可以在web.xml中配置它的访问路径 访问的时候前面加上rest即可
第五节 转发和重定向
1. 转发到同一个控制器的方法
2. 转发到不同控制器的方法
3. 重定向,只需要把forward改成redirect即可
第六节 RequestParam参数描述
RequestParam参数描述
value:参数名称defaultValue:默认值required:参数是否必须有值(如果为true,参数又为空,会报错)(可以防止一些空指针异常) 拼接一个uid的值则不会报错 也可以设置一个默认值