SpringMVC传值、转发、重定向例子
- 练习接收页面参数值
- 使用request
- 使用@RequestParam注解
- 使用实体对象
- 练习向页面传出数据
- 使用HttpServletRequest和session
- 使用ModelAndView对象 (内部为利用HttpServletRequest的Attribute传递数据到页面)
- 使用ModelMap对象 (内部为利用HttpServletRequest的Attribute传递数据到页面)
- 使用@ModelAttribute注解 (内部为利用HttpServletRequest的Attribute传递数据到页面)
- 练习使用session
- 在Controller方法参数上直接声明HttpSession即可使用
- 练习重定向
- 使用RedirectView
- 使用redirect:
package web; import java.util.HashMap;
import java.util.Map; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView; import entity.User; //非注解方式
//public class HelloController implements Controller {
//
//
// public ModelAndView handleRequest(HttpServletRequest request,
// HttpServletResponse response) throws Exception {
// System.out.println("Hello, Controller.");
// return new ModelAndView("jsp/hello");
// }
//
//} @Controller
@RequestMapping("/demo")
public class HelloController{
private Integer age=22; @RequestMapping("hello.do")
public ModelAndView hello(HttpServletRequest request,
HttpServletResponse response) throws Exception{
return new ModelAndView("jsp/hello");
} /**
* 测试request接收参数*/
@RequestMapping("test1.do")
public ModelAndView test1(HttpServletRequest req){
String userName = req.getParameter("userName");
String password = req.getParameter("password");
System.out.println(userName);
System.out.println(password);
return new ModelAndView("jsp/hello");
}
/**
* 测试sping会自动将表单参数注入到方法参数
* 最好每个形参前都添加@requestparameter
* 通过反射只能得到方法参数类型不能等到方法参数名称 没有加注解能成功获得为编译器自动添加
*/
@RequestMapping("test2.do")
public ModelAndView test2(String userName,
@RequestParam("password") String pwd){
System.out.println(userName+","+pwd);
return new ModelAndView("jsp/hello");
} /**
* 测试对象接收参数
*/
@RequestMapping("test3.do")
public ModelAndView test3(User user){
System.out.println(user);
return new ModelAndView("jsp/hello");
} /**
* 使用ModelAndView传出参数 内部 HttpServletRequest的Attribute传递到jsp页面
* ModelAndView(String viewName,Map data)data是处理结果
*/
@RequestMapping("test4.do")
public ModelAndView test4(User user){
Map<String, Object> data = new HashMap<String, Object>();
data.put("user", user);
return new ModelAndView("jsp/hello",data);
} /**
* 使用ModelMap传出参数 内部HttpServletRequest的Attribute传递到jsp页面
*/
@RequestMapping("test5.do")
public ModelAndView test5(User user,ModelMap modelMap){
modelMap.put("user", user);
return new ModelAndView("jsp/hello");
} /**
* 使用ModelAttribute 内部HttpServletRequest的Attribute传递到jsp页面
* 在Contoller的参数部分或者bean属性方法上使用
*/
@RequestMapping("test6.do")
public ModelAndView test6(@ModelAttribute("user")User user){
return new ModelAndView("jsp/hello");
} @ModelAttribute("age")
public Integer getAge(){
return age;
} /**
* session存储 可以使用HttpServletRequest的getSession方法访问
*/
@RequestMapping("test7.do")
public ModelAndView test7(HttpServletRequest req){
HttpSession session = req.getSession();
session.setAttribute("salary", 6000.0);
return new ModelAndView("jsp/hello");
} //返回String 转发
@RequestMapping("/test8.do")
public String test8(User user, ModelMap model) {
model.addAttribute("user", user);
return "jsp/hello";
} /**
* 错误页面
*/
@RequestMapping("test9.do")
public String test9(){
return "error/error";
} /**
*使用RedirectView重定向
*/
@RequestMapping("test10")
public ModelAndView test10(User user){
if(user.getUserName().equals("123")){
return new ModelAndView("jsp/hello");//test10.do 转发
}else{
return new ModelAndView(new RedirectView("test9.do"));//test9.do?age=22 重定向
}
} /**
* 使用redirect重定向
*/
@RequestMapping("test11")
public String test11(User user){
if(user.getUserName().equals("123")){
return "jsp/hello";
}else{
return "redirect:test9.do";
}
}
}
user实体
package com.tarena.entity;
import java.io.Serializable;
public class User implements Serializable {
private Integer id;
private String userName;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
SpringMVC传值、转发、重定向例子的更多相关文章
- SpringMVC之转发重定向
package com.tz.controller; import org.springframework.stereotype.Controller; import org.springframew ...
- SSM-SpringMVC-22:SpringMVC中转发(forward)和重定向(redirect)
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 转发和重定向大家都熟悉,都学到框架了,怎么能不了解转发和重定向呢? 如果有不熟悉的,可以去百度搜几篇博客去看看 ...
- 04springMVC结构,mvc模式,spring-mvc流程,spring-mvc的第一个例子,三种handlerMapping,几种控制器,springmvc基于注解的开发,文件上传,拦截器,s
1. Spring-mvc介绍 1.1市面上流行的框架 Struts2(比较多) Springmvc(比较多而且属于上升的趋势) Struts1(即将被淘汰) 其他 1.2 spring-mv ...
- JavaWeb_day04搜索_乱码_路径_转发重定向_cookie
本文为博主辛苦总结,希望自己以后返回来看的时候理解更深刻,也希望可以起到帮助初学者的作用. 转载请注明 出自 : luogg的博客园 谢谢配合! 搜索功能 DAO层都是一些数据库的增删改查操作 Ser ...
- Python的Asyncore异步Socket模块及实现端口转发的例子
Python的Asyncore异步Socket模块及实现端口转发的例子 Asyncore模块提供了以异步的方式写入套接字服务客户端和服务器的基础结构. 只有两种方式使一个程序在单处理器上实现" ...
- SpringMVC中的重定向和转发的实现
1.请求转发和重定向的区别 请求重定向和请求转发都是web开发中资源跳转的方式. 请求转发是服务器内部的跳转 地址栏比发生变化 只有一个请求相应 可以通过request域对跳转目标的请求 请求重定向是 ...
- SpringMVC核心技术---转发和重定向
@Controller public class Mycontroller { //转发 @RequestMapping("/adduser") public String add ...
- SpringMVC框架——转发与重定向
网上摘取一段大神总结的转发与重定向的区别,如下: 转发(服务端行为) 形式:request.getRequestDispatcher().forward(request,response) 转发在服务 ...
- SpringMVC怎么样设定重定向和转发的?
(1)转发:在返回值前面加"forward:",譬如"forward:user.do?name=method4" (2)重定向:在返回值前面加"red ...
随机推荐
- .Net语言 APP开发平台——Smobiler学习日志:如何快速在手机上实现ContextMenu
最前面的话:Smobiler是一个在VS环境中使用.Net语言来开发APP的开发平台,也许比Xamarin更方便 样式一 一.目标样式 我们要实现上图中的效果,需要如下的操作: 1.从工具栏上的&qu ...
- Java程序员应该了解的10个面向对象设计原则
面向对象设计原则: 是OOPS(Object-Oriented Programming System,面向对象的程序设计系统)编程的核心,但大多数Java程序员追逐像Singleton.Decorat ...
- iOS之计算上次日期距离现在多久, 如 xx 小时前、xx 分钟前等
/** * 计算上次日期距离现在多久 * * @param lastTime 上次日期(需要和格式对应) * @param format1 上次日期格式 * @para ...
- [django]数据导出excel升级强化版(很强大!)
不多说了,原理采用xlwt导出excel文件,所谓的强化版指的是实现在网页上选择一定条件导出对应的数据 之前我的博文出过这类文章,但只是实现导出数据,这次左思右想,再加上网上的搜索,终于找出方法实现条 ...
- OpenGL shader 中关于顶点坐标值的思考
今天工作中需要做一个事情: 在shader内部做一些空间距离上的计算,而且需要对所有的点进行计算,符合条件的显示,不符合条件的点不显示. 思路很简单,在vertex shader内知道顶点坐标,进行计 ...
- hadoop 2.4 遇到的问题
不管出什么问题,首先查看日志. 在启动过hadoop的前提下,打开浏览器,输入http://localhost:50070 点击Utilities下的logs,选择hadoop-root-datano ...
- XSS 前端防火墙 —— 无懈可击的钩子
昨天尝试了一系列的可疑模块拦截试验,尽管最终的方案还存在着一些兼容性问题,但大体思路已经明确了: 静态模块:使用 MutationObserver 扫描. 动态模块:通过 API 钩子来拦截路径属性. ...
- 【腾讯Bugly干货分享】程序员们也该知道的事——“期权和股票”
本文来自于腾讯Bugly公众号(weixinBugly),未经作者同意,请勿转载,原文地址:https://mp.weixin.qq.com/s/pfj9NLLuKYAfJJF84R9WAw 作者:B ...
- Hadoop单机模式配置
Required Software 1. 安装Java环境推荐的版本在链接中有介绍HadoopJavaVersions. 2. 安装ssh以使用hadoop脚本管理远程Hadoop daemons. ...
- 解决CSharpGL使用CGCompiler时发现的几个问题
解决CSharpGL使用CGCompiler时发现的几个问题 为了获取CSharpShadingLanguage的token流,我设计了这样一个文法: <Expression> ::= & ...