First, a simple understanding of the use of spring mvc
First, build a maven web project, the easiest mvc project would just be
<dependency> <the groupId> org.springframework </ the groupId> <the artifactId> Spring-webmvc </ the artifactId> <Version> 5.1.7.RELEASE </ Version> </ dependency> // Note configuration scope, and to prevent self-tomcat conflict with the jar <dependency> <the groupId> the javax.servlet </ the groupId> <the artifactId> the javax.servlet-API </ the artifactId> <Version> 4.0.1 </ Version> <scope> Provided </ scope> </ dependency > // if not applicable jsp, you can not configure the <dependency> <groupId> the javax.servlet </ groupId> <artifactId> JSTL </artifactId> <version>1.2</version> </dependency>
web.xml simplest configuration
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <!-- root ioc --> <display-name>study spring mvc </display-name> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/spring-mvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>/index.html</welcome-file> </welcome-file-list> </web-app>
Mvc resources created in the directory xml configuration file, in accordance with the specific name of the initialization parameters in web.xml, are the most basic configuration of the components used source code analysis
<?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: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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="urlMap"> <props> <prop key="/test.do">simpleConrol</prop> </props> </property> <property name="interceptors" ref="myHandlerInterceptor"></property> </bean> <!--控制器 --> <bean id="simpleConrol" class="control.SimpleControl "/> class <bean <-! Execute Adapter ->="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" /> <!--资源解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/page/" /> <property name="suffix" value=".jsp" /> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> </bean> <bean id="myHandlerInterceptor" class="interceptor.MyHandlerInterceptor"></bean> <bean class="exceptionResolver.SimpleHandlerExceptionResolver"></bean> </beans>
The following are the components of the class
controller
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; public class SimpleControl implements Controller{ public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { int a = 1/0; ModelAndView mv = new ModelAndView("test"); mv.addObject("word", "this is test word"); return mv; } }
HandlerExceptionResolver
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.HandlerExceptionResolver; import org.springframework.web.servlet.ModelAndView; public class SimpleHandlerExceptionResolver implements HandlerExceptionResolver { public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { ModelAndView mv = new ModelAndView("error"); return mv; } }
HandlerInterceptor
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; public class MyHandlerInterceptor implements HandlerInterceptor { public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("this is MyHandlerInterceptor preHandle"); return true; } public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("this is MyHandlerInterceptor postHandle"); } public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("this is MyHandlerInterceptor afterCompletion"); } }
May also be custom exception, special return code returned when the returned exception Custom
import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.ResponseStatus; @ResponseStatus(code = HttpStatus.INTERNAL_SERVER_ERROR, reason = "this is myException") public class MyException extends Exception { private static final long serialVersionUID = 1L; }
Is typically configured to take over
<?xml version="1.0" encoding="GBK"?> <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: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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="com.tuling.mvc.control"/> <!-- 注解驱动 --> <mvc:annotation-driven/> <!-- 视图仓库 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/page/"/> <property name="suffix" value=".jsp"/> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> </bean> </beans>