首先看一下如何获得request对象、session对象

普通的Controller类,示例代码如下:

@Controller
@RequestMapping(value = "user")
public class UserController { @RequestMapping(value = "/reg2")
public String reg2(String uname,HttpServletRequest req,ModelMap map){
req.setAttribute("a", "aa");
req.getSession().setAttribute("b", "bb");
return "index";
}
}

ModelMap:

是map的实现,可以在其中存放属性,作用域同request。下面这个示例,我们可以在modelMap中放入数据,然后在forward的页面上显示这些数据。

通过el表达式、JSTL、java代码均可。

代码如下:

package com.sxt.web;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController; @Controller
@RequestMapping(value = "user")
public class UserController extends MultiActionController { @RequestMapping(value = "/reg")
public String reg(String uname,ModelMap map){
map.put("a", "aaa");
return "index";
}
}
JSP页面:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head></head>
<body>
<h1>${requestScope.a}</h1>
<c:out value="${requestScope.a}"></c:out>
</body>
</html>

请求转发和重定向:

package com.sxt.web;

import javax.annotation.Resource;
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.SessionAttributes; @Controller
@RequestMapping(value = "user")
public class UserController { @RequestMapping(value = "/reg4")
public String reg4(ModelMap map) {
System.out.println("HelloController.handleRequest()");
// return "forward:index.jsp";
// return "forward:user.do?method=reg5"; //转发
// return "redirect:user.do?method=reg5"; //重定向
return "redirect:http://www.baidu.com"; //重定向
} @RequestMapping(value = "/reg5")
public String reg5(@RequestParam("uname") String uname,ModelMap map) {
System.out.println("HelloController.handleRequest()");
System.out.println(uname);
return "index";
} }

这样如果是请求转发(forward)的话传值可以用request和modelMap传递。

Spring MVC 3.0 请求转发和重定向的更多相关文章

  1. Spring MVC中forward请求转发2种方式(带参数)

    Spring MVC中forward请求转发2种方式(带参数) http://www.51gjie.com/javaweb/956.html  

  2. spring mvc 请求转发和重定向(转)

    spring mvc controller间跳转 重定向 传参 url:http://zghbwjl.blog.163.com/blog/static/12033667220137795252845/ ...

  3. spring mvc 请求转发和重定向

    spring mvc controller间跳转 重定向 传参 url:http://zghbwjl.blog.163.com/blog/static/12033667220137795252845/ ...

  4. Spring MVC 3.0 深入及对注解的详细讲解

    核心原理 1.       用户发送请求给服务器.url:user.do 2.       服务器收到请求.发现Dispatchservlet可以处理.于是调用DispatchServlet. 3.  ...

  5. Spring MVC 3.0.5+Spring 3.0.5+MyBatis3.0.4全注解实例详解(四)

    这一章大象将详细分析web层代码,以及使用Spring MVC的注解及其用法和其它相关知识来实现控制器功能.     之前在使用Struts2实现MVC的注解时,是借助struts2-conventi ...

  6. Spring MVC 3.0 深入及对注解的详细讲解[转载]

    http://blog.csdn.net/jzhf2012/article/details/8463783 核心原理 1.       用户发送请求给服务器.url:user.do 2.       ...

  7. Spring MVC 3.0.5+Spring 3.0.5+MyBatis3.0.4全注解实例详解(二)

    在上一篇文章中我详细的介绍了如何搭建maven环境以及生成一个maven骨架的web项目,那么这章中我将讲述Spring MVC的流程结构,Spring MVC与Struts2的区别,以及例子中的一些 ...

  8. Spring MVC的映射请求

    一.SpringMVC常用注解 @Controller 声明Action组件 @Service    声明Service组件    @Service("myMovieLister" ...

  9. Spring MVC 异步处理请求,提高程序性能

    原文:http://blog.csdn.net/he90227/article/details/52262163 什么是异步模式 如何在Spring MVC中使用异步提高性能? 一个普通 Servle ...

随机推荐

  1. 【LeetCode】101 - Symmetric Tree

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  2. ASP.NET MVC Razor视图引擎攻略

    --引子 看下面一段MVC 2.0的代码. <%if (Model != null){%> <p><%=Model%></p><%}%>&l ...

  3. C++11 多线程

    C++11开始支持多线程编程,之前多线程编程都需要系统的支持,在不同的系统下创建线程需要不同的API如pthread_create(),Createthread(),beginthread()等,使用 ...

  4. Embedded之Introduction

    1 Embedded system An embedded system is a combination of computer hardware and software, and perhaps ...

  5. 60个响应式的Web设计教程–能够手机访问!

    想要学习响应式[responsive:屏幕自适应的效果]的网页设计和开发技术?在这个超大的收藏集合中,我想你定会找到想要开始学习的响应式网页设计教程. 面对超过1亿的手机互联网用户,开发专业和用户友好 ...

  6. [WebService]之JWS_1

    创建JWS项目步骤: 1:创建接口 2:创建实现类 3:开启服务 1:编写接口 @WebService public interface IMyService { public int add(int ...

  7. Hadoop概述

    本章内容 什么是Hadoop Hadoop项目及其结构 Hadoop的体系结构 Hadoop与分布式开发 Hadoop计算模型—MapReduce Hadoop的数据管理 小结 1.1 什么是Hado ...

  8. 'System.Web.Http.GlobalConfiguration' does not contain a definition for 'Configure'

    It needs the system.web.http.webhost which is part of this package. I fixed this by installing the f ...

  9. cocos中添加显示文字的三种方式(CCLabelTTF 、CCLabelBMFont 和CCLabelAtlas)

    CCLabelTTF CCLabelTTF 每次调用 setString (即改变文字)的时候,一个新的OPENGL 纹理将会被创建..这意味着setString 和创建一个新的标签一样慢. 这个类使 ...

  10. windows 无法启动asp.net 状态服务 错误 0x8007277a

    处理方法:如果在启动ASP.NET State Service服务时遇到问题0x8007277a即无法启动或初始化,请尝试在命令行(CMD)中输入netsh winsock  reset(有可能是wi ...