在上一节的基础上集成spring_mvc
首先添加依赖文件
<properties> <javax.servlet.version>4.0.0</javax.servlet.version> <jstl.version>1.2</jstl.version> </properties> <!--springMVC start--> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>${jstl.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>${javax.servlet.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <!--springMVC end -->
之后在web.xml中配置
<servlet> <servlet-name>spring-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 配置springMVC需要加载的文件--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring-dispatcher</servlet-name> <!-- 默认匹配所有请求--> <url-pattern>/</url-pattern> </servlet-mapping>
之后在resources下创建Spring-mvc配置文件
插入代码
<?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" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <!-- 配置组件扫描 --> <context:component-scan base-package="com.xjx.controller" /> <!-- 配置MVC注解扫描 --> <mvc:annotation-driven /> <!-- 配置视图解析器, --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/views" /><!-- 前缀 --> <property name="suffix" value=".jsp" /><!-- 后缀 --> </bean> <!-- 配置拦截器 <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/*"/> <bean class="interceptors.SomeInterceptor"/> </mvc:interceptor> </mvc:interceptors> --> </beans>
<property name="prefix" value="/WEB-INF/views" /><!-- 前端代码的位置--> <property name="suffix" value=".jsp" /><!-- 后缀 -->
然后创建
/WEB-INF/views下创建jsp文件
再在运行文件下加上RequestMapping注解:
之后就可以部署在运行了
原文:https://www.cnblogs.com/Xjx-zhenCai/p/10633856.html