Spring3系列13-Controller和@RequestMapping

Controller返回值,String或者ModelAndView

@RequestMapping关联url

@RequestMapping的属性

一、      Controller返回值,String或者ModelAndView

首先看一下spring的配置文件,如下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="com.lei.demo.controller" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/user/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>

第一种,返回类型为String,Controller中的方法如下

    @RequestMapping(value="welcome",method=RequestMethod.GET)
public String printMessage(ModelMap model) { model.addAttribute("message", "返回类型String");
return "users";
}

根据spring配置文件和以上controller,访问“/welcome”时,对应的返回页面为“/WEB-INF/user/users.jsp”

第二种,返回类型为ModelAndView,Controller中的方法如下

    @RequestMapping("/welcome")
public ModelAndView printMessage (){
ModelAndView mv = new ModelAndView();
mv.addObject("message", "返回类型ModelAndView ");
mv.setViewName("users");
return mv;
}

两种方法返回的页面相同,不同的是第二种方法将model和view整合成ModelAndView实例,方法中不需要再加入model参数。

二、      @RequestMapping关联url

@RequestMapping可以是类级别和方法级别。

1.        类级别,类前有@RequestMapping

@Controller
@RequestMapping("/user")
public class UserController { //访问此方法的url为“/user/manager”,url前要加入类级别的路径/user
@RequestMapping(value="/manager",method=RequestMethod.GET)
public ModelAndView printMessage(){
ModelAndView mv = new ModelAndView();
mv.addObject("message", "MVC");
mv.setViewName("users");
return mv;
} //访问此方法的url为“/user/hello”,url前要加入类级别的路径/user
@RequestMapping(value="/hello",method=RequestMethod.GET)
public ModelAndView hello(){
ModelAndView mv = new ModelAndView();
mv.addObject("message", "hello");
mv.setViewName("users");
return mv;
}
}

2.        方法级别,类前没有@RequestMapping

@Controller
public class UserController { //没有类级别的@RequestMapping,访问此方法的url为“/manager”
@RequestMapping(value="/manager",method=RequestMethod.GET)
public ModelAndView printMessage(){
ModelAndView mv = new ModelAndView();
mv.addObject("message", "MVC");
mv.setViewName("users");
return mv;
} //没有类级别的@RequestMapping,访问此方法的url为“/hello”
@RequestMapping(value="/hello",method=RequestMethod.GET)
public ModelAndView hello(){
ModelAndView mv = new ModelAndView();
mv.addObject("message", "hello");
mv.setViewName("users");
return mv;
}
}

3.        @RequestMapping支持多个映射路径映射到同一个controller

    @RequestMapping(value={"/hello","/foo"})
public ModelAndView hello(){
ModelAndView mv = new ModelAndView();
mv.addObject("message", "hello");
mv.setViewName("users");
return mv;
}

以上“/hello”和“/foo”映射到同一个函数处理。

三、      @RequestMapping的属性

@RequestMapping有如下几个属性:value、method、params、headers

这几个属性用法如下

1.      @RequestMapping中的value属性

通过value属性,表达主要的映射,在Servlet环境中,映射路径(如,/myPath.do),也支持Any风格的(如,/myPath/*.do)。在方法级别中的相对路径需要类级别的主路径支持。

@RequestMapping("/user")等同于@RequestMapping(value="/user")

2.      @RequestMapping中的method属性

通过HTTP请求的method来缩小主映射的范围。GET, POST, HEAD, OPTIONS, PUT, DELETE, TRACE。支持定义在类级别或方法级别。

3.      @RequestMapping中的params属性

格式为“paramname=paramvalue” 或 “paramname!=paramvalue”。不带参数则表示paramvalue可以为任意值。

例如,params =  {"param1=1","param2!=2","param3"},表示对应的url必须包括param1,param2,param3三个参数,其中param1的值必须为1,param2的值不能为2,param3的值可以为任意值。

4.      @RequestMapping中的headers属性

headers用来限定对应的reqeust请求的headers中必须包括的内容,例如

headers={"Connection=keep-alive"}, 表示请求头中的connection的值必须为keep-alive。

Spring3系列13-Controller和@RequestMapping的更多相关文章

  1. 【#】Spring3 MVC 注解(一)---注解基本配置及@controller和 @RequestMapping 常用解释

    Spring3 MVC 注解(一)---注解基本配置及@controller和 @RequestMapping 常用解释 博客分类:  spring MVCSpringWebXMLBean  一:配置 ...

  2. Spring3系列7- 自动扫描组件或Bean

    Spring3系列7- 自动扫描组件或Bean 一.      Spring Auto Scanning Components —— 自动扫描组件 1.      Declares Component ...

  3. Spring框架系列(13) - SpringMVC实现原理之DispatcherServlet的初始化过程

    前文我们有了IOC的源码基础以及SpringMVC的基础,我们便可以进一步深入理解SpringMVC主要实现原理,包含DispatcherServlet的初始化过程和DispatcherServlet ...

  4. Spring3系列12- Spring AOP AspectJ

    Spring3系列12- Spring AOP AspectJ 本文讲述使用AspectJ框架实现Spring AOP. 再重复一下Spring AOP中的三个概念, Advice:向程序内部注入的代 ...

  5. Spring3系列11- Spring AOP——自动创建Proxy

    Spring3系列11- Spring AOP——自动创建Proxy 在<Spring3系列9- Spring AOP——Advice>和<Spring3系列10- Spring A ...

  6. Spring3系列10- Spring AOP——Pointcut,Advisor拦截指定方法

    Spring3系列10- Spring AOP——Pointcut,Advisor 上一篇的Spring AOP Advice例子中,Class(CustomerService)中的全部method都 ...

  7. Spring3系列9- Spring AOP——Advice

    Spring3系列9- Spring AOP——Advice Spring AOP即Aspect-oriented programming,面向切面编程,是作为面向对象编程的一种补充,专门用于处理系统 ...

  8. Spring3系列8- Spring 自动装配 Bean

    Spring3系列8- Spring 自动装配 Bean 1.      Auto-Wiring ‘no’ 2.      Auto-Wiring ‘byName’ 3.      Auto-Wiri ...

  9. Spring3系列6 - Spring 表达式语言(Spring EL)

    Spring3系列6-Spring 表达式语言(Spring EL) 本篇讲述了Spring Expression Language —— 即Spring3中功能丰富强大的表达式语言,简称SpEL.S ...

随机推荐

  1. Neo4j 2.0 M4 发布

    Neo4j 发布了 2.0 的第四个里程碑版本,该版本要求 Java 7 的支持.详细的改进记录请看发行通知. Neo是一个网络——面向网络的数据库——也就是说,它是一个嵌入式的.基于磁盘的.具备完全 ...

  2. .NET Core竟然无法在Mac下进行build

    KRuntime 改为 XRE 之后(详见从 KRE 到 XRE :ASP.NET 5 中正在消失的那些K),昨天在 mac 用 git 签出 XRE 的代码库,直接执行其中的 build 命令 sh ...

  3. [ucgui] 对话框6——触屏位置简单例子

    >_<:直接调用函数获得触屏位置: xPhys = GUI_TOUCH_GetxPhys(); /* Get the A/D mesurement result in x */ yPhys ...

  4. [ACM_模拟] ACM - Draw Something Cheat [n个长12的大写字母串,找出交集,按字母序输出]

    Description Have you played Draw Something? It's currently one of the hottest social drawing games o ...

  5. [matlab] 矩阵操作

    >_<:矩阵构造 1.简单矩阵构造 最简单的方法是采用矩阵构造符“[]”.构造1´n矩阵(行向量)时,可以将各元素依次放入矩阵构造符[]内,并且以空格或者逗号分隔:构造m´n矩阵时,每行如 ...

  6. JPA oneToMany 级联更新

    oneToMany 使用: 示例:Employee与Phone为例. 1.类定义如下: package com.vrvwh.wh01.domain; import javax.persistence. ...

  7. [BTS] System.Xml.Schema.XmlSchemaException: The complexType has already been declared when generate IDoc schema.

    I use wcf-sap adapter for generate the schema of IDoc that named "YHREMPMASTER". but throw ...

  8. httpwebrequest 请求压缩,接受压缩的字符流请求

    请看图,客户端和服务端都使用gzip压缩. 客户端参数以json字符串形式gzip压缩,以二进制流发送到服务端:服务端接收到请求,解压缩gzip后,进行业务逻辑处理:处理完后在将返回值以json形式, ...

  9. 仿Material UI框架的动画特效

    Material UI是一款功能非常强大,界面却十分清新简洁的CSS框架,Material UI利用了Google的Material Design 全新设计语言,并且让每一个UI组件都变得非常独立,因 ...

  10. js常规日期格式处理、月历渲染、倒计时函数

    日期格式处理在前端的日常任务中非常常见,但是为此引入monent.js这样的类库又会显得有点臃肿,毕竟我们一个特定的项目中,并不需要monent.js那么全的涵盖范围.另外,如果现在公司让你自己手写一 ...