Spring MVC系列[2]——参数传递及重定向
1.目录结构

2.代码
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="2.5" 3 xmlns="http://java.sun.com/xml/ns/javaee" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 6 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 7 <servlet> 8 <servlet-name>springmvc</servlet-name> 9 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 10 <init-param> 11 <param-name>contextConfigLocation</param-name> 12 <param-value>classpath:spring-mvc.xml</param-value> 13 </init-param> 14 <load-on-startup>1</load-on-startup> 15 </servlet> 16 17 <servlet-mapping> 18 <servlet-name>springmvc</servlet-name> 19 <url-pattern>*.do</url-pattern> 20 </servlet-mapping> 21 </web-app>
1 <?xml version="1.0" encoding="utf-8"?> 2 3 <beans xmlns="http://www.springframework.org/schema/beans" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" 6 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 7 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" 8 xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemaLocation=" 9 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 10 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd 11 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd 12 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd 13 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd 14 http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd 15 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 16 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 17 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"> 18 19 <!-- 开启注解扫描 --> 20 <context:component-scan base-package="com.java"/> 21 22 <!-- 开启mvc注解扫描 --> 23 <mvc:annotation-driven/> 24 25 <!-- 定义视图解析器 --> 26 <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 27 <property name="prefix" value="/WEB-INF/"/> 28 <property name="suffix" value=".jsp"/> 29 </bean> 30 31 </beans>
1 package com.java.entity; 2 3 public class User { 4 private Integer userId; 5 private String userName; 6 private String password; 7 8 9 public Integer getUserId() { 10 return userId; 11 } 12 public void setUserId(Integer userId) { 13 this.userId = userId; 14 } 15 public String getuserName() { 16 return userName; 17 } 18 public void setuserName(String userName) { 19 this.userName = userName; 20 } 21 public String getPassword() { 22 return password; 23 } 24 public void setPassword(String password) { 25 this.password = password; 26 } 27 28 }
1 package com.java.controller; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpSession; 8 9 import org.springframework.stereotype.Controller; 10 import org.springframework.ui.ModelMap; 11 import org.springframework.web.bind.annotation.ModelAttribute; 12 import org.springframework.web.bind.annotation.RequestMapping; 13 import org.springframework.web.bind.annotation.RequestParam; 14 import org.springframework.web.servlet.ModelAndView; 15 import org.springframework.web.servlet.view.RedirectView; 16 17 import com.java.entity.User; 18 19 @Controller 20 @RequestMapping("/demo") 21 public class HelloController { 22 23 /** 24 * 接收参数方式一:采用HttpServletRequest 25 * @param request 26 * @return 27 */ 28 @RequestMapping("/test1.do") 29 public String test1(HttpServletRequest request){ 30 String userName = request.getParameter("userName"); 31 String password = request.getParameter("password"); 32 33 System.out.println("userName:"+userName); 34 System.out.println("password:"+password); 35 36 return "jsp/success"; 37 } 38 39 /** 40 * 接收参数方式二:采用@RequestParam注解方式 41 * @param userName 42 * @param password 43 * @return 44 */ 45 @RequestMapping("/test2.do") 46 public String test2( 47 @RequestParam String userName, 48 @RequestParam String password){ 49 System.out.println("userName:"+userName); 50 System.out.println("password:"+password); 51 return "jsp/success"; 52 } 53 54 /** 55 * 接收参数方式三:采用实体方式 56 * 前提是: 被提交表单中name 和 实体中的属性完全一致 57 * @param user 58 * @return 59 */ 60 @RequestMapping("/test3.do") 61 public String test3(User user){ 62 System.out.println("userName:"+user.getuserName()); 63 System.out.println("password:"+user.getPassword()); 64 return "jsp/success"; 65 } 66 67 68 //////////////////////////////////////////////////////////// 69 /*以下是对【传出参数 的研究】*/ 70 71 /** 72 * 传出参数方式一:使用ModelAndView传出参数 73 */ 74 @RequestMapping("test4.do") 75 public ModelAndView test4(){ 76 Map<String, Object> data = new HashMap<String, Object> (); 77 data.put("success", true); 78 data.put("message", "操作成功"); 79 return new ModelAndView("jsp/success",data); 80 } 81 82 /** 83 * 传出参数方式二:使用ModelMap传出参数 84 * @param map 85 * @return 86 */ 87 @RequestMapping("test5.do") 88 public ModelAndView test5(ModelMap map){ 89 map.addAttribute("success", false); 90 map.addAttribute("message", "操作失败"); 91 return new ModelAndView("jsp/success"); 92 } 93 94 /** 95 * 使用ModelAttribute传出实体属性值 96 * ModelAttribute 会利用 HttpServletRequest 的 Attribute传递到JSP页面中。 97 * @return 98 */ 99 @ModelAttribute("age") 100 public int getAge(){ 101 return 25; 102 } 103 104 /** 105 * 传出参数方式三:使用ModelAttribute传出参数 106 * 需要注意的是:@ModelAttribute括号里面的名字应该与表单name保持一致 107 * @param userName 108 * @param password 109 * @return 110 */ 111 @RequestMapping("/test6.do") 112 public ModelAndView test6( 113 @ModelAttribute("userName") String userName, 114 @ModelAttribute("password") String password){ 115 116 return new ModelAndView("jsp/success"); 117 } 118 119 //////////////////////////////////////////////////////////// 120 /*以下是对【session 和 重定向 的研究】*/ 121 122 /** 123 * session的使用 124 * 可以利用session传递参数 125 */ 126 @RequestMapping("test7.do") 127 public ModelAndView test7(HttpServletRequest request){ 128 129 String userName = request.getParameter("userName"); 130 String password = request.getParameter("password"); 131 132 HttpSession session = request.getSession(); 133 134 session.setAttribute("sal", 8000); 135 session.setAttribute("userName", userName); 136 session.setAttribute("password", password); 137 138 return new ModelAndView("jsp/success"); 139 } 140 141 /** 142 * 测试String 143 * @param user 144 * @param modelmap 145 * @return 146 */ 147 @RequestMapping("test8.do") 148 public String test8(User user, ModelMap modelmap){ 149 modelmap.addAttribute("user", user); 150 return "jsp/success"; 151 } 152 153 /** 154 * 返回错误页面 155 * @return 156 */ 157 @RequestMapping("test9.do") 158 public String test9(){ 159 return "jsp/error"; 160 } 161 162 /** 163 * 重定向方式一:利用RedirectView重定向 164 * @param user 165 * @return 166 */ 167 @RequestMapping("test10.do") 168 public ModelAndView test10(User user){ 169 if(user.getuserName().equals("java")){ 170 return new ModelAndView("jsp/success"); 171 } 172 return new ModelAndView(new RedirectView("test9.do")); 173 } 174 175 /** 176 * 重定向方式二:利用redirect:前缀重定向 177 * 结果和方式一完全一样 178 * @param user 179 * @return 180 */ 181 @RequestMapping("test11.do") 182 public String test11(User user){ 183 if("java".equals(user.getuserName())){ 184 return "jsp/success"; 185 } 186 return "redirect:test9.do"; 187 } 188 }
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP 'index.jsp' starting page</title> 13 <meta http-equiv="pragma" content="no-cache"> 14 <meta http-equiv="cache-control" content="no-cache"> 15 <meta http-equiv="expires" content="0"> 16 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 17 <meta http-equiv="description" content="This is my page"> 18 <!-- 19 <link rel="stylesheet" type="text/css" href="styles.css"> 20 --> 21 </head> 22 23 <body> 24 <h2>测试</h2> 25 <!-- 26 <a href="demo/test1.do">点击我试试</a> 27 --> 28 <hr> 29 30 <form action="demo/test11.do" method="post"> 31 <label id="userName">用户名:</label><input type="text" name="userName"/><br/> 32 <label id="password">密 码:</label><input type="password" name="password"/><br/> 33 <input type="submit" value="登录"/> 34 </form> 35 </body> 36 </html>
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP 'MyJsp.jsp' starting page</title> 13 14 <meta http-equiv="pragma" content="no-cache"> 15 <meta http-equiv="cache-control" content="no-cache"> 16 <meta http-equiv="expires" content="0"> 17 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 18 <meta http-equiv="description" content="This is my page"> 19 <!-- 20 <link rel="stylesheet" type="text/css" href="styles.css"> 21 --> 22 23 </head> 24 25 <body> 26 <h2>是否成功:${success}</h2> 27 <h2>提示消息:${message}</h2> 28 29 <hr/> 30 31 <h2>年龄:${age}</h2> 32 33 <h2>用户名:${userName}</h2> 34 <h2>密码:${user.password}</h2> 35 36 <h2>薪资:${sal}</h2> 37 38 <h2>对象:${user.userName}</h2> 39 </body> 40 </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=ISO-8859-1"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 不好,页面被外星人劫持! 11 </body> 12 </html>

浙公网安备 33010602011771号