SpringMVC(三)控制器获取页⾯请求参数以及将控制器数据传递给页⾯和实现
重定向的⽅式
⾸先做好环境配置
在l⾥进⾏配置
1.开启组件扫描
2.开启基于mvc的标注
3.配置试图处理器
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="/schema/beans"
3 xmlns:xsi="/2001/XMLSchema-instance"
4 xmlns:context="/schema/context"
5 xmlns:lang="/schema/lang"
6 xmlns:mvc="/schema/mvc"
7 xmlns:util="/schema/util"
8 xmlns:task="/schema/task"
9 xmlns:aop="/schema/aop"
10 xsi:schemaLocation="/schema/mvc /schema/mvc/spring-mvc-4.1.xsd
11 /schema/task /schema/task/spring-task-4.1.xsd
12 /schema/beans /schema/beans/spring-beans.xsd
13 /schema/context /schema/context/spring-context-4.1.xsd
14 /schema/lang /schema/lang/spring-lang-4.1.xsd
15 /schema/aop /schema/aop/spring-aop-4.1.xsd
16 /schema/util /schema/util/spring-util-4.1.xsd">
17 <!-- 开启组件扫描 -->
18 <context:component-scan base-package=""></context:component-scan>
19 <!-- 开启mvc标注 -->
20 <mvc:annotation-driven></mvc:annotation-driven>
21 <!-- 配置视图处理器 -->
22 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
23 <property name="prefix" value="/WEB-INF/"></property>
24 <property name="suffix" value=".jsp"></property>
25 </bean>
26 </beans>
在l配置
1.配置请求参数如⼊⼝
2.配置初始化参数
1 <?xml version="1.0" encoding="UTF-8"?>
2 <web-app xmlns:xsi="/2001/XMLSchema-instance" xmlns="/xml/ns/javaee" xsi:schemaLocation="/xml/ns/javaee /xml/ns/javaee/web-app_3_1.xsd" version="3.1">
3 <display-name>SpringMVC-03</display-name>
4 <welcome-file-list>
5 <welcome-file>index.html</welcome-file>
6 <welcome-file>index.htm</welcome-file>
7 <welcome-file>index.jsp</welcome-file>
8 <welcome-file>default.html</welcome-file>
9 <welcome-file>default.htm</welcome-file>
10 <welcome-file>default.jsp</welcome-file>
11 </welcome-file-list>
12 <!-- 配置请求⼊⼝ -->
13 <servlet>
14 <servlet-name>SpringMVC</servlet-name>
15 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
16 <!-- 配置初始化参数 -->
17 <init-param>
18 <param-name>contextConfigLocation</param-name>
19 <param-value>l</param-value>
20 </init-param>
21 <load-on-startup>1</load-on-startup>
22 </servlet>
23 <servlet-mapping>
24 <servlet-name>SpringMVC</servlet-name>
25 <url-pattern>*.do</url-pattern>
26 </servlet-mapping>
27 </web-app>
控制器获取页⾯请求参数⽅式如下:
1.使⽤HttpServletRequest获取直接定义 HttpServletRequest参数
2.直接把请求参数的名字定义成控制器的参数名
3.当页⾯参数和控制器参数不⼀致可以使⽤ @RequestParam("页⾯参数名"),加在控制器⽅法对应的参数上
ntroller;
2
3import javax.servlet.http.HttpServletRequest;
4
5import org.springframework.stereotype.Controller;
6import org.springframework.ui.Model;
7import org.springframework.ui.ModelMap;
8import org.springframework.web.bind.annotation.RequestMapping;
9import org.springframework.web.bind.annotation.RequestParam;
10import org.springframework.web.servlet.ModelAndView;
11
12 @Controller
13public class LoginController {
14// 获取参数⽅式⼀
15 @RequestMapping("/login.do")
16public String login(HttpServletRequest request) {
17 String username = Parameter("username");
18 String password = Parameter("password");
19 System.out.println(username + ":" + password);
20return "login";
21 }
22
23// 获取参数⽅式⼆
24 @RequestMapping("/login2.do")
25public String login2(String username, String password) {
26 System.out.println(username + ":" + password);
27return "login";
29
30// 获取参数⽅式三
31 @RequestMapping("/login3.do")
32public String login3(@RequestParam("username") String uname, @RequestParam("password") String pwd) {
33 System.out.println(uname + ":" + pwd);
34return "login";
35 }
36 }
LoginController
1 <%@ page language="java" contentType="text/html; charset=utf-8"
2 pageEncoding="utf-8"%>
3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "/TR/html4/loose.dtd">
4 <html>
5 <head>
6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
7 <title>Insert title here</title>
8 </head>
9 <body>
10 <form action="${tPath }/login8.do"> <!--${tPath }动态获取路径 --> 11账号:<input type="text" name="username"
><br>
12密码:<input type="text" name="password" ><br>
13 <input type="submit" value="登录"><br>
14 </form>
15 </body>
16 </html>
login.lsp
控制器中数据传递给页⾯的⽅式如下:
1.使⽤ request session application 这些域对象传输
2.使⽤ModelAndView来传输数据
//Model().put("username", username);
ModelMap().addAttribute("username", username);
3.使⽤ Model 来传输数据
4.使⽤ModelMap 进⾏传参
ntroller;
2
3import javax.servlet.http.HttpServletRequest;
4
5import org.springframework.stereotype.Controller;
6import org.springframework.ui.Model;
7import org.springframework.ui.ModelMap;
8import org.springframework.web.bind.annotation.RequestMapping;
9import org.springframework.web.bind.annotation.RequestParam;
10import org.springframework.web.servlet.ModelAndView;
11
12 @Controller
13public class LoginController {
14// 將控制器中的数据传给页⾯⽅式⼀
15 @RequestMapping("/login4.do")
16public String login4(@RequestParam("username") String uname, @RequestParam("password") String pwd,
17 HttpServletRequest request) {
18 System.out.println(uname + ":" + pwd);
19 request.setAttribute("username", uname);
20return "main";
21 }
22
23// 將控制器中的数据传给页⾯⽅式⼆
24 @RequestMapping("/login5.do")
25public ModelAndView login5(@RequestParam("username") String uname, @RequestParam("password") String pwd) {
26 System.out.println(uname + ":" + pwd);
27 ModelAndView mav = new ModelAndView();
28 mav.setViewName("main");
29 Model().put("username", uname);
30return mav;
spring framework组件31 }
32
33// 將控制器中的数据传给页⾯⽅式三
34 @RequestMapping("/login6.do")
35public ModelAndView login6(@RequestParam("username") String uname, @RequestParam("password") String pwd,
36 ModelAndView mav) {
37 System.out.println(uname + ":" + pwd);
38 mav.setViewName("main");
39 ModelMap().addAttribute("username", uname);
40// ModelMap().put("username", uname);
41return mav;
42 }
43
44// 將控制器中的数据传给页⾯⽅式四
45 @RequestMapping("/login7.do")
46public String login7(@RequestParam("username") String uname, @RequestParam("password") String pwd, Model model) {
47 System.out.println(uname + ":" + pwd);
48 model.addAttribute("username", uname);
49return "main";
50 }
51
52//將控制器中的数据传给页⾯⽅式五
53 @RequestMapping("/login8.do")
54public String login8(@RequestParam("username") String uname, @RequestParam("password") String pwd,ModelMap map) {
55 System.out.println(uname + ":" + pwd);
56 map.put("username", uname);
57return "main";
58 }
59 }
LoginController
1 <%@ page language="java" contentType="text/html; charset=utf-8"
2 pageEncoding="utf-8"%>
3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "/TR/html4/loose.dtd">
4 <html>
5 <head>
6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
7 <title>Insert title here</title>
8 </head>
9 <body>
10 <form action="${tPath }/login8.do"> <!--${tPath }动态获取路径 --> 11账号:<input type="text" name="username" ><br>
12密码:<input type="text" name="password" ><br>
13 <input type="submit" value="登录"><br>
14 </form>
15 </body>
16 </html>
1 <%@ page language="java" contentType="text/html; charset=utf-8"
2 pageEncoding="utf-8"%>
3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "/TR/html4/loose.dtd">
4 <html>
5 <head>
6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
7 <title>Insert title here</title>
8 </head>
9 <body>
10 <h1>欢迎${username }来访</h1>
11 </body>
12 </html>
main.jsp
实现重定向⽅式如下:
1.当控制器⽅法返回String时return"redirect:路径";
默认是转发,转发的结果直接交给ViewResolver可以通过加forward:来继续处理,⽽不交给ViewResolver
2.当控制器⽅法返回 ModelAndView 时使⽤RedirectView 完成重定向(/代表项⽬名前⾯的部分不包含项⽬名)
ntroller;
2
3import javax.servlet.http.HttpServletRequest;
4import org.springframework.stereotype.Controller;
5import org.springframework.web.bind.annotation.RequestMapping;
6import org.springframework.web.bind.annotation.RequestParam;
7import org.springframework.web.servlet.ModelAndView;
8import org.springframework.web.servlet.view.RedirectView;
9
10 @Controller
11public class LoginController {
12private static final String URL = "toMain.do";
13 @RequestMapping("/toLogin.do")
14public String toLogin() {
15return "login";
16 }
17 @RequestMapping("/toMain.do")
18public String toMain() {
19return "main";
20 }
21// 实现重定向⽅式⼀
22 @RequestMapping("/login.do")
23public String login(@RequestParam("username") String uname,@RequestParam("password") String pwd,HttpServletRequest request) {
24 System.out.println(uname + ":" + pwd);
25 request.setAttribute("usernameq", uname);
26 request.setAttribute("password", pwd);
27//return "redirect:/toMain.do"; //使⽤redirect: 直接重定向,导致数据丢失,所以页⾯⽆法获取
28return "forward:/toMain.do"; //使⽤forward: 先转发后跳转到main.jsp,不会导致数据丢失,所以页⾯可以获取控制器数据
29 }
30// 实现重定向⽅式⼆
31 @RequestMapping("/login1.do")
32public ModelAndView login1(@RequestParam("username") String uname,@RequestParam("passw
ord") String pwd,HttpServletRequest request) {
33 System.out.println(uname + ":" + pwd); //打印后台数据
34 String path = ServletContext().getContextPath(); //获取项⽬名前⾯的路径
35 ModelAndView mView = new ModelAndView();
36 RedirectView rView = new RedirectView(path + "/" + URL); //将路径和项⽬名进⾏拼接起来
37 mView.setView(rView);
38 ModelMap().addAttribute("username", uname); //将控制器的数据传给页⾯
39return mView;
40 }
41 }
LoginController
1 <%@ page language="java" contentType="text/html; charset=utf-8"
2 pageEncoding="utf-8"%>
3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "/TR/html4/loose.dtd">
4 <html>
5 <head>
6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
7 <title>Insert title here</title>
8 </head>
9 <body>
10 <form action="${tPath }/login1.do"> <!--${tPath }动态获取路径 -->
11账号:<input type="text" name="username" ><br>
12密码:<input type="text" name="password" ><br>
13 <input type="submit" value="登录"><br>
14 </form>
15 </body>
16 </html>
login.jsp
1 <%@ page language="java" contentType="text/html; charset=utf-8"
2 pageEncoding="utf-8"%>
3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "/TR/html4/loose.dtd">
4 <html>
5 <head>
6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
7 <title>Insert title here</title>
8 </head>
9 <body>
10 <h1>欢迎${param.username }来访</h1>
11 </body>
12 </html>
main.jsp
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论