【framework】spring3-mvc实例-redirect(请求转发)
2012-08-30 23:41
525 查看
前言
上篇文章为spring-mvc做了个开篇,并且讲解了第一个例子Sample. 这篇文章将简单说一下请求转发,并且简单解释部分注解
相关信息
该项目例子是spirng-mvc-showcase的RedirectController类,位于org.springframework.samples.mvc.redirect 包内
类信息
解读
这个例子主要讲解了通过注解@RequestMapping的一些请求转发工作。
a. @RequestMapping
@RequestMapping不仅用于类,还可以用于方法,并且是一种增量关系。
1. uriTemplate()方法实际被请求URI为/redirect/uriTemplate
2. uriComponentsBuilder() 方法实际被请求URI为 /redirect/uriComponentsBuilder
3. show()方法可以在这里用于接收其他两个方法的请求,并且最终跳转到redirectResults.jsp
b. 两种请求方式
uriTemplate()和uriComponentsBuilder()分别展示了两种请求方式,主要用到了RedirectAttributes 和 UriComponentsBuilder
关注几个注解
1. @Inject
这个注解在这里功能是@Autowired一样的,@Autowired是spring的注解,而@Inject是JSR-330的注解,又叫java的标准注解(standard annotations)。
注:如果用maven,只需加入如下配置:
或者
自行下载,地址如下:http://repo1.maven.org/maven2/javax/inject/javax.inject/1/ 点击javax.inject-1.jar 即可
对比spring与标准注解JSR-330
#代表不存在,或者不需说明。
2. @PathVariable
在SpringMVC中,用@PathVariable注解在一个方法的参数上用于绑定这个参数到URI模板值。
看例子中的show()方法(注意这里的{account}不是SPEL),那为何{account}能够作为被请求的URI? 答案便是@PathVariable注解。 而@RequestParam则是用于 查询参数(queryParam)
3. @RequestParam
绑定请求参数Controller的方法参数
3.1 相关注解
除了@PathVariable和@RequestParam,相关注解还有:
@RequestHeader
@RequestBody
@RequestPart
HttpEntity<?> 这个简单描述一下:用于访问Servlet请求的HTTP头和content.
收尾
我相信,通过这篇文章,你对spring-mvc的请求转发,以及相关注解有了一定认识。修行在个人了,呵呵,这些确实比较简单,希望大家自己也动手熟悉一下。
本文出自 “书生” 博客,请务必保留此出处http://yjplxq.blog.51cto.com/4081353/977996
上篇文章为spring-mvc做了个开篇,并且讲解了第一个例子Sample. 这篇文章将简单说一下请求转发,并且简单解释部分注解
相关信息
该项目例子是spirng-mvc-showcase的RedirectController类,位于org.springframework.samples.mvc.redirect 包内
类信息
@Controller @RequestMapping("/redirect") public class RedirectController { private final ConversionService conversionService; @Inject public RedirectController(ConversionService conversionService) { this.conversionService = conversionService; } //--完整路径为/redirect/uriTemplate @RequestMapping(value="/uriTemplate", method=RequestMethod.GET) public String uriTemplate(RedirectAttributes redirectAttrs) { redirectAttrs.addAttribute("account", "a123"); //--用作URI的模板参数 即{account} redirectAttrs.addAttribute("date", new LocalDate(2011, 12, 31)); // 追加的查询参数(queryParam) return "redirect:/redirect/{account}"; } //--完成路径为/redirect/uriComponentsBuilder @RequestMapping(value="/uriComponentsBuilder", method=RequestMethod.GET) public String uriComponentsBuilder() { String date = this.conversionService.convert(new LocalDate(2011, 12, 31), String.class); //UriComponentsBuilder 是用于构建URI的 builder UriComponents redirectUri = UriComponentsBuilder.fromPath("/redirect/{account}").queryParam("date", date) .build().expand("a123")//--这里代替了show方法的{account} .encode(); return "redirect:" + redirectUri.toUriString(); } @RequestMapping(value="/{account}", method=RequestMethod.GET) public String show(@PathVariable String account, @RequestParam(required=false) LocalDate date) { return "redirect/redirectResults";//--跳转到/redirect/redirectResults.jsp } }
解读
这个例子主要讲解了通过注解@RequestMapping的一些请求转发工作。
a. @RequestMapping
@RequestMapping不仅用于类,还可以用于方法,并且是一种增量关系。
1. uriTemplate()方法实际被请求URI为/redirect/uriTemplate
2. uriComponentsBuilder() 方法实际被请求URI为 /redirect/uriComponentsBuilder
3. show()方法可以在这里用于接收其他两个方法的请求,并且最终跳转到redirectResults.jsp
b. 两种请求方式
uriTemplate()和uriComponentsBuilder()分别展示了两种请求方式,主要用到了RedirectAttributes 和 UriComponentsBuilder
关注几个注解
1. @Inject
这个注解在这里功能是@Autowired一样的,@Autowired是spring的注解,而@Inject是JSR-330的注解,又叫java的标准注解(standard annotations)。
注:如果用maven,只需加入如下配置:
<dependency> <groupId>javax.inject</groupId> <artifactId>javax.inject</artifactId> <version>1</version> </dependency>
或者
自行下载,地址如下:http://repo1.maven.org/maven2/javax/inject/javax.inject/1/ 点击javax.inject-1.jar 即可
对比spring与标准注解JSR-330
Spring | javax.inject.* | javax.inject 限制/说明 |
@Autowired | @Inject | @Inject has no 'required' attribute |
@Component | @Named | # |
@Scope("singleton") | @Singleton | The JSR-330 default scope is like Spring's prototype. However, in order to keep it consistent with Spring's general defaults, a JSR-330 bean declared in the Spring container is a singleton by default. In order to use a scope other than singleton, you should use Spring's @Scope annotation. javax.inject also provides a @Scope annotation. Nevertheless, this one is only intended to be used for creating your own annotations. |
@Qualifier | @Named | # |
@Value | # | 无等效注解 |
@Required | # | 无等效注解 |
@Lazy | # | 无等效注解 |
2. @PathVariable
在SpringMVC中,用@PathVariable注解在一个方法的参数上用于绑定这个参数到URI模板值。
看例子中的show()方法(注意这里的{account}不是SPEL),那为何{account}能够作为被请求的URI? 答案便是@PathVariable注解。 而@RequestParam则是用于 查询参数(queryParam)
3. @RequestParam
绑定请求参数Controller的方法参数
3.1 相关注解
除了@PathVariable和@RequestParam,相关注解还有:
@RequestHeader
@RequestBody
@RequestPart
HttpEntity<?> 这个简单描述一下:用于访问Servlet请求的HTTP头和content.
收尾
我相信,通过这篇文章,你对spring-mvc的请求转发,以及相关注解有了一定认识。修行在个人了,呵呵,这些确实比较简单,希望大家自己也动手熟悉一下。
本文出自 “书生” 博客,请务必保留此出处http://yjplxq.blog.51cto.com/4081353/977996
相关文章推荐
- 【framework】spring3-mvc实例-信息转换
- 【framework】spring3-mvc实例-标准参数
- org.springframework.web.servlet.mvc.support.RedirectAttributes
- MyBatis代码实例系列-05:Mybatis初步整合Spring + Spring MVC框架,实现Web请求实例
- 27、SSH框架-Ajax+SpringMVC+Spring+Mybatis+MySql+js用户注册实例(2)
- SpringMVC return forward redirect疑问
- SpringBoot的DeferredResult实例:二、另一个请求控制请求的返回时机
- jQuery中Ajax+Spring MVC实现跨域请求
- spring MVC return "redirect:test.do" 传参数
- Spring Web MVC的请求处理流程
- spring mvc controller接收请求值及controller之间跳转及传值
- SpringMVC JQuery Ajax Get Post请求在Tomcat中乱码解决方案
- Spring+SpringMVC+mybatis+easyui整合实例-----easyUI界面部分
- Spring mvc Controller 处理安卓post和get请求
- 如何让 Spring MVC Controller 的同一个 URL 请求,根据逻辑判断返回 JSON 或者 HTML 视图?
- Spring3 MVC请求参数获取的几种方法
- Spring-MVC入门(一):入门实例 (重点学习)
- dubbo+zookeeper+springmvc搭建实例教程
- Spring4 MVC文件下载实例
- SpringMVC RestTemplate的几种请求调用