SpringMVC -- 梗概--源码--壹--数据传递
附:实体类
Class : User
package com.c61.entity; import java.text.SimpleDateFormat; import java.util.Date; import org.springframework.format.annotation.DateTimeFormat; import com.alibaba.fastjson.annotation.JSONField; public class User { private Integer id; private String name; //@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") @DateTimeFormat(pattern="yyyy-MM-dd")//定制在接收请求参数时的日期格式 @JSONField(format="yyyy-MM-dd")//作用在java序列化成json时 private Date birth; private String dateStr; public String getDateStr() { return dateStr; } public void setDateStr(String dateStr) { this.dateStr = dateStr; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getBirth() { return birth; } public void setBirth(Date birth) { this.birth = birth; SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd"); this.dateStr=format.format(birth); } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", birth=" + birth + "]"; } public User(){} public User(Integer id, String name, Date birth) { super(); this.id = id; this.name = name; this.birth = birth; } }
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1.配置web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 前端控制器 /=默认的url-pattern /a/b/c /a /a/d/c /a/d /a / *注意:此控制器默认加载/WEB-INF下的xxx-servlet.xml文件 :其中xxx等于【DispatcherServlet的配置名】 --> <servlet> <servlet-name>mvc61</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:mvc62.xml</param-value> </init-param> <!-- 随项目启动而启动 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mvc61</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 专治Post请求参数乱码 --> <filter> <filter-name>encoding61</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <!-- 将请求的编码方式设置为utf-8 --> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encoding61</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
2.配置控制器
Class : DataController
package com.c61.controller; import java.util.Date; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import com.c61.entity.User; @Controller @RequestMapping(value="/mvc4")//等价于namespace public class DataController { //DefaultAnnotationHandlerMapping a; @RequestMapping("/data1")//等价于<action name="mvc1" public String testData(HttpServletRequest req){ req.setAttribute("name", "limeOracle"); User user=new User(); user.setId(1); user.setName("lime"); user.setBirth(new Date()); req.setAttribute("user", user); return "forward:/data.jsp"; } @RequestMapping("/data2")//等价于<action name="mvc1" public String testData2(HttpSession session,Integer id,String name,HttpServletRequest req){ session.setAttribute("name", "limeOracle"); User user=new User(); user.setId(1); user.setName("lime"); user.setBirth(new Date()); session.setAttribute("user", user); return "redirect:/data2.jsp"; } @RequestMapping("/data3")//等价于<action name="mvc1" public String testData3(Model model,Map map,ModelMap modelM){ User user=new User(); user.setId(1); user.setName("lime"); user.setBirth(new Date()); //model.addAttribute("user",user); //model.addAttribute("name", "Oracle"); //map.put("user", user); //map.put("name", "Oracle2"); modelM.addAttribute("user", user); modelM.addAttribute("name", "Oracle3"); return "forward:/data.jsp"; } //当使用 Model,Map,ModelMap在重定向中传值时,可以将简单数据(数字,字符串)拼接在url中 @RequestMapping("/data4")//等价于<action name="mvc1" public String testData4(Model model,Map map,ModelMap modelM){ User user=new User(); user.setId(1); user.setName("lime"); user.setBirth(new Date()); //model.addAttribute("user",user); //model.addAttribute("name", "Oracle"); //map.put("user", user); //map.put("name", "Oracle2"); modelM.addAttribute("user", user); modelM.addAttribute("name", "Oracle3"); return "redirect:/data.jsp"; } }
3 配置视图
View : data.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> name:${requestScope.name}--<br/> user.id:${requestScope.user.id }--<br/> user.name:${requestScope.user.name }--<br/> user.dateStr:${requestScope.user.dateStr }--<br/> param.name:${param.name }--<br/> </body> </html>
Client :
View : data2.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> name:${sessionScope.name}--<br/> user.id:${sessionScope.user.id }--<br/> user.name:${sessionScope.user.name }--<br/> user.dateStr:${sessionScope.user.dateStr }--<br/> param.name:${param.name }<br/> </body> </html>
Client :
Client :
View : data.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> name:${requestScope.name}--<br/> user.id:${requestScope.user.id }--<br/> user.name:${requestScope.user.name }--<br/> user.dateStr:${requestScope.user.dateStr }--<br/> param.name:${param.name }--<br/> </body> </html>
Client :
View : data.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> name:${requestScope.name}--<br/> user.id:${requestScope.user.id }--<br/> user.name:${requestScope.user.name }--<br/> user.dateStr:${requestScope.user.dateStr }--<br/> param.name:${param.name }--<br/> </body> </html>
Client :
Client :
转发中数据传递
1>利用HttpServletRequest
public String testData(HttpServletRequest req){
req.setAttribute("name", "limeOracle");
...
}
页面中${requestScope.name}
2>Model Map ModelMap 存活周期为一个请求,可以在一个请求内数据传递
public String testData3(Model model,Map map,ModelMap modelM){
User user=new User(...);
//model.addAttribute("user",user);
//model.addAttribute("name", "Oracle");
//map.put("user", user);
//map.put("name", "Oracle2");
modelM.addAttribute("user", user);
modelM.addAttribute("name", "Oracle3");
...
}
页面中${requestScope.name}
${requestScope.user.id}
重定向中数据传递
1>利用session
public String testData2(HttpSession session){
session.setAttribute(“name”,xxx);
}
页面中:${sessionScope.name}
2>如果数据是简单数据(数字,字符串)的话
public String testData4(Model model,Map map,ModelMap modelM){
//会将简单数据拼接在url中,成为请求参数
modelM.addAttribute("name", "Oracle3");
return "redirect:/data.jsp";
}
页面中${param.name}
在JSP(View)中取值
EL+JSTL ${xxxx} <c:if> <c:forEach>
啦啦啦