@RequestMapping 是一个用来处理请求地址映射的注解,可用于类或方法上。
用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径;
用于方法上,表示在类的父路径下追加方法上注解中的地址将会访问到该方法,此处需注意@RequestMapping用在类上可以没用,但是用在方法上必须有。
=======================================================================
用在类和方法上:
package com.awaimai.web;
import org.hibernate.validator.constraints.*;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.Size;
import java.util.Enumeration;
@Controller
@Validated
@RequestMapping(value = "/abc")
public class kzq
{
@RequestMapping(value = "/test6")
@ResponseBody
public String test6()
{
return "123";
}
}
web访问:
![](https://i-blog.csdnimg.cn/blog_migrate/c83cdef5234b11fb253b17595ffc0cd4.png)
==============================================================================================
用在方法上:
package com.awaimai.web;
import org.hibernate.validator.constraints.*;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.Size;
import java.util.Enumeration;
@Controller
@Validated
public class kzq
{
@RequestMapping(value = "/test6")
@ResponseBody
public String test6()
{
return "123";
}
}
web访问:
![](https://i-blog.csdnimg.cn/blog_migrate/5a65044b850bab1b5762cba84cf1a70b.png)
====================================================================