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 ...
随机推荐
- arcpy+PyQt+py2exe快速开发桌面端ArcGIS应用程序
前段时间有一个项目,大体是要做一个GIS数据处理工具. 一般的方法是基于ArcObjects来进行开发,因为我对ArcObjects不太熟悉,所以就思考有没有其他简单快速的方法来做. 在查看ArcGI ...
- 用SecureCRT连接虚拟机中的Linux系统(Ubuntu)
今天突然练习linux命令行的时候,想在window中联系linux命令行.经过一番dudu找到了一个不错的的工具(SecureCRT--意思安全)就是用SSH链接linux主机.推荐大家使用.毕竟w ...
- requests源码阅读学习笔记
0:此文并不想拆requests的功能,目的仅仅只是让自己以后写的代码更pythonic.可能会涉及到一部分requests的功能模块,但全看心情. 1.另一种类的初始化方式 class Reques ...
- Memcached简介
在Web服务开发中,服务端缓存是服务实现中所常常采用的一种提高服务性能的方法.其通过记录某部分计算结果来尝试避免再次执行得到该结果所需要的复杂计算,从而提高了服务的运行效率. 除了能够提高服务的运行效 ...
- webpack搭建前端一条龙服务
作为从grunt.gulp一路走来的老码农,一开始用webpack的时候我是很抗拒的.但由于核心库使用了vue,而webpack又是vue的最佳拍档(vue作者专门为其写了vue-loader),所以 ...
- Entity Framework 6 Recipes 2nd Edition(10-5)译 -> 在存储模型中使用自定义函数
10-5. 在存储模型中使用自定义函数 问题 想在模型中使用自定义函数,而不是存储过程. 解决方案 假设我们数据库里有成员(members)和他们已经发送的信息(messages) 关系数据表,如Fi ...
- 【MSP是什么】MSP认证之项目集与项目群的关系和区别
项目群和项目集都是一个意思,翻译时没有统一口径造成的.只要能与项目组合区别开就可以了. 项目集与项目群的区别,不在于那些项目自身,而在于管理者的思想,管理者对待项目的态度.项目集与项目群,首先都是多个 ...
- 读取xml数据装配到字典中
public Dictionary<string, string> GetXml() { Dictionary<string, string> dic = new Dictio ...
- Android之SQLite数据库篇
一.SQLite简介 Google为Andriod的较大的数据处理提供了SQLite,他在数据存储.管理.维护等各方面都相当出色,功能也非常的强大. 二.SQLite的特点 1.轻量级使用 SQLit ...
- 扎克伯格开发的家用AI: Jarvis
扎克伯格本周二在facebook发布了一篇文章,介绍自己利用个人时间开发的一套在自己家里使用的AI系统,并将它命名为Jarvis,对!就是电影钢铁侠里的AI助手Jarvis. 文章并没有讲细节的技术c ...