您的位置:首页 > 编程语言 > Java开发

【framework】spring3-mvc实例-redirect(请求转发)

2012-08-30 23:41 525 查看
前言

上篇文章为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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: