一、新建工程
1、新建一个空的maven工程。
2、添加多个maven module,按需选择 "Create from archetype" ->
maven-archetype-webapp,或者不选择 "Create from archetype"。
3、完成之后,结构可能如下图
4、编译之后,源文件和目标文件的映射关系
二、文件配置
参考工程:spring-mvc-showcase
1、根目录下的pom.xml文件
<properties>
<spring.version>4.2.7.RELEASE</spring.version>
</properties>
...
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
Spring MVC依赖了如下几个包:
- spring-beans
- spring-context
- spring-core
- spring-web
- spring-expression
2、web.xml配置(src/main/webapp/WEB-INF/web.xml)
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup> // 小于0: 可以在任何时候load;0: 在应用部署时load;大于0: 按照值的顺序load,较小的先load
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
contextConfigLocation 有两种路径的写法
/WEB-INF/spring/some-context.xml
-
classpath:other-context.xml
, 指向src\main\resources\other-context.xml
3、JSP 视图解析器配置
DispatcherServlet 的配置文件中
<context:component-scan base-package="me.johnshen.springmvc.controller"/>
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources/ directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
jsp 片段示例
<head>
<title>spring-mvc-showcase</title>
<link href="<c:url value="/resources/form.css" />" rel="stylesheet" type="text/css" />
<link href="<c:url value="/resources/jqueryui/1.8/themes/base/jquery.ui.core.css" />" rel="stylesheet" type="text/css"/>
<link href="<c:url value="/resources/jqueryui/1.8/themes/base/jquery.ui.theme.css" />" rel="stylesheet" type="text/css"/>
<link href="<c:url value="/resources/jqueryui/1.8/themes/base/jquery.ui.tabs.css" />" rel="stylesheet" type="text/css"/>
<!--
Used for including CSRF token in JSON requests
Also see bottom of this file for adding CSRF token to JQuery AJAX requests
-->
<meta name="_csrf" content="${_csrf.token}"/>
<meta name="_csrf_header" content="${_csrf.headerName}"/>
</head>
4、Velocity 视图解析器配置
maven 依赖
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-tools</artifactId>
<version>2.0</version>
</dependency>
配置文件
<context:component-scan base-package="com.aliyun.drcloud.portal.action"/>
<bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath" value="/WEB-INF/velocity/"/>
<property name="velocityProperties">
<props>
<prop key="input.encoding">utf-8</prop>
<prop key="output.encoding">utf-8</prop>
</props>
</property>
</bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="cache" value="true"/>
<property name="suffix" value=".vm"/>
<property name="contentType" value="text/html;charset=UTF-8"/>
</bean>
三、Tomcat 部署
1.利用Tomcat自动部署
将 ourweb 目录拷贝到$CATALINA_HOME/webapps
下,然后启动服务器就可以了,Tomcat启动时将自动加载应用。
访问地址如下:http://localhost:8080/ourweb/
2、修改Server.xml文件部署
修改$CATALINA_HOME/conf/server.xml
文件,在在<Host> </Host>
标签之间输入
<Context path ="/myapp" reloadable ="false" docBase ="local path to myapp" workDir ="local path to myapp tmp" />
其中,
- reloadable
表示可以在运行时在classes与lib文件夹下自动加载类包。其中reloadable="false"表示当应用程序 中的内容发生更改之后服务器不会自动加载,这个属性在开发阶段通常都设为true,方便开发,在发布阶段应该设置为false,提高应用程序的访问速度。
3、增加自定义web部署文件(推荐使用,不需要重启Tomcat)
在$CATALINA_HOME/conf/Catalina/localhost
中添加一个xml文件
<Context path ="/myapp" docBase ="local path to myapp" debug ="0" privileged ="true" reloadable ="false" >
</Context>