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 ...
随机推荐
- WPF中Grid实现网格,表格样式通用类
/// <summary> /// 给Grid添加边框线 /// </summary> /// <param name="grid"></ ...
- javascript arguments(转)
什么是arguments arguments 是是JavaScript里的一个内置对象,它很古怪,也经常被人所忽视,但实际上是很重要的.所有主要的js函数库都利用了arguments对象.所以agru ...
- FineBI:一个简单易用的自助BI工具
过去,有关企业数据分析的重担都压在IT部门,传统BI分析更多面向的是具有IT背景的人员.但随着业务分析需求的增加,很多公司都希望为业务用户提供自助分析服务,将分析工作落实到业务人员手中.但同时,分析工 ...
- RMS Server打开或关闭日志记录
原文: https://technet.microsoft.com/zh-cn/library/cc732758 在 Active Directory Rights Management Servic ...
- Linux.NET实战手记—自己动手改泥鳅(下)
在上回合中,我们不痛不痒的把小泥鳅的数据库从只能供在Windows下运行的Access数据库改为支持跨平台的MYSQL数据库,毫无营养的修改,本回合中,我们将把我们修改后得来的项目往Linux中部署. ...
- Oracle11g CentOS7安装记录
1. 操作系统环境.安装包准备 宿主机:Max OSX 10.10.5 虚拟机:Parallel Desktop 10.1.1 虚拟机操作系统:CentOS-7-x86_64-DVD-1511.iso ...
- Repository 仓储,你的归宿究竟在哪?(三)-SELECT 某某某。。。
写在前面 首先,本篇博文主要包含两个主题: 领域服务中使用仓储 SELECT 某某某(有点晕?请看下面.) 上一篇:Repository 仓储,你的归宿究竟在哪?(二)-这样的应用层代码,你能接受吗? ...
- IIS日志-网站运维的好帮手
对于一个需要长期维护的网站来说,如何让网站长久稳定运行是件很有意义的事情. 有些在开发阶段没有暴露的问题很有可能就在运维阶段出现了,这也是很正常的. 还有些时候,我们希望不断地优化网站,让网站更快速的 ...
- Eclipse安装SVN教程
第1种安装方法 下载SVN安装包.地址:subclipse.tigris.org/servlets/ProjectDocumentList?folderID=2240. 根据Eclipse版本进行下载 ...
- xp搭建IIS出现HTTP 500错误-2147467259 (0x80004005)
安装IIS后访问localhost页面, 提示The remote procedure call failed and did not execute,再刷新变为:-2147467259 (0x800 ...