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>
啦啦啦
SpringMVC -- 梗概--源码--壹--数据传递的更多相关文章
- SpringMVC -- 梗概--源码--壹--springMVC json处理
附:实体类 Class : User package com.c61.entity; import java.text.SimpleDateFormat; import java.util.Date; ...
- SpringMVC -- 梗概--源码--壹--收参
附:实体类 Class : User package com.c61.entity; import java.text.SimpleDateFormat; import java.util.Date; ...
- SpringMVC -- 梗概--源码--壹--跳转
1.配置web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version=&qu ...
- SpringMVC -- 梗概--源码--贰--拦截器:Interceptor
附:实体类 1.配置web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app versi ...
- SpringMVC -- 梗概--源码--贰--下载
1.配置web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version=&qu ...
- SpringMVC -- 梗概--源码--贰--上传
1.配置web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version=&qu ...
- SpringMVC -- 梗概--源码--贰--异常管理
附:实体类 Class : User package com.c61.entity; import java.text.SimpleDateFormat; import java.util.Date; ...
- SpringMVC -- 梗概--源码--贰--RestFul收参(了解) @PathVariable
1>定制方式: //如下两个路径都可以访问到如下方法,请求路径不同,则name61和pwd61匹配到的值不同 //http://localhost:8989/appname/ful/lime/1 ...
- SpringMVC -- 梗概--源码--贰--mvc:annotation-driven
1>在springMVC的处理流程中,有两个重要组件:HandlerMapping和HandlerAdapter 分别负责解析Handler和执行Handler 2>如果配置了<mv ...
随机推荐
- Hive Tunning 补充 关于bucket
在前面的几篇文章当中一直有一个概念bucketing不清楚到底是怎么回事. 网友南京-李先森给了他收集的一些资料,如下: Buckets 对指定列计算 hash,根据 hash 值切分数据,目的是为了 ...
- ambari删除脚本
#.删除hdp.repo和hdp-util.repo cd /etc/yum.repos.d/ rm -rf hdp* rm -rf HDP* #rm -rf ambari* #.删除安装包 #用yu ...
- 【C】——pthread_mutex_lock
函数名 pthread_mutex_lock, pthread_mutex_trylock, pthread_mutex_unlock - lock and unlock a mutex SYNOPS ...
- Ubuntu free以及Linux内存占用大的解释
-bash-3.00$ free total used free shared buffers cached Mem: 514020 465932 48088 0 15864 348844 -/ bu ...
- 《编程之美》practice
1.2.中国象棋将帅问题 要求:只用一个字节存储变量,输出将帅不照面的所有可能位置. 思路简单,就是穷举让将和帅不在同一列即可,用char高四字节和低四字节分别存储将和帅的位置,位置编号从1到9.代码 ...
- Android-FragmentPagerAdapter刷新无效的解决方案
按照通常使用ListView的习惯做法,如果你只是更新保存Fragment的List数据,然后调用adapter的notifyDataSetChanged()是不会起作用的. 搜索了下发现此问题普遍存 ...
- JSP之response对象使用
1.使用response对象实现重定向 response.sendRedirect("http://www.cnblogs.com"); 2.使用response对象刷新页面 每隔 ...
- 10、QT分析之WebKit
该文章整理自 网易博客 http://blog.163.com/net_worm/blog/static/12770241920101831312381/ 转载请注明出处 WebKit是QT4新整合的 ...
- [转]Android开源测试框架学习
近期因工作需要,分析了一些Android的测试框架,在这也分享下整理完的资料. Android测试大致分三大块: 代码层测试 用户操作模拟,功能测试 安装部署及稳定性测试 代码层测试 对于一般java ...
- Solr系列五:solr搜索详解(solr搜索流程介绍、查询语法及解析器详解)
一.solr搜索流程介绍 1. 前面我们已经学习过Lucene搜索的流程,让我们再来回顾一下 流程说明: 首先获取用户输入的查询串,使用查询解析器QueryParser解析查询串生成查询对象Query ...