(1) Development environment
create
.Solve the problem of slow maven project creation:
archetypeCatalog
internal
Create direction: java, resources: right click: mark directory as (sources root and Resources root)
(2) Guide coordinates (guide package)
<spring.version>5.0.2.RELEASE</spring.version>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
(3) Front-end controller
<web-app> <display-name>Archetype Created Web Application</display-name> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
(5) Configure Tomcat server
(6) Create a basic class, jsp page
(7) Guide constraint
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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
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">
(9)
<!--Enable annotation scanning--> <context:component-scan base-package="cn.rzpt"></context:component-scan>
@Controller public class HellController { @RequestMapping(path = "/hello") public String sayHello(){ System.out.println("hello springmvc"); return null; } }
(10) Tomcat starts springmvc.xml
<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:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
@RequestMapping(path = "/hello") public String sayHello(){ System.out.println("hello springmvc"); //By default jump to the jsp page with the success file name return "success"; }
Create success.jsp
<!--视图解析器对象--> <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"></property> <property name="suffix" value=".jsp" </bean>
<!--Enable the support of springmvc framework annotations--> <mvc:annotation-driven></mvc:annotation-driven>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>入门程序</title> </head> <body> <a href="hello">入门程序</a> </body> </html>
Start steps:
(1) Start the server and load some configuration files
-
According to the <load-on-startup>1</load-on-startup> configuration, the server creates the DispatcherServlet object
- During startup,
<param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> 被加载
- According to springmvc.xml, automatically scan the class, according to
@Controller public class HellController is created as an object and added to the spring container by default
-
<!--视图解析器对象--> <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"></property> <property name="suffix" value=".jsp"></property> </bean> 可以创建对象
-
<!--Enable the support of springmvc framework annotations--> <mvc:annotation-driven></mvc:annotation-driven> enable
@RequestMapping(path = "/hello") annotation takes effect public String sayHello()
(2) Send a request,
-
<a href="hello">Starter program</a>
- Send to web.xml, url-pattern filtering, Servlet----DispatcherServlet (command center, control function)
- According to the project path/hello, send a request to the Servlet written by myself:
HellController
- according to
@RequestMapping(path = "/hello"), execute the sayHello method, and return the success string
-
<!--View resolver object--> <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages /"></property> <property name="suffix" value=".jsp"></property> </bean> According to the configuration here, go to the success.jsp file
-