先准备一个含有表单的页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录页面</title>
</head>
<body>
<h1>登录页面</h1>
<%--
/demo/login1.do
/demo/login2.do
/demo/login3.do
/demo/login4.do
/demo/login5.do
/demo/login6.do
/demo/login7.do
/demo/login8.do
--%>
<form action="${pageContext.request.contextPath}/demo/login9.do" method="post">
编 号*:<input type="text" name="num"><br>
用户名:<input type="text" name="username"><br>
密 码: <input type="password" name="password"><br>
<input type="submit" value="登录">
</form>
</body>
</html>
一、向页面传值的方式
1、使用HttpServletRequest对象绑定数据转发给jsp
使用HttpServletRequest对象绑定数据转发给jsp,通过JavaBean封装请求参数值,将数据绑定到HttpServletRequest。
@RequestMapping("/login4.do")
public String login4(UserParam userParam,HttpServletRequest request){
System.out.println("login4()");
int num = userParam.getNum();
String username = userParam.getUsername();
String password = userParam.getPassword();
//将数据绑定到request
request.setAttribute("num",num);
request.setAttribute("username",username);
request.setAttribute("password",password);
request.setAttribute("userParam",userParam);
//默认情况下,DispatcherServlet会默认使用转发。转发:forward:index,重定向:redirect:index
return "index";
}
2、使用ModelAndView对象
通过JavaBean封装请求参数值放到map中,然后将数据封装到ModelAndView对象作为方法的返回值
@RequestMapping("/login5.do")
public ModelAndView login5(UserParam userParam){
System.out.println("login5()");
int num = userParam.getNum();
String username = userParam.getUsername();
String password = userParam.getPassword();
//创建一个Map
Map<String,Object> data = new HashMap<String,Object>();
//底层会把key作为绑定名,相当于执行了req.setAttribute()方法
data.put("num",num);
data.put("username",username);
data.put("password",password);
//创建ModelAndView对象
ModelAndView mav = new ModelAndView("index", data);
mav.addObject(userParam);
return mav;
}
3、使用ModelMap对象
将该对象作为方法的参数,然后将数据绑定到该对象。
@RequestMapping("/login6.do")
public String login6(UserParam userParam, ModelMap modelMap){
System.out.println("login6()");
int num = userParam.getNum();
String username = userParam.getUsername();
String password = userParam.getPassword();
//相当于执行了request.setAttribute()
modelMap.addAttribute("num",num);
modelMap.addAttribute("username",username);
modelMap.addAttribute("password",password);
modelMap.addAttribute("userParam",userParam);
return "index";
}
4、使用HttpSession对象
将数据绑定到session对象
注:(若重定向302使用session绑定数据,使用请求对象的话在请求之后就被销毁了)
@RequestMapping("/login7.do")
public String login7(UserParam userParam, HttpSession session){
System.out.println("login7()");
int num = userParam.getNum();
String username = userParam.getUsername();
String password = userParam.getPassword();
session.setAttribute("num",num);
session.setAttribute("username",username);
session.setAttribute("password",password);
session.setAttribute("userParam",userParam);
return "index";
}
5、通过EL表达式获取绑定的值
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>SpringMVC请求与响应</title>
</head>
<body>
<h1>登录成功...</h1>
<h2>向页面传值方式</h2>
<ol>
<li>绑定数据到request</li>
<li>使用ModelAndView对象作为方法返回值</li>
<li>使用ModelMap对象</li>
<li>绑定数据到HttpSession</li>
</ol>
<%----%>
<hr>
<h2>1.通过HttpServletRequest</h2>
<h3>${num}</h3>
<h3>${username}</h3>
<h3>${password}</h3>
<h3>${requestScope.userParam}</h3>
<hr>
<h2>2.通过ModelAndView</h2>
<h3>${requestScope.num}</h3>
<h3>${requestScope.username}</h3>
<h3>${requestScope.password}</h3>
<%--<h3>${pageScope.password}</h3>
<h3>${sessionScope.password}</h3>
<h3>${applicationScope.password}</h3>--%>
<h3>${requestScope.userParam}</h3>
<hr>
<h2>3.通过ModelMap</h2>
<h3>${requestScope.num}</h3>
<h3>${requestScope.username}</h3>
<h3>${requestScope.password}</h3>
<h3>${requestScope.userParam}</h3>
<hr>
<h2>4.通过HttpSession</h2>
<h3>${sessionScope.num}</h3>
<h3>${sessionScope.username}</h3>
<h3>${sessionScope.password}</h3>
<h3>${sessionScope.userParam}</h3>
</body>
</html>
二、重定向
参考原文
1、方法返回值是String类型
如果返回值是String(视图名),在重定向地址前加 "redirect:xxx.do",这样跳转的请求和原请求就是两个不相干的请求,跳转的请求会丢失掉原请求的中的所有数据,一般的解决方法是将原请求中的数据放到跳转请求的链接中这样来获取数据。
重定向是浏览器端向一个新地址发送请求,应写成:"redirect:xxx.do"的形式。
@RequestMapping("/login8.do")
public String login8(){
System.out.println("login8()");
return "redirect:toIndex.do";
}
@RequestMapping("/toIndex.do")
public String toIndex(){
System.out.println("toIndex()");
return "index";
}
2、返回值是ModelAndView
使用RedirectView对象↓
springMVC源码分析--页面跳转RedirectView(三)_归田的博客-CSDN博客_redirectview
简单来说RedirectView实现了链接的重定向,并且将数据保存到FlashMap中, 这样在跳转后的链接中可以获取一些数据。
RequestMapping("/login9.do")
public ModelAndView login9(){
//创建RedirectView
System.out.println("login9()");
RedirectView redirectView = new RedirectView("toIndex.do");
ModelAndView modelAndView = new ModelAndView(redirectView);
return modelAndView;
}
三、请求参数乱码问题
1、为什么会产生乱码?
表单提交时,浏览器会对中文进行编码(会使用打开表单所在页面时的字符集来编码,比如使用utf-8来编码).而服务器端默认使用ISO-8859-1来解码,所以会产生乱码。
2、如何解决乱码?
springmvc提供了一个过滤器(CharacterEncodingFilter), 只需要配置该过滤器即可。
注意:
(1)表单的提交方式必须是"post"。
(2)编码与浏览器端的一致。
<filter>
<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>