使用@controller定义controllers
Spring mvc将特定url的请求分发到controller类来进行处理
在spring 3.0中,通过@controller标注即可将class定义为一个controller类。为使spring能找到定义为controller的bean,需要在spring-context配置文件中增加如下定义
<context:component-scan base-package="net.zhepu.web" />
使用@RequestMapping标注来关联url和controllers
通过@RequestMapping标注,可以将特定的url和具体的controller类或controller类中的方法绑定。如
@Controller
@RequestMapping("/helloworld")
public class Helloworld {
@RequestMapping(method=RequestMethod.GET)
public ModelAndView hello() {
ModelAndView mv = new ModelAndView();
mv.setViewName("helloworld");
return mv;
}
}
将/hellowrold和 Hellowrold这个controller类绑定,而在hello()方法前的@RequestMapping(method=RequestMethod.GET)则将hello这个方法和/hellorworld.action的get请求绑定起来。
定义multiaction controllers
使用一个controller来处理多个action,称为multiaction controller。@RequestMapping标注并不要求一定放在class定义之前,而可以直接作为一个method level的标注来使用,当这样使用时,contorller类就变成了一个multiaction controller,例如
@Controller
public class MultiactionHelloworld {
@RequestMapping("/hello1")
public ModelAndView hello1(){
ModelAndView mv = new ModelAndView();
mv.setViewName("helloworld");
return mv;
}
@RequestMapping("/hello2")
public ModelAndView hello2(){
ModelAndView mv = new ModelAndView();
mv.setViewName("helloworld");
return mv;
}
这里分别定义了两个方法hello1和hello2,对应的url为/hello1和/hello2
url template
@requestmapping参数中的url,除了常规的url外,也可以使用url template来定义形成类似REST请求的url。
例如
@RequestMapping("/urltemplate/{username}")
public ModelAndView test1(@PathVariable String username){
ModelAndView mv = new ModelAndView();
mv.addObject("userName", username);
System.out.println(username);
mv.setViewName("test002");
return mv;
}
以上通过@PathVariable将入参中的username和userid分别与请求url中的{username}和{userid}的值做绑定
@requestmapping的可选参数
value:表示需要匹配的url的格式。
method:表示所需处理请求的http 协议(如get,post,put,delete等),可选值为RequestMethod这个enum的值。
params:格式为”paramname=paramvalue” 或 “paramname!=paramvalue”。不带参数则表示paramvalue可以为任意值。
如params = {"param1=1","param2!=2","param3"},表示对应的url必须包括param1,param2,param3三个参数,其中param1的值必须为1,param2的值不能为2,param3的值可以为任意值。
headers:用来限定对应的reqeust请求的headers中必须包括的内容,例如
headers={"Connection=keep-alive"}, 表示请求头中的connection的值必须为keep-alive。
分享到:
相关推荐
Spring MVC--2.@RequestMapping 映射请求
基于注解的spring mvc,dao 层注解:@Repository("userDao"), entity层注解:@Entity,service层注解:@Service("userService"), action层注解:@Controller("userController") @RequestMapping("/user.do")
2.Spring MVC @RequestMapping 注解案例 3.Spring MVC 请求参数的获取案例 4.Spring MVC 域对象共享数据案例 5.Spring MVC @ModelAttribute 注解案例 6.Spring MVC 国际化案例 7.Spring MVC 请求转发与请求重定向...
本文通过分析Spring MVC的核心组件和执行流程,提供了一个全面的学习指南。 Spring MVC基于Model-View-Controller(MVC)架构模式,优化了Web应用程序的设计和开发。在Spring MVC中,DispatcherServlet作为前端控制...
SpringMVC不能只使用@Controller而不使用@RequestMapping
Spring mvc中@RequestMapping 6个基本用法小结
本篇文章主要介绍了Spring MVC之@RequestMapping 详解,RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。有兴趣的可以了解一下。
前段时间项目中用到了REST风格来开发程序,但是当用POST、PUT模式提交数据时,发现服务器端接受不到提交的数据(服务器端参数绑定没有加任何注解),查看了提交方式为application/json, 而且服务器端通过request....
弹簧 @Controller @RequestMapping @RequestParam,模型和ModelAndView Spring MVC实践
Spring Web model-view-controller (MVC)框架是围绕 DispatcherServlet 设计的,并分发请求到处理程序(handler),Spring MVC支持可配置的处理程序映射(handler mapping),视图解析(view resolution)、 区域设置...
工具类数据校验 jsp自定义标签 Spring自定义注解 默认requestMapping 1.1.2 代码生成器 1.1.3 首页修改 dateformat.js 时间参数转换 SpringMVC配置文件集中 快递参数接口 1.1.4 des加解密字符串和文件 1.1.5 redis...
spring-mvc与xfire的集成 对于这样的集成,看看里面的配置文件就行了 一个简单的接口,对应一个简单的实现。 然后在配置文件里指明就可以了。 DispatcherServlet本身会管理xfire的请求 配置文件中配置的key可以理解...
这边基于s的注释@RequestMapping、@Controller方式mvc实现,通过简单helloworld例子完整全面的将整个配置过程及相关细节,了解整个流程
本篇文章主要介绍了详解获取Spring MVC中所有RequestMapping以及对应方法和参数,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
本教程完整地讲解了Spring MVC的每个知识点,包括独立运行的Demo示例,欢迎一起交流学习。Java学习交流群:239126529 教程软件版本说明: 1)Spring MVC框架:spring-framework-5.0.2.RELEASE 2)开发工具:IntelliJ...
基于SpringMVC,实体的Controller只要继承该类即刻拥有增删改查的的方法。 比如: @Controller @RequestMapping("/role") RoleController extends CrudController<Role> 只要简单的继承就可以通过/role/save.do...
The Spring Web model-view-controller (MVC) framework is designed around a DispatcherServlet that dispatches requests to handlers, with configurable handler mappings, view resolution, locale and theme ...