1 SpringMVC的介绍
springMVC是Spring框架的一部分,是基于java实现的一个轻量级web框架 学习SpringMVC框架最核心的就是DispatcherServlet的设计,掌握好DispatcherServlet是掌握SpringMVC的核心关键
2 基于XML的HelloWorld
创建maven项目–右键项目–Add Framework Support–勾选Web Application(4.0) 添加pom依赖
< dependencies>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-context</ artifactId>
< version> 5.2.3.RELEASE</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-web</ artifactId>
< version> 5.2.3.RELEASE</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-webmvc</ artifactId>
< version> 5.2.3.RELEASE</ version>
</ dependency>
</ dependencies>
编写web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
< web-app xmlns = " http://xmlns.jcp.org/xml/ns/javaee"
xmlns: xsi= " http://www.w3.org/2001/XMLSchema-instance"
xsi: schemaLocation= " http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version = " 4.0" >
< 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:applicationContext.xml</ param-value>
</ init-param>
</ servlet>
< servlet-mapping>
< servlet-name> springmvc</ servlet-name>
< url-pattern> /</ url-pattern>
</ servlet-mapping>
< servlet>
< servlet-name> default</ servlet-name>
< servlet-class> org.apache.catalina.servlets.DefaultServlet</ servlet-class>
</ servlet>
< servlet-mapping>
< servlet-name> default</ servlet-name>
< url-pattern> *.html</ url-pattern>
</ servlet-mapping>
</ web-app>
applicationContext.xml:在resources下编写spring配置文件
<?xml version="1.0" encoding="UTF-8"?>
< beans xmlns = " http://www.springframework.org/schema/beans"
xmlns: xsi= " http://www.w3.org/2001/XMLSchema-instance"
xsi: schemaLocation= " http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" >
< bean id = " internalResourceViewResolver" class = " org.springframework.web.servlet.view.InternalResourceViewResolver" >
< property name = " prefix" value = " /WEB-INF/jsp/" > </ property>
< property name = " suffix" value = " .jsp" > </ property>
</ bean>
< bean id = " /hello" class = " com.mashibing.controller.HelloController" > </ bean>
</ beans>
HelloController.java:需要导入servlet和jsp的jar包,否则编译不过去
package com. mashibing. controller;
import org. springframework. web. servlet. ModelAndView;
import org. springframework. web. servlet. mvc. Controller;
import javax. servlet. http. HttpServletRequest;
import javax. servlet. http. HttpServletResponse;
public class HelloController implements Controller {
public ModelAndView handleRequest ( HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
ModelAndView mv = new ModelAndView ( ) ;
mv. addObject ( "msg" , "helloSpringMVC" ) ;
mv. setViewName ( "hello" ) ;
return mv;
}
}
创建hello.jsp页面
< % --
Created by IntelliJ IDEA.
User: root
Date: 2020 / 3 / 5
Time: 20 : 25
To change this template use File | Settings | File Templates.
-- % >
< % @ page contentType= "text/html;charset=UTF-8" language= "java" % >
< html>
< head>
< title> Title< / title>
< / head>
< body>
${ msg}
< / body>
< / html>
Artifacts–右键WEB-INF–Create Directory–lib–右键lib–Add Copy Of–Library Files–选中Tomcat中没有的所有jar包 配置tomcat,发送请求:http://localhost:8080/hello
3 基于注解的HelloWorld
添加pom依赖 web.xml spring配置文件:applicationContext.xml
< context: component-scan base-package = " com.mashibing" > </ context: component-scan>
HelloController.java
package com. mashibing. controller;
import org. springframework. stereotype. Controller;
import org. springframework. ui. Model;
import org. springframework. web. bind. annotation. RequestMapping;
@Controller
public class HelloController {
@RequestMapping ( "/hello" )
public String hello ( Model model) {
model. addAttribute ( "msg" , "hello,SpringMVC" ) ;
return "hello" ;
}
}
编写hello.jsp 输入请求http://localhost:8080/hello
4 @RequestMapping
4.1 属性值
value:要匹配的请求 method:限制发送请求的方式: method={RequestMethod.POST} params:表示请求要接受的参数,如果定义了这个属性,那么发送的时候必须要添加该参数
直接写参数的名称:params = {“username”} 表示请求不能包含的参数:params = {"!username"} 表示请求中需要要包含的参数但是可以限制值:params = {“username!=123”,“age”} headers:request中必须包含指定的header值,该方法才能处理请求 consumes:方法只处理Content-Type为指定值的请求,在request中,Content-Type用来告诉服务器当前发送的数据是什么格式 produces:方法只处理Accept中包含指定值的请求,在request中,Accept用来告诉服务器,客户端能认识哪些格式,最好返回这些格式中的其中一种
package com. mashibing. controller;
import org. springframework. stereotype. Controller;
import org. springframework. ui. Model;
import org. springframework. web. bind. annotation. RequestMapping;
import org. springframework. web. bind. annotation. RequestMethod;
@Controller
@RequestMapping ( "/mashibing" )
public class HelloController {
@RequestMapping ( "/hello" )
public String hello ( Model model) {
model. addAttribute ( "msg" , "hello,SpringMVC" ) ;
return "hello" ;
}
@RequestMapping ( value = "/hello2" , method = RequestMethod. POST)
public String hello2 ( ) {
return "hello" ;
}
@RequestMapping ( value = "/hello3" , params = { "username!=123" , "age" } )
public String hello3 ( String username) {
System. out. println ( username) ;
return "hello" ;
}
@RequestMapping ( value = "/hello4" , headers = { "User-Agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:73.0) Gecko/20100101 Firefox/73.0" } )
public String hello4 ( ) {
return "hello" ;
}
}
4.2 通配符
@RequestMapping包含三种模糊匹配的方式
?:能替代任意一个字符 *: 能替代任意多个字符和一层路径 **:能代替多层路径
@RequestMapping ( value = "/**/h*llo?" )
public String hello5 ( ) {
System. out. println ( "hello5" ) ;
return "hello" ;
}
5 @PathVariable
可以获取请求路径中的值 如果不加@PathVariable,默认为url中,?name="zhangsan"中的这个name
@RequestMapping ( value = "/pathVariable/{name}" )
public String pathVariable ( @PathVariable ( "name" ) String name) {
System. out. println ( name) ;
return "hello" ;
}
6 REST
REST为表现层状态转化,是目前最流行的一个互联网软件架构,可以降低开发的复杂性,提高系统的可伸缩性 我们在获取资源的时候就是进行增删改查的操作,如果是原来的架构风格,需要发送四个请求
查询:localhost:8080/query?id=1 增加:localhost:8080/insert 删除:localhost:8080/delete?id=1 更新:localhost:8080/update?id=1 按照此方式发送请求的时候比较麻烦,需要定义多种请求,而在HTTP协议中,有不同的发送请求的方式,分别是GET、POST、PUT、DELETE等,我们如果能让不同的请求方式表示不同的请求类型就可以简化我们的查询
查询:localhost:8080/book/1:发送get请求 增加:localhost:8080/book:发送post请求 删除:localhost:8080/book/1:发送delete请求 更新:localhost:8080/book/1:发送put请求 我们在发送请求的时候只能发送post或者get,没有办法发送put和delete请求,因此需要在页面中,对请求类型进行转换,这也就是REST表现层状态转换的名称由来 RestController.java
package com. mashibing. controller;
import org. springframework. stereotype. Controller;
import org. springframework. web. bind. annotation. PathVariable;
import org. springframework. web. bind. annotation. RequestMapping;
import org. springframework. web. bind. annotation. RequestMethod;
import org. springframework. web. servlet. view. InternalResourceViewResolver;
@Controller
public class RestController {
@RequestMapping ( value = "/user" , method = RequestMethod. POST)
public String add ( ) {
System. out. println ( "添加" ) ;
return "success" ;
}
@RequestMapping ( value = "/user/{id}" , method = RequestMethod. DELETE)
public String delete ( @PathVariable ( "id" ) Integer id) {
System. out. println ( "删除:" + id) ;
return "success" ;
}
@RequestMapping ( value = "/user/{id}" , method = RequestMethod. PUT)
public String update ( @PathVariable ( "id" ) Integer id) {
System. out. println ( "更新:" + id) ;
return "success" ;
}
@RequestMapping ( value = "/user/{id}" , method = RequestMethod. GET)
public String query ( @PathVariable ( "id" ) Integer id) {
System. out. println ( "查询:" + id) ;
return "success" ;
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
< web-app xmlns = " http://xmlns.jcp.org/xml/ns/javaee"
xmlns: xsi= " http://www.w3.org/2001/XMLSchema-instance"
xsi: schemaLocation= " http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version = " 4.0" >
< 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-servlet.xml</ param-value>
</ init-param>
</ servlet>
< servlet-mapping>
< servlet-name> springmvc</ servlet-name>
< url-pattern> /</ url-pattern>
</ servlet-mapping>
< filter>
< filter-name> hiddenFilter</ filter-name>
< filter-class> org.springframework.web.filter.HiddenHttpMethodFilter</ filter-class>
</ filter>
< filter-mapping>
< filter-name> hiddenFilter</ filter-name>
< url-pattern> /*</ url-pattern>
</ filter-mapping>
</ web-app>
rest.jsp
< % --
Created by IntelliJ IDEA.
User: root
Date: 2020 / 3 / 6
Time: 23 : 01
To change this template use File | Settings | File Templates.
-- % >
< % @ page contentType= "text/html;charset=UTF-8" language= "java" % >
< html>
< head>
< title> Title< / title>
< / head>
< body>
< form action= "/user" method= "post" >
< input type= "submit" value= "增加" >
< / form>
< form action= "/user/1" method= "post" >
< ! -- 在form表单中,需要配置一个input,name固定为_method,value配置为想转换成的请求-- >
< input name= "_method" value= "delete" type= "hidden" >
< input type= "submit" value= "删除" >
< / form>
< form action= "/user/1" method= "post" >
< input name= "_method" value= "put" type= "hidden" >
< input type= "submit" value= "修改" >
< / form>
< a href= "/user/1" > 查询< / a> < br/ >
< / body>
< / html>
success.jsp
< % @ page contentType= "text/html;charset=UTF-8" language= "java" isErrorPage= "true" % >
< html>
< head>
< title> Title< / title>
< / head>
< body>
666
< / body>
< / html>