SpringMVC(一)
第一个SpringMVC程序:helloworld
- 步骤
1、加入jar包
2、在web.xml中配置DispatcherServlet
3、加入SpringMVC的配置文件
4、编写处理请求的处理器(C),并标识为处理器
5、编写视图
1、jar包(复制到lib文件夹):
-commons-logging-1.1.3.jar
-spring-aop-4.0.0.RELEASE.jar
-spring-beans-4.0.0.RELEASE.jar
-spring-context-4.0.0.RELEASE.jar
-spring-core-4.0.0.RELEASE.jar
-spring-expression-4.0.0.RELEASE.jar
-spring-web-4.0.0.RELEASE.jar
-spring-webmvc-4.0.0.RELEASE.jar
2、配置DispatcherServlet
-下载并安装SpringIDE在web.xml文件下输入<,然后再删除掉,可以在提示栏中找到dispatcherServlet ,回车即可快速补全。(快速配置)
<!-- 配置DispatcherServlet -->
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置DispatcherServlet的一个初始化参数;配置SpringMVC配置文件的位置和名称 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<!-- 将请求给到servlet进行处理 -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<!-- 应答自由请求 “/”表示应答所有请求-->
<url-pattern>/</url-pattern>
</servlet-mapping>
注意:实际上也可以不通过contextConfigLocation来配置 SpringMVC配置文件,而使用默认的。
默认的配置文件为:/WEB-INF/<servlet-name>-servlet.xml
例:springDispatcherServlet-servlet.xml
3、创建springmvc.xml文件
-src中新建一个Spring Bean Configuration File,并选择命名空间:beans、context、mvc
第一个SpringMVC程序:HelloWorld
1、新建一个handler类(@Controller 注解为控制器)
通过RequesrMapping映射请求
package com.gh.springmvc.handlers;
@Controller
public class HelloWorld {
/**
* 1、使用 @RequestMapping 注解来映射请求的URL
* 2、返回值会通过视图解析器解析为实际的物理视图,对于InternalResourceViewResolver视图解析器,会做如下的解析
* 通过 prefix + returnVal + 后缀 这样的方式得到实际的物理视图,然后做转发操作
* @return
*/
@RequestMapping("/helloworld") //映射请求
public String hello() {
System.out.println("hello world!!");
return "success";
}
}
spirngmvc.xml文件配置:
-配置指定扫描的包
-配置制图解析器
<!-- 配置自动扫描的包 -->
<context:component-scan base-package="com.gh.springmvc.handlers"></context:component-scan>
<!--配置视图解析器:如何把 handler 方法返回值解析为实际的物理视图 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/views/"></property>
<!-- 后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
建立发送请求的页面 index.jsp:
<!-- 发送请求的页面 -->
<body>
<a href="helloworldoos">Hello World!</a>
</body>
建立一个success.jsp文件,用于处理请求后显示页面
<body>
<h4>Success Page</h4>
</body>
@RequestMapping注解
使用@RequestMapping映射请求
-SpringMVC使用@RequestMapping注解为控制器指定可以处理哪些URL请求
-在控制器的类定义及方法定义出都可标注
@RequestMapping
-类定义处:提供初步的请求映射信息。相对于WEB应用的根目录
-方法处:提供进一步的细分映射信息。相对于类定义处的URL,若类定义处未标注@RequestMapping,则方法处标记的URL相对于WEB应用的根目录
- DispatcherServlet截获请求后,就通过控制器上@RequestMapping提供的映射信息确定请求所对应的处理方法。
当类、方法均有@RequestMapping注解时:
@RequestMapping("/springmvc") //限定了处理器类可以处理所有URL为/springmvc的请求,它相对于WEB容器部署的跟目录
@Controller
public class HelloWorld {
@RequestMapping("/helloworld") //处理器可以定义多个处理方法,处理来自/springmvc下的请求
public String hello() {
System.out.println("hello world!!");
return "success";
}
}
则链接请求处的写法为:
<a href="springmvc/helloworld">Hello World!</a>
- 映射请求参数、请求方法或请求头
-@RequestMapping除了可以使用请求URL映射请求外,还可以使用请求方法、请求参数及请求头映射请求
-@RequestMapping的value、method、params及heads分别表示请求URL、请求方法、请求参数及请求头的映射条件,他们之间是与的关系,联合使用多个条件可让请求映射更加精确化。
-params和headers支持简单的表达式:
param1:表示请求必须包含名为param1的请求参数
!param1:表示请求不能包含名为param1的请求参数
param1 !=value1:表示请求包含名为param1的请求参数,但其值不能为value1
{“param1 =value1”,“param2”}:请求必须包含名为param1和param2的两个请求参数,且param1参数的值必须为value1
指定请求方式例子(常用):
@RequestMapping("/springmvc")
@Controller
public class SpringMVCTest {
private static final String SUCCESS = "success";
/**
* 常用:使用method属性指定请求方式为POST
* @return
*/
@RequestMapping(value="/test" , method=RequestMethod.POST)
public String test() {
System.out.println("test");
return SUCCESS;
}
}
此时映射的请求需为POST请求,<a超链接发送来的请求为GET,不可映射
例:
<form action="springmvc/test" method="POST">
<input type="submit" value="submit"/>
</form>
指定请求参数和请求头例子(仅作了解)
@RequestMapping(value="/testParams",
params= {"username","age!=20"},
headers= {"Accept-Language=en-US,en;q=0.9,zh;q=0.8,zh-CN;q=0.7"})
public String testParams() {
System.out.println("testParams");
return SUCCESS;
}
jsp链接:
<a href="springmvc/testParams?username=zhangsan&age=10">testParams</a>
请求头信息需要查看浏览器的开发者模式得知
@RequestMapping映射请求URL
-支持Ant风格的URL:
Ant风格资源地址支持3种匹配符:
? :匹配文件名中的一个字符
:匹配文件名中的任意字符
**:匹配多层路径
例:/user//xxx 匹配 /user/aaa/xxx
/user/**/xxx 匹配 /user/bbb/aaa/xxx
/user/aa??/xxx 匹配/user/aabb/xxx
@PathVariable
@PathVariable映射URL绑定的占位符
通过@PathVariable可以将URL中占位符参数绑定到控制器处理方法的形参中:URL中的 {xxx} 占位符可以通过@PathVariable(“xxx”)绑定到操作方法的形参中。
/**
* @PathVariable 可以映射URL中的占位符到目标方法的参数中。
*/
@RequestMapping("/testPathVariable/{num}")
public String testPathVariable(@PathVariable("num") int num) {
System.out.println("testPathVariable::"+num);
return SUCCESS;
}
jsp链接:
<a href="springmvc/testPathVariable/1">testPathVariable</a>
把 1 赋给了testPathVariable函数的num
REST
RESR:即Representational State Transfer表现层(资源)状态转化(表述性状态传递)。
资源:URL即为每一个资源的独一无二的识别符
表现层:把资源具体呈现出来的形式
状态转化:HTTP协议里,四个表示操作方式的动词:GET、POST、PUT、DELETE。它们分别对应四种基本操作:GET用来获取资源;POST用来新建资源;PUT用来更新资源;DELETE用来删除资源。
示例:
…/order/1 HTTP GET:得到 id=1 的order
…/order/1 HTTP DELETE:删除 id=1 的order
…/order/1 HTTP PUT:更新 id=1 的order
…/order HTTP POST:新增 order
- HiddenHttpMethodFilter(Tomcat7.0)
浏览器form表单只支持GET与POST请求,而DELETE、PUT等method并不支持,该过滤器可以将这些请求转换成标准的http方法,使得支持GET、POST、PUT与DELETE请求。将_method属性值转为POST。
/**
* Rest 风格的URL
* 以CRUD 为例:
* 新增:/order POST
* 修改:/order/1 PUT
* 获取:/order/1 GET
* 删除: /order/1 DELETE
*
* 如何发送PUT请求和DELETE请求:
* 1、需要配置HiddenHttpMethodFilter
* 2、需要POST请求
* 3、需要在发送POST请求时携带一个name=“_method"的隐藏域,值为PUT或 DELETE
*
* 在SpringMVC的目标方法中如何得到id呢?
* 使用@ParhVariable注解
* @param id
* @return
*/
@RequestMapping(value="/testRest/{id}",method=RequestMethod.GET)
public String testRest(@PathVariable int id) {
System.out.println("testRest GET:"+id);
return SUCCESS;
}
@RequestMapping(value="/testRest",method=RequestMethod.POST)
public String testRest() {
System.out.println("testRest POST:");
return SUCCESS;
}
@RequestMapping(value="/testRest/{id}",method=RequestMethod.DELETE)
public String testRestDelete(@PathVariable Integer id) {
System.out.println("testRest DELETE:"+id);
return SUCCESS;
}
@RequestMapping(value="/testRest/{id}",method=RequestMethod.PUT)
public String testRestPut(@PathVariable Integer id) {
System.out.println("testRest PUT:"+id);
return SUCCESS;
}
配置HiddenHttpMethodFilter
<!-- 配置 org.springframework.web.filter.HiddenHttpMethodFilter:可以把POST 请求转为 DELETE 或 POST 请求-->
<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>
jsp文件:
<a href="springmvc/testRest/1">Test Rest GET</a>
<br/>
<form action="springmvc/testRest" method="POST">
<input type="submit" value="Test Rest POST"/>
</form>
<br/>
<form action="springmvc/testRest/1" method="POST">
<input type="hidden" name="_method" value="DELETE"/>
<input type="submit" value="Test Rest DELETE"/>
</form>
<br/>
<form action="springmvc/testRest/1" method="POST">
<input type="hidden" name="_method" value="PUT"/>
<input type="submit" value="Test Rest PUT"/>
</form>