微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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 {

               /testRedirect )
     public String testRedirect(){
          System. out.println(testRedirect );
           return redirect:/index.jsp ;
      }

     →springmvc/testRedirect>Test Redirect >



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

     URL,要求方式,是不是包括甚么参数,参数值是不是等于已定义好的值,包括要求头...

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

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

     示例:
     @RequestMapping(value=/testRequestMapping2 ,
               method=RequestMethod. POST,params={name ,age=12 })
     public String testRequestMapping2(){
          Systemout.println(testRequestMapping2 );
           return SUCCESS ;
     }

     @RequestMapping(/testRequestHeader )
     public String testRequestHeader(@RequestHeader (value=Accept-Language ) String al,
               @RequestParam(age ) int age, @CookieValue(JSESSIONID ) String jid){
          SystemAccept-Language:  + al + ,age:  + age +            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){
          Systemusername:  + un);
           SUCCESS ;
     }


     ②. @PathVariable:可以将 URL 中占位符参数绑定到控制器处理方法的入参中。
       URL 中的 {xxx} 占位符可以通过 @PathVariable(xxx) 绑定到操作方法的入参中。
 
        @RequestMapping/testRequestMappingPathVariable/{id} )
         public String testPathVariable( @PathVariable(id ) Integer id){
          SystemtestRequestMappingPathVariable:  + id);
           SUCCESS ;
        }


2). 直接使用类:直接在入参中使用自定义的类作为方法的入参。
         适用于表单要求,把表单参数直接映照到对象的属性
------------------------------Action------------------------------------------------------------
           @RequestMapping(/testPojo)
     public String testPojo(User user){
          System. user:  + user);
           SUCCESS ;
     }
     
---------------------------页面要求--------------------------------------------------
     <form actionspringmvc/testPojo method =POST>
          username: <input typetext name =username/>
           br>
          age: age>
          email: email>
           submit value =Test Pojo/>
     form >
---------------------------------POJO---------------------------------------------------
public class User {
     private Integer id;
     private String username;
     email;
     private int age ;


5. 如果需要可使用 ServletAPI:request, response, session 作为方法的入参
/testServletAPI )
     public String testServletAPI(HttpServletRequest request,HttpServletResponse response,
              HttpSession session,Reader reader) throws IOException{
          Systemout.println(request);
          Systemout.println(response);
          Systemout.println(session);
          
          Systemout.println(request.getReader() == reader);  //true
          
           SUCCESS ;
     }

6. 使用 IO 作为入参:Servlet 的 ServletRequest 具有 getInputStream() 和 getReader() 的方法,可以通过它们读取要求信息。相应 Servlet 的 ServletResponse 的 getouputStream() 和 getWriter() 方法,可以通过它们输出响应信息。
/testWriter )
     void testWriter(Writer out) throws IOException{
          out.write( Hello~);
     }


版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐