SpringMVC中使用GetMapping等注解
在SpringMVC中使用REST风格
在springmvc中,使用REST风格有两种方式
第一种使用@RequestMapping注解
第二种使用@GetMapping等注解
第一种方式
@RequestMapping (value = “/地址”,method = RequestMethod.GET)
使用RequestMapping里面的参数,method,设置GET/POST等方
第二种方式
@GetMapping ("/地址")
这种方式等同于第一种方式,GetMapping等同于设置了method=RequestMethod.GET的RequestMapping
但是如果想要使用这种注解的话,需要在配置文件中开启注解驱动
<mvc:annotation-driven/>
但是form只能支持get还有post格式,所以需要配置过滤器
使用配置文件的方式
<filter>
<filter-name>hiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
<init-param>
<param-name>methodParam</param-name>
<param-value>_m</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>hiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
使用java代码的方式
@Configuration
public class AppConfig {
@Bean
public HiddenHttpMethodFilter filter(){
HiddenHttpMethodFilter hiddenHttpMethodFilter = new HiddenHttpMethodFilter();
hiddenHttpMethodFilter.setMethodParam("_m");
return hiddenHttpMethodFilter;
}
}
jsp页面
<form action="地址" method="post">
<input type="hidden" name="_m" value="DELETE"/>
<input type="text" name="name"/>
<input type="text" name="age"/>
<input type="submit"/>
</form>
其中_m是上面通过配置文件/java代码设置的,value设置的是请求方式,可以是DELETE/PUT
由于JSP页面没有DELETE/PUT方式,所有需要使用POST方式进行提交,然后经过过滤,找到_m,识别到value里面的DELETE就转换成了DELETE请求