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

SpringMVC使用注解完成 URL 和 Controller & 方法之间的映射细节

2014-12-04 20:06 716 查看
1. 使用 @RequestMapping 来进行 URL 和 类 及 方法之间的映射。

2. @RequestMapping 注解可以标识到类上面,也可以标识到方法上

1). 若类上面没有标识,则方法上面的直接相对于 WEB 应用的根目录
< a href ="helloworld?name=springMVC">Hello
SpringMVC </a>

2). 若类上面有 @RequestMapping 标识,则类上面的 @RequestMapping 的 / 为相对于 WEB 应用的根目录,而方法上的 / 相对于类上面的路径
@RequestMapping("/springmvc" )

@Controller
public class SpringMVCTest
{

@RequestMapping("/testRedirect" )
public String
testRedirect(){
System. out.println("testRedirect" );
return "redirect:/index.jsp" ;
}
< a href ="springmvc/testRedirect">Test
Redirect </a>

3. @RequestMapping 提供了足够精细的映射细节

URL,请求方式,是否包含什么参数,参数值是否等于已经定义好的值,包含请求头...

1). 映射 URL:使用 @RequestMapping 的 value 属性

2). 映射请求方式:使用 @RequestMapping 的 method 属性

示例:
@RequestMapping(value="/testRequestMapping2" ,
method=RequestMethod. POST,params={"name" ,"age=12" })
public String testRequestMapping2(){
System. out.println("testRequestMapping2" );
return SUCCESS ;
}
@RequestMapping("/testRequestHeader" )
public String testRequestHeader(@RequestHeader (value="Accept-Language" )
String al,
@RequestParam("age" ) int age, @CookieValue("JSESSIONID" )
String jid){
System. out.println("Accept-Language:
" + al + ", age: " + age + ", JSESSIONID: " + jid);
return SUCCESS ;
}
4.★在目标方法中如何得到请求参数 ?

1). 使用注解:@RequestParam 或 @PathVariable。
①. @RequestParam:用于映射请求参数。

@RequestParam(value="age", required=false) int age:把 age 这个请求参数赋给 age 这个入 参,且该请求参数不是必须的!

required 默认为 true,即若没有这个请求参数,则 SpringMVC 会抛出异常

@RequestMapping(value= "testRequestParam")
public String testRequestParam(@RequestParam (value="username" ,required=false)
String un){
System. out.println("username: " +
un);
return SUCCESS ;
}
②. @PathVariable:可以将 URL 中占位符参数绑定到控制器处理方法的入参中。

URL 中的 {xxx} 占位符可以通过 @PathVariable("xxx") 绑定到操作方法的入参中。


@RequestMapping( "/testRequestMappingPathVariable/{id}" )
public String testPathVariable( @PathVariable("id" )
Integer id){
System. out.println("testRequestMappingPathVariable:
" + id);
return SUCCESS ;
}
2). 直接使用类:直接在入参中使用自定义的类作为方法的入参。
适用于表单请求,把表单参数直接映射到对象的属性上

------------------------------Action------------------------------------------------------------
@RequestMapping("/testPojo")

public String testPojo(User user){
System. out.println("user: " + user);
return SUCCESS ;
}
---------------------------页面请求--------------------------------------------------
<form action= "springmvc/testPojo" method ="POST">
username: <input type= "text" name ="username"/>
<br>
age: <input type= "text" name ="age"/>
<br>
email: <input type= "text" name ="email"/>
<br>
<input type= "submit" value ="Test
Pojo"/>
</form >
---------------------------------POJO---------------------------------------------------
public class User
{
private Integer id;
private String username;
private String email;
private int age ;
5. 如果需要可以使用 ServletAPI:request, response, session 作为方法的入参
@RequestMapping("/testServletAPI" )
public String testServletAPI(HttpServletRequest request, HttpServletResponse
response,
HttpSession session, Reader reader) throws IOException{
System. out.println(request);
System. out.println(response);
System. out.println(session);

System. out.println(request.getReader() == reader); //true

return SUCCESS ;
}
6. 使用 IO 作为入参:Servlet 的 ServletRequest 拥有 getInputStream() 和 getReader() 的方法,可以通过它们读取请求信息。相应 Servlet 的 ServletResponse 的 getOuputStream() 和
getWriter() 方法,可以通过它们输出响应信息。

@RequestMapping("/testWriter" )
public void testWriter(Writer
out) throws IOException{
out.write( "Hello~");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐