首先做好环境配置
在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" 5 xmlns: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 11 http://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.xsd 14 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>
控制器获取页面请求参数方式如下:
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 }
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>
控制器中数据传递给页面的方式如下:
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 }
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>
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>
实现重定向方式如下:
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 }
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>
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>