Spring MVC(三)控制器獲取頁面請求引數以及將控制器資料傳遞給頁面和實現重定向的方式
阿新 • • 發佈:2018-11-11
首先做好環境配置
在mvc.xml裡進行配置
1.開啟元件掃描
2.開啟基於mvc的標註
3.配置試圖處理器
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5mvc.xmlxmlns:lang="http://www.springframework.org/schema/lang" 6 xmlns:mvc="http://www.springframework.org/schema/mvc" 7 xmlns:util="http://www.springframework.org/schema/util" 8 xmlns:task="http://www.springframework.org/schema/task" 9 xmlns:aop="http://www.springframework.org/schema/aop" 10 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd 11http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd 12 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 13 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd14 http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.1.xsd 15 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd 16 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd"> 17 <!-- 開啟元件掃描 --> 18 <context:component-scan base-package="com.xcz"></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>
在web.xml配置
1.配置請求引數如入口
2.配置初始化引數
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/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>classpath:mvc.xml</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>web.xml
控制器獲取頁面請求引數方式如下:
1.使用HttpServletRequest獲取直接定義 HttpServletRequest引數
2.直接把請求引數的名字定義成控制器的引數名
3.當頁面引數和控制器引數不一致可以使用 @RequestParam("頁面引數名"),加在控制器方法對應的引數上
1 package com.xcz.controller; 2 3 import javax.servlet.http.HttpServletRequest; 4 5 import org.springframework.stereotype.Controller; 6 import org.springframework.ui.Model; 7 import org.springframework.ui.ModelMap; 8 import org.springframework.web.bind.annotation.RequestMapping; 9 import org.springframework.web.bind.annotation.RequestParam; 10 import org.springframework.web.servlet.ModelAndView; 11 12 @Controller 13 public class LoginController { 14 // 獲取引數方式一 15 @RequestMapping("/login.do") 16 public String login(HttpServletRequest request) { 17 String username = request.getParameter("username"); 18 String password = request.getParameter("password"); 19 System.out.println(username + ":" + password); 20 return "login"; 21 } 22 23 // 獲取引數方式二 24 @RequestMapping("/login2.do") 25 public String login2(String username, String password) { 26 System.out.println(username + ":" + password); 27 return "login"; 28 } 29 30 // 獲取引數方式三 31 @RequestMapping("/login3.do") 32 public String login3(@RequestParam("username") String uname, @RequestParam("password") String pwd) { 33 System.out.println(uname + ":" + pwd); 34 return "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" "http://www.w3.org/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="${pageContext.request.contextPath }/login8.do"> <!--${pageContext.request.contextPath }動態獲取路徑 --> 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來傳輸資料
//mav.getModel().put("username", username);
mav.getModelMap().addAttribute("username", username);
3.使用 Model 來傳輸資料
4.使用ModelMap 進行傳參
1 package com.xcz.controller; 2 3 import javax.servlet.http.HttpServletRequest; 4 5 import org.springframework.stereotype.Controller; 6 import org.springframework.ui.Model; 7 import org.springframework.ui.ModelMap; 8 import org.springframework.web.bind.annotation.RequestMapping; 9 import org.springframework.web.bind.annotation.RequestParam; 10 import org.springframework.web.servlet.ModelAndView; 11 12 @Controller 13 public class LoginController { 14 // 將控制器中的資料傳給頁面方式一 15 @RequestMapping("/login4.do") 16 public String login4(@RequestParam("username") String uname, @RequestParam("password") String pwd, 17 HttpServletRequest request) { 18 System.out.println(uname + ":" + pwd); 19 request.setAttribute("username", uname); 20 return "main"; 21 } 22 23 // 將控制器中的資料傳給頁面方式二 24 @RequestMapping("/login5.do") 25 public 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 mav.getModel().put("username", uname); 30 return mav; 31 } 32 33 // 將控制器中的資料傳給頁面方式三 34 @RequestMapping("/login6.do") 35 public ModelAndView login6(@RequestParam("username") String uname, @RequestParam("password") String pwd, 36 ModelAndView mav) { 37 System.out.println(uname + ":" + pwd); 38 mav.setViewName("main"); 39 mav.getModelMap().addAttribute("username", uname); 40 // mav.getModelMap().put("username", uname); 41 return mav; 42 } 43 44 // 將控制器中的資料傳給頁面方式四 45 @RequestMapping("/login7.do") 46 public String login7(@RequestParam("username") String uname, @RequestParam("password") String pwd, Model model) { 47 System.out.println(uname + ":" + pwd); 48 model.addAttribute("username", uname); 49 return "main"; 50 } 51 52 //將控制器中的資料傳給頁面方式五 53 @RequestMapping("/login8.do") 54 public String login8(@RequestParam("username") String uname, @RequestParam("password") String pwd,ModelMap map) { 55 System.out.println(uname + ":" + pwd); 56 map.put("username", uname); 57 return "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" "http://www.w3.org/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="${pageContext.request.contextPath }/login8.do"> <!--${pageContext.request.contextPath }動態獲取路徑 --> 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" "http://www.w3.org/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 完成重定向 (/代表專案名前面的部分 不包含專案名)
1 package com.xcz.controller; 2 3 import javax.servlet.http.HttpServletRequest; 4 import org.springframework.stereotype.Controller; 5 import org.springframework.web.bind.annotation.RequestMapping; 6 import org.springframework.web.bind.annotation.RequestParam; 7 import org.springframework.web.servlet.ModelAndView; 8 import org.springframework.web.servlet.view.RedirectView; 9 10 @Controller 11 public class LoginController { 12 private static final String URL = "toMain.do"; 13 @RequestMapping("/toLogin.do") 14 public String toLogin() { 15 return "login"; 16 } 17 @RequestMapping("/toMain.do") 18 public String toMain() { 19 return "main"; 20 } 21 // 實現重定向方式一 22 @RequestMapping("/login.do") 23 public 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: 直接重定向,導致資料丟失,所以頁面無法獲取 28 return "forward:/toMain.do"; //使用forward: 先轉發後跳轉到main.jsp,不會導致資料丟失,所以頁面可以獲取控制器資料 29 } 30 // 實現重定向方式二 31 @RequestMapping("/login1.do") 32 public ModelAndView login1(@RequestParam("username") String uname,@RequestParam("password") String pwd,HttpServletRequest request) { 33 System.out.println(uname + ":" + pwd); //列印後臺資料 34 String path = request.getServletContext().getContextPath(); //獲取專案名前面的路徑 35 ModelAndView mView = new ModelAndView(); 36 RedirectView rView = new RedirectView(path + "/" + URL); //將路徑和專案名進行拼接起來 37 mView.setView(rView); 38 mView.getModelMap().addAttribute("username", uname); //將控制器的資料傳給頁面 39 return 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" "http://www.w3.org/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="${pageContext.request.contextPath }/login1.do"> <!--${pageContext.request.contextPath }動態獲取路徑 --> 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" "http://www.w3.org/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