@RequestMapping 用法

1:SpringMVC中,@RequestMapping用来处理请求,比如XXX.do

@RequestMapping("/aaa")//类级别,可以不需要,如果要了,下面所有的请求路径前都需要加入  /aaa  
 public class ccccontroller{  
  
      @RequestMapping("/bbb")//方法级别,必须有,决定这个方法处理哪个请求,如果有类级别 /aaa/bbb  
  
      public String xxx()  
      {  
            //如果没有类级别的就直接请求/bbb  
            return;  
      }  
}  
View Code

2:接收带参数的请求,接收用户请求参数 值 请求1: /test/start.do?name=zhangsan

请求2: /test/start/zhangsan.do

在请求2中 将参数作为请求URL传递,感觉用的不习惯   采用 URL模板 @RequestMapping("/start/{name}")//这个name  随便  啥都可以

 public String start(@PathVariable("name") string name){       //和上面的对应  
     return ;//方法体里面就可以直接获得参数  
} 
View Code

包含多个  @RequestMapping (value="/departments/{departmentId}/employees/{employeeId}") 

@RequestMapping(value="/departments/{departmentId}/employees/{employeeId}")  

public String findEmployee(  

  @PathVariable String departmentId,  

  @PathVariable String employeeId){  

  

    System.out.println("Find employee with ID: " + employeeId +   

      " from department: " + departmentId);  

    return "someResult";  

  

}  
View Code

3:不同请求方法,用不同处理方法   get  post    @RequestMapping (value="/start" ,method=RequestMethod.GET)//处理post 就换成 POST

另外,如果Servlet想要做到/test/start/zhangsan.do这种请求的话 需要 URL 重写才能做到,或者用 /start/* 作为 Servlet 请求控制器,在其中进行截取和判断。

  第二种方法:自己写一个 URL 重写的 Filter

第三种方法:使用现成的 urlrewriter 工具 

第四种方法:使用 Web 服务器的 URL 重写功能 Servlet中用Filter重写示例  web.xml中:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
    xmlns=" http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation=" http://java.sun.com/xml/ns/j2ee 
     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <servlet>
    <servlet-name>Action</servlet-name>
    <servlet-class>com.bao.servlet.Action</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Action</servlet-name>
    <url-pattern>/Action</url-pattern>
  </servlet-mapping>
  
  <filter>
    <filter-name>action_name</filter-name>
    <filter-class>com.bao.filter.ActionNameFilter</filter-class>
    <init-param>
      <param-name>pattern</param-name>
      <param-value>/action/[^/]+</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>action_name</filter-name>
    <url-pattern>/action/*</url-pattern>
  </filter-mapping>
</web-app>
View Code

Filter中:

private Matcher matcher;

Matcher 不是线程安全的,所以这样写是错误的。改正一下:

package com.bao.filter;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

public class ActionNameFilter implements Filter {

    private Pattern pattern;

    public void init(FilterConfig config) throws ServletException {
        String p = config.getInitParameter("pattern");
        pattern= Pattern.compile(p);
    }

    public void doFilter(ServletRequest req, ServletResponse res,
            FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest)req;
        String context = request.getContextPath();
        String uri = request.getRequestURI();
        // 去掉 URI 中的 context path
        String path = uri.substring(context.length());
        if(pattern.matcher(path).matches()) {
            // 如果 URI path 符合配置中的模式,则重新转发
            int idx = uri.lastIndexOf("/");
            String name = uri.substring(idx + 1);
            request.getRequestDispatcher("/Action?username=" + name).forward(req, res);
        } else {
            // 不符合的话做该做的事情
            chain.doFilter(req, res);
        }
    }

    public void destroy() {
        
    }
}
View Code

参考文章:http://heisetoufa.iteye.com/blog/1042046

       http://www.2cto.com/kf/201302/189407.html

在阅读的时候文章提到REST风格,好像是有关URL方面的。好奇搜索了下找到篇不错的文章:

设计 REST 风格的 MVC 框架:http://www.ibm.com/developerworks/cn/java/j-lo-restmvc/ 

从头看到尾,有时间在细看。

原文地址:https://www.cnblogs.com/kentyouyou/p/3578633.html