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

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

2016-12-14 21:40 561 查看


1. 使用 @RequestMapping 来进行 URL 和 类 及 方法之间的映照。


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

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

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

[align=left]@Controller[/align]
public class SpringMVCTest
{
[align=left]
[/align]

               @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);
[align=left]           return SUCCESS ;[/align]
[align=left]     }[/align]

     
---------------------------页面要求--------------------------------------------------

     <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~");

     }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: