牧场SZShepherd 2020-06-03
一、新建过程
二、构架目录结构
1.在main目录下new出两个文件夹:java、resources。并且分别设为Sources Root 和 Resource Root。
2.在webapp目录下建立三个文件夹:js,image,css
3.在WEB-INF目录下建立一个文件夹:pages(要和视图解析器配的一致)
三、配置
1.添加相应的坐标依赖
org.springframework:spring-context、spring-web、spring-webmvc
javax.servlet:servlet-api
javax.servlet.jsp:jsp-api
<dependencies> <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> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> </dependency> </dependencies>
2.在Resources下创建Spring的配置文件
主要有几个作用:
a命名空间:beans,context,mvc
b.注解扫描
c.视图解析器
d.注解支持:
e.<mvc:anotation-driven>会自动加载处理映射器和处理器适配器
<?xml version="1.0" encoding="UTF-8"?> <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" xmlns="http://www.springframework.org/schema/beans" 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"> <context:component-scan base-package=""/> <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"/> <property name="suffix" value=".jsp"/> </bean> <mvc:resources mapping="/js/**" location="/js/"/> <mvc:annotation-driven/> </beans>
3.在web.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></servlet><servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern></servlet-mapping>
4.配置tomcat