1. 练习接收页面参数值
    1. 使用request
    2. 使用@RequestParam注解
    3. 使用实体对象
  2. 练习向页面传出数据
    1. 使用HttpServletRequest和session
    2. 使用ModelAndView对象  (内部为利用HttpServletRequest的Attribute传递数据到页面)
    3. 使用ModelMap对象 (内部为利用HttpServletRequest的Attribute传递数据到页面)
    4. 使用@ModelAttribute注解 (内部为利用HttpServletRequest的Attribute传递数据到页面)
  3. 练习使用session
    1. 在Controller方法参数上直接声明HttpSession即可使用
  4. 练习重定向
    1. 使用RedirectView
    2. 使用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传值、转发、重定向例子的更多相关文章

  1. SpringMVC之转发重定向

    package com.tz.controller; import org.springframework.stereotype.Controller; import org.springframew ...

  2. SSM-SpringMVC-22:SpringMVC中转发(forward)和重定向(redirect)

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 转发和重定向大家都熟悉,都学到框架了,怎么能不了解转发和重定向呢? 如果有不熟悉的,可以去百度搜几篇博客去看看 ...

  3. 04springMVC结构,mvc模式,spring-mvc流程,spring-mvc的第一个例子,三种handlerMapping,几种控制器,springmvc基于注解的开发,文件上传,拦截器,s

     1. Spring-mvc介绍 1.1市面上流行的框架 Struts2(比较多) Springmvc(比较多而且属于上升的趋势) Struts1(即将被淘汰) 其他 1.2  spring-mv ...

  4. JavaWeb_day04搜索_乱码_路径_转发重定向_cookie

    本文为博主辛苦总结,希望自己以后返回来看的时候理解更深刻,也希望可以起到帮助初学者的作用. 转载请注明 出自 : luogg的博客园 谢谢配合! 搜索功能 DAO层都是一些数据库的增删改查操作 Ser ...

  5. Python的Asyncore异步Socket模块及实现端口转发的例子

    Python的Asyncore异步Socket模块及实现端口转发的例子 Asyncore模块提供了以异步的方式写入套接字服务客户端和服务器的基础结构. 只有两种方式使一个程序在单处理器上实现" ...

  6. SpringMVC中的重定向和转发的实现

    1.请求转发和重定向的区别 请求重定向和请求转发都是web开发中资源跳转的方式. 请求转发是服务器内部的跳转 地址栏比发生变化 只有一个请求相应 可以通过request域对跳转目标的请求 请求重定向是 ...

  7. SpringMVC核心技术---转发和重定向

    @Controller public class Mycontroller { //转发 @RequestMapping("/adduser") public String add ...

  8. SpringMVC框架——转发与重定向

    网上摘取一段大神总结的转发与重定向的区别,如下: 转发(服务端行为) 形式:request.getRequestDispatcher().forward(request,response) 转发在服务 ...

  9. SpringMVC怎么样设定重定向和转发的?

    (1)转发:在返回值前面加"forward:",譬如"forward:user.do?name=method4" (2)重定向:在返回值前面加"red ...

随机推荐

  1. 从display:run-in;中学习新技能

    有时我们想在一行内显示一个标题,以及一段内容,虽然看起来比较简单,但是为了语义化用dl比较合适,但是它默认是block元素,改成inline?那么有多段呢?不就都跑上来了?用float?那问题也挺多. ...

  2. CRM 数据密钥 忘记 解决方案

    UPDATE EmailServerProfile SET IncomingPassword=nullUPDATE EmailServerProfile SET OutgoingPassword=nu ...

  3. SQL-日期函数

    GETDATE() :取得当前日期时间 DATEADD (datepart , number, date ),计算增加以后的日期.参数date为待计算的日期:参数number为增量:参数datepar ...

  4. date命令

    GNU的date提供+%s(小写s), 能打印出自1970-01-01 00:00:00到当前时间的秒数. 这可能大家都不陌生,但有两点需要注意: 1. %s存在于GNU扩展版本.像在solaris等 ...

  5. IM 去中心化概念模型与架构设计

    今天打算写写关于 IM 去中心化涉及的架构模型变化和设计思路,去中心化的概念就是说用户的访问不是集中在一个数据中心,这里的去中心是针对数据中心而言的. 站在这个角度而言,实际上并非所有的业务都能做去中 ...

  6. Linux 中的数值计算和符号计算

    不知道经常需要做科学计算的朋友们有没有这样的好奇:在 Linux 系统下使用什么工具呢?说到科学计算,首先想到的肯定是 Matlab,如果再说到符号计算,那就非 Mathematica 不可了.可惜, ...

  7. [Intel Edison开发板] 03、Edison开发IDE入门及跑官方提供的DEMO

    一.启动Eclipse爱迪生开发板IDE eclipse开发环境在iss-iot-win_03-14-16中,但是一定每次都是点bat脚本启动,否则就会少东西(windows->preferen ...

  8. [翻译]AKKA笔记 - DEATHWATCH -7

    当我们说Actor生命周期的时候,我们能看到Actor能被很多种方式停掉(用ActorSystem.stop或ActorContext.stop或发送一个PoisonPill - 也有一个kill和g ...

  9. selenium自动化基础知识

    什么是自动化测试? 自动化测试分为:功能自动化和性能自动化 功能自动化即使用计算机通过编码的方式来替代手工测试,完成一些重复性比较高的测试,解放测试人员的测试压力.同时,如果系统有不份模块更改后,只要 ...

  10. 【.net 深呼吸】自定义缓存配置(非Web项目)

    在前一篇烂文中,老周简单讲述了非Web应用的缓存技术的基本用法.其实嘛,使用系统默认方案已经满足我们的需求了,不过,如果你真想自己来配置缓存,也是可以的. 缓存的自定义配置可以有两种方案,一种是用代码 ...