springMVC重点知识
一、springMVC请求流程
二、springMVC环境搭建
Idea 下创建 springmvc01 ⼯程
1、pom.xml 坐标添加
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>12</maven.compiler.source>
<maven.compiler.target>12</maven.compiler.target>
</properties>
<dependencies>
<!-- spring web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.4.RELEASE</version>
</dependency>
<!-- spring mvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.4.RELEASE</version>
</dependency>
<!-- web servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 编译环境插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- jetty插件 -->
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.4.27.v20200227</version>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<!-- 设置端⼝ -->
<httpConnector>
<port>8080</port>
</httpConnector>
<!-- 设置项⽬路径 -->
<webAppConfig>
<contextPath>/springmvc01</contextPath>
</webAppConfig>
</configuration>
</plugin>
</plugins>
</build>
2、配置 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<!-- 编码过滤 utf-8 -->
<filter>
<description>char encoding filter</description>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filterclass>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- servlet请求分发器 -->
<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:servlet-context.xml</param-value>
</init-param>
<!-- 表示启动容器时初始化该Servlet -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<!-- 这是拦截请求, "/"代表拦截所有请求,"*.do"拦截所有.do请求 -->
<!-- <url-pattern>/</url-pattern> -->
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
3、servlet-context.xml 配置
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
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="com.xxxx.springmvc.controller"/>
<!-- 使⽤默认的 Servlet 来响应静态⽂件 -->
<mvc:default-servlet-handler/>
<!-- 开启注解驱动-->
<mvc:annotation-driven/>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<!-- 前缀:在WEB-INF⽬录下的jsp⽬录下 -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!-- 后缀:以.jsp结尾的资源 -->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
4、⻚⾯控制器的编写
@Controller
public class HelloController {
/**
* 请求映射地址 /hello.do
* @return
*/
@RequestMapping("/hello")
public ModelAndView hello(){
ModelAndView mv=new ModelAndView();
mv.addObject("hello", "hello spring mvc");
mv.setViewName("hello");
return mv;
}
}
5、添加视图⻚⾯
在 WEB-INF 下新建 jsp ⽂件夹 ,并在⽂件夹下新建 hello.jsp
<%@page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath =
request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"
;
%>
<!DOCTYPE HTML>
<html>
<head>
<base href="<%=basePath %>">
<title>My JSP 'hello.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<!-- el表达式接收参数值 -->
${hello}
</body>
</html>
6、启动 jetty 服务器
访问地址 http://localhost:8080/springmvc01/hello.do
⾄此,springmvc 环境搭建完毕
三、URL 地址映射配置
通过注解 @RequestMapping 将请求地址与⽅法进⾏绑定,可以在类级别和⽅法级别声明。类级别的注解负责将⼀个特定的请求路径映射到⼀个控制器上,将 url 和类绑定;通过⽅法级别的注解可以细化映射,能够将⼀个特定的请求路径映射到某个具体的⽅法上,将 url 和类的⽅法绑定。
1、映射单个 URL
@RequestMapping("") 或 @RequestMapping(value="")
/**
* @RequestMapping 声明在⽅法上⾯,映射单个 URL
* 访问地址:(如果有类路径需要写在⽅法路径前⾯)
* http://ip:port/springmvc01/test01
* @return
*/
@RequestMapping("/test01")
// @RequestMapping(value = "/test01")
public ModelAndView test01(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("hello","test01");
modelAndView.setViewName("hello");
return modelAndView;
}
/**
* 路径开头是否加 斜杠"/" 均可
* @RequestMapping("/请求路径") 与 @RequestMapping("请求路径")均可
* 建议加上,如:@RequestMapping("/test02")
* 访问地址:(如果有类路径需要写在⽅法路径前⾯)
* http://ip:port/springmvc01/test02
* @return
*/
@RequestMapping("test02")
public ModelAndView test02(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("hello","test02");
modelAndView.setViewName("hello");
return modelAndView;
}
2、映射多个 URL
@RequestMapping({"",""}) 或 @RequestMapping(value={"",""})
/**
* @RequestMapping 声明在⽅法上⾯,映射多个 URL
* ⽀持⼀个⽅法绑定多个 url 的操作
* 访问地址:(如果有类路径需要写在⽅法路径前⾯)
* http://ip:port/springmvc01/test03_01
* http://ip:port/springmvc01/test03_02
* @return
*/
@RequestMapping({"/test03_01","/test03_02"})
// @RequestMapping(value = {"/test03_01","/test03_02"})
public ModelAndView test03(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("hello","test03");
modelAndView.setViewName("hello");
return modelAndView;
}
3、映射 URL 在控制器上
⽤于类上,表示类中的所有响应请求的⽅法都是以该地址作为⽗路径。
@Controller
@RequestMapping("/url")
public class UrlController {
/**
* @RequestMapping 声明在类上⾯,类中的的⽅法都是以该地址作为⽗路径
* 声明级别:
* 类级别 + ⽅法级别 (/类路径/⽅法路径)
* 访问地址:
* http://ip:port/springmvc01/url/test04
* @return
*/
@RequestMapping("/test04")
public ModelAndView test04(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("hello","test04");
modelAndView.setViewName("hello");
return modelAndView;
}
}
4、设置 URL 映射的请求⽅式
默认没有设置请求⽅式,在HTTP 请求中最常⽤的请求⽅法是 GET、POST,还有其他的⼀些⽅法,
如:DELET、PUT、HEAD 等。
可以通过 method 属性设置⽀持的请求⽅式,如 method=RequestMethod.POST;如设置多种请求⽅式,以⼤括号包围,逗号隔开即可。
/**
* 设置请求⽅式
* 通过 method 属性设置⽅法⽀持的请求⽅式,默认 GET请求和 POST等请求都⽀持。
* 设置了请求⽅式,则只能按照指定的请求⽅式请求。
* 访问地址:(只能使⽤POST请求访问)
* http://ip:port/springmvc01/url/test05
* @return
*/
@RequestMapping(value = "/test05",method = RequestMethod.POST)
public ModelAndView test05(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("hello","test05");
modelAndView.setViewName("hello");
return modelAndView;
}
5、通过参数名称映射 URL
/**
* 通过参数名称访问
* 通过参数的形式访问
* 访问地址:
* http://ip:port/springmvc01/url?test06
* @return
*/
@RequestMapping(params = "test06")
public ModelAndView test06(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("hello","test06");
modelAndView.setViewName("hello");
return modelAndView;
}
原文地址:https://blog.csdn.net/weixin_64689713/article/details/143905807
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!