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 ...
随机推荐
- webRTC源码下载 Windows Mac(iOS) Linux(Android)全
webRTC源码下载地址:https://pan.baidu.com/s/18CjClvAuz3B9oF33ngbJIw 提取码:wl1e Windows版:visual studio 2017工 ...
- C# 最全的系统帮助类
using System;using System.Collections;using System.Collections.Generic;using System.Configuration;us ...
- spring mvc中关于url中传递中文乱码的解决方法
在传值过程中,也是乱码出现的频繁地.先不说到底是什么场景了,通常常用的方案有如下几个 配置指定的filter <!-- 配置请求过滤器,编码格式设为UTF-8,避免中文乱码--> < ...
- 4、QT分析之调试跟踪系统
原文地址:http://blog.163.com/net_worm/blog/static/127702419201002004518944/ 在我们前面的分析中,经常看到qWarning()和qDe ...
- 【转】关于HTTP服务器每个客户端2个连接的限制
http://www.cnblogs.com/lishenglyx/archive/2010/01/07/1641190.html 这两天猫在家里搞一个多线程的断点续传得C#程序,发现同时只能开2个线 ...
- java不确定参数个数方法例子
package test; public class myTest { // 根据输入的参数动态生成数组 public static int max(int... param) { int max = ...
- Semantic segmentation using adversarial networks
FAIR Paris分部的论文,NIPS2016 Workshop. Motivation是让predict出来的结果和真实label在高层感觉上有一致性. 基本思想就是用GAN来区分segmenta ...
- mysql函数find_in_set()
SELECT FIND_IN_SET('b','a,b,c,d'); 结果:2 SELECT * from video where find_in_set(id,'1,2,3,4'); 查找id在‘1 ...
- Eclipse SVN过滤文件夹及文件的方法
有两个步骤: 1.在TortoiseSVN中将文件.文件夹加入ignore list: 2.在Eclipse中Ignored Resources利用模式匹配忽略指定文件夹.文件类型. 这样,那些无需同 ...
- ubuntu 安装 SVN 后的错误:Subversion Native Library Not Available & Incompatible JavaHL library loaded
问题一 安装了SVN的eclipse插件,使用的时候就会弹出一个错误的提示框: Subversion Native Library Not Available,加载不到JavaHL. 解决方法 ...