文章目录
基本配置
-
jar
spring常规包
spring-web
spring-mvc -
配置文件
spring-mvc自带一个servlet,用它拦截所有请求
springMVC.xml(默认位置:WEB-INF/< servlet-name>的值-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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="controller"/>
<!-- 配置视图解析器:InternalResourceViewResolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 给controller返回值加上前缀 -->
<property name="prefix" value="/view/"/>
<!-- 后缀 -->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
web.xml
<!-- 配置spring-mvc的servlet:DispatcherServlet -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- 配置SpringMVC.xml位置 不配置即为默认位置 -->
<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>springDispatcherServlet</servlet-name>
<!-- 所有jsp请求为.do结尾的都拦截 -->
<url-pattern>*.do</url-pattern>
<!-- 也可用 / 拦截一切请求 -->
</servlet-mapping>
controller
@Controller
/*
类前有RequestMapping jsp: springMvc/welcome.do
没有 jsp:welcome.do
*/
@RequestMapping("springMvc")
public class SpringMVCController {
@RequestMapping("welcome.do")//跟jsp的请求一样
public String welcome(){
//加上上面配置的前缀和后缀,即访问/view/success.jsp
return "success";
}
}
@RequestMapping
/**
* method:只拦截 post请求
* params分别只拦截: 有name参数的,没有Sex参数的,age参数=11的,没有father参数或者father参数!=zs的
* headers为请求头 谷歌 Network中可看各种请求的请求头 cookie等
*/
@RequestMapping(
value = "springMvc",
method = RequestMethod.POST ,
params = {"name" , "!Sex" ,"age=11","father!=zs"},
headers = {"Accept =text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3"}
)
)
ant风格的请求路径
ant风格的请求路径
?单字符 welcome/a?c/test
* 任意个字符(0或多个字符,但不能多目录)welcome/*/test
** 任意目录(可多目录)welcome/**/test
注解取值(例子)
@PathVariable取值:
@RequestMapping("welcome.do/{name}")//jsp: welcome.do/张三
public String welcome(@PathVariable("name") String name){
return "success";
}
REST风格(普通浏览器设置put/delete)
GET:查
POST:增
PUT:改
DELETE:删
- jsp
<!-- type和name为固定值,value二选一(必须大写),请求方式为post -->
< input type ="hidden" name="_method" value="DELETE/PUT"/>
- web.xml
<filter>
<!-- spring-mvc自带的拦截器 HiddenHttpMethodFilter ,由于某些浏览器不支持delete和put -->
<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>
@RequestParam取值
@RequestMapping("welcome.do")
//第二个:前端有收前端,前端没有默认23
public String welcome3(@RequestParam("name") String name , @RequestParam(value = "age",required = false,defaultValue = "23")int age){
return "success";
}
@RequestHeader取头信息
@RequestMapping("welcome2.do")
public String welcome2(@RequestHeader("Accept-Language")String al){
return "success";
}
@CookieValue取cookie值
@RequestMapping("testCookie.do")
public String testCookie(@CookieValue("JSESSIONID")String jsessionID){
return "success";
}
实体类取值
控制器:
/**
* 1. jsp处 input的name 需要跟 student的属性名 一模一样
* 2. 支持级联设置,即address.schoolAddress
* 3. 可以用原生态servlet api的对象
*/
@RequestMapping("student.do")
public String getStudent(String name,Student student, HttpServletRequest request , HttpServletResponse response){
return "success";
}
jsp:
```bash
<form action="springMvc/student.do" method="post">
<!-- 此处name要跟属性名一样 -->
id:<input name="id" type="text" class="myStyle">
name:<input name="name" type="text" class="myStyle">
homeAddress:<input name="address.homeAddress" type="text" class="myStyle">
schoolAddress:<input name="address.schoolAddress" type="text" class="myStyle">
<input type="submit" value="提交">
</form>