1.web.xml需配置
<!-- 配置DispatcherServlet --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--
<init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param>
-->
<load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
2.springmvc-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="org.springframework.samples.petclinic.web"/> <!-- 配置自动扫描的包 --> <context:component-scan base-package="com.finn.web"/> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
1.@RequestMapping除了修饰方法,还可以来修饰类
类定义处:提供初步的请求映射信息。相对于WEB应用的根目录
方法处:若类定义处未标注@RequestMapping,则方法处标记的URL相对于WEB应用的根目录
2.@RequestMapping的value,method
@RequestMapping(value="/mappingmethod",method=RequestMethod.POST)
public String MappingPost(){
System.out.println("22222");
return SUCCESS;
}
@RequestMapping的params,headers
@RequestMapping(value="/mappingmethod",params={"username","age!=10"}) 必须包含username参数,并且age的值不等于10
3.@PathVaiable可以映射URL中的占位符到目标方法的参数中
@RequestMapping("/pathvaiable/{id}")
public String PathVariable(@PathVariable("id") Integer id){
System.out.println("PathVariable"+id);
return SUCCESS;
}
4.REST:资源表现层状态转化.PUT、DELETE、GET、POST
5.HiddenHttpMethodFilter
先配置到web.xml中,作用可以把POST请求转成PUT和 DELETE请求
<!-- 配置HiddenHttpMethodFilter -->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
控制器
@RequestMapping(value="/testRest/{id}",method=RequestMethod.DELETE)
public String testRest(@PathVariable("id") Integer id){
System.out.println("DELETE"+id);
return SUCCESS;
}
页面上
<form action="springmvc/testRest/1" method="post">
<input type="hidden" name="_method" value="DELETE"/>
<input type="submit" value="REST DELETE" />
</form>
浙公网安备 33010602011771号