SpringMVC常用以下几个注解:
1.@Controller
表明这个类是SpringMVC里的Controller,将其声明为Spring的一个bean,Dispatcher Servlet会自动扫描注解了此注解的类,并将web请求映射到注解了@RequestMapping的方法上。
2.@RequestMapping
SpringMVC的核心注解,负责访问的url与调用方法之间的映射;@RequestMapping可以放在类和方法上。注解在方法上的@RequestMapping路劲会继承注解在类上的路劲。@RequestMapping支持Servelt的request和response作为参数,也支持对request和response的媒体类型进行配置。
@RequestMapping的属性produces属性控制response返回的形式;如Content-Type:text/plain
; application/json等。
@RequestMapping的属性method属性控制接受访问的类型,不写不做限制
3.@ResponseBody
放在方法上或者返回值类型前,@ResponseBody支持将返回值放在response体内,而不是返回一个页面。我们在很多基于Ajax的程序的时候,可以以此注解返回数据而不是页面。
4.@RequestBody
放在方法参数前。@RequestBody允许请求的参数在request体内,而不是直接链接在地址后面。如下:
5.@PathVariable
@PathVariable用来接收路劲参数。如/news/001,可接收001作为参数,此注解放在参数前。
6.@RestController
这是个组合注解,组合了@Controller和@ResponseBody,这就意味着当你只开发一个和页面交互数据的控制器的时候,只需要使用此注解即可。
关于上面注解的使用,代码示例如下:
一:添加依赖(完整的pom)
4.0.0
com.flysun
springmvc4
0.0.1-SNAPSHOT
war
1.7
UTF-8
UTF-8
2.2
1.2
3.1.0
4.1.5.RELEASE
1.0.13
1.7.5
javax
javaee-web-api
7.0
provided
org.springframework
spring-webmvc
${spring-framework.version}
javax.servlet
jstl
${jstl.version}
javax.servlet
javax.servlet-api
${servlet.version}
provided
javax.servlet.jsp
jsp-api
${jsp.version}
provided
org.springframework
spring-tx
${spring-framework.version}
org.slf4j
slf4j-api
${slf4j.version}
log4j
log4j
1.2.16
org.slf4j
jcl-over-slf4j
${slf4j.version}
ch.qos.logback
logback-classic
${logback.version}
ch.qos.logback
logback-core
${logback.version}
ch.qos.logback
logback-access
${logback.version}
com.fasterxml.jackson.dataformat
jackson-dataformat-xml
2.5.3
commons-fileupload
commons-fileupload
1.3.1
commons-io
commons-io
2.3
org.springframework
spring-test
${spring-framework.version}
test
junit
junit
4.11
test
org.apache.maven.plugins
maven-compiler-plugin
2.3.2
${java.version}
org.apache.maven.plugins
maven-war-plugin
2.3
false
pom中有个对json和xml都支持的jackson-dataformat-xml依赖,实际开发中,我们主要支持json格式数据就可以了,可以直接使用下面的依赖:
com.fasterxml.jackson.core
jackson-databind
2.5.3
二:传值类Vo
public class DemoObj {
private Long id;
private String name;
//jackson对对象和json做转换时一定需要此空构造
public DemoObj() {
super();
}
public DemoObj(Long id, String name) {
super();
this.id = id;
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
三:控制器类(分@Controller和@RestController)
1.@Controller的控制器类
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.wisely.highlight_springmvc4.domain.DemoObj;
@Controller // 1声明此类为控制器
@RequestMapping("/anno") //2此类的访问路劲为/anno 根地址为http://localhost:8080/spring4/anno
public class DemoAnnoController {
//此方法未标注路劲,因此使用类级别的路劲/anno
@RequestMapping(produces = "text/plain;charset=UTF-8",method = RequestMethod.GET)//声明response的媒体类型和字符集
public @ResponseBody String index(HttpServletRequest request) { //参数可接收HttpServletRequest,当前也接收HttpServletResponse
//此处的@ResponseBody用在返回值前面
return "url:" + request.getRequestURL() + " can access";
}
//映射路劲http://.../anno/pathvar/123
@RequestMapping(value = "/pathvar/{str}", produces = "text/plain;charset=UTF-8")
public @ResponseBody String demoPathVar(@PathVariable String str, //接收路劲参数,参数名必须和{}中的保持一致
HttpServletRequest request) {
//上面的@PathVariable注解必须加上,否则str为空
return "url:" + request.getRequestURL() + " can access,str: " + str;
}
//接收路劲参数 映射路劲http://.../anno/requestParam?id=123
@RequestMapping(value = "/requestParam", produces = "text/plain;charset=UTF-8")
public @ResponseBody String passRequestParam(@RequestParam Long id,
HttpServletRequest request) {
//上面的@RequestParam可以加,也可以不加,都可以获得参数id的值
return "url:" + request.getRequestURL() + " can access,id: " + id;
}
//演示参数到对象 映射路劲http://.../anno/obj?id=123&name=flysun
@RequestMapping(value = "/obj", produces = "application/json;charset=UTF-8")
@ResponseBody //@ResponseBody也可以用在方法上,上面的例子用在返回值前
public String passObj(DemoObj obj, HttpServletRequest request) {
return "url:" + request.getRequestURL()
+ " can access, obj id: " + obj.getId()+" obj name:" + obj.getName();
}
//演示不同的路劲到相同的方法上 映射路劲http://.../anno/name1 或者http://.../anno/name2
@RequestMapping(value = { "/name1", "/name2" }, produces = "text/plain;charset=UTF-8")
public @ResponseBody String remove(HttpServletRequest request) {
return "url:" + request.getRequestURL() + " can access";
}
}
1.@RestController的控制器类
@RestController //声明是控制器,并且返回的数据不需要加@ResponseBody
@RequestMapping("/rest")
public class DemoRestController {
@RequestMapping(value = "/getjson",
produces={"application/json;charset=UTF-8"}) //返回的数据类型是json
public DemoObj getjson (@RequestBody DemoObj obj){
return new DemoObj(obj.getId()+1, obj.getName()+"yy");//直接返回对象,对象会自动转换为json
}
@RequestMapping(value = "/getxml",
produces={"application/xml;charset=UTF-8"})//返回的数据类型是xml
public DemoObj getxml(DemoObj obj){
return new DemoObj(obj.getId()+1, obj.getName()+"yy");//直接返回对象,对象会自动转换为xml
}
}