J2EE Spring mvc环境配置
1.下载并配置jdk tomcat eclipse
2.eclipse的配置(在eclipse中用maven创建项目)
1)右键new>>Maven Project
2)选择maven-archetype-webapp
3)点击next继续,填写Group id和Artifact id, Version默认,Package可以不填,然后完成创建
3.Spring mvc配置
1)开项目中的pom.xml文件,并点击Dependencies标签,点击add添加新的依赖
如果知道依赖的group id和artifact id,可以直接填写,如果不清楚,可以输入关键字进行查询,或是到http://search.maven.org网站查询
2)需要添加的依赖有:spring-webmvc,版本为4.1.4. RELEASE。
在pom.xml中添加以下代码块
<properties>
<spring.version>4.1.4.RELEASE</spring.version>
</properties>
3)打开src/main/webapp/WEB-INF/web.xml文件,最终修改如如下内容:
<?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/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="study" version="2.5">
<display-name>Archetype Created Web Application</display-name>
<description>sprintMVC环境搭建</description>
<!-- 加载Spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/configs/spring-*.xml</param-value>
</context-param>
<!-- Spring监听 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring MVC配置 -->
<servlet>
<servlet-name>Dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 自定义spring mvc的配置文件名称和路径 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:configs/spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- spring mvc 请求后缀 -->
<servlet-mapping>
<servlet-name>Dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
4)在Java Resources/scr/main/resources目录下,创建configs文件夹,以便存放在web.xml中声明的配置路径
在Java Resources/scr/main/resources/configs目录下,创建spring-servlet.xml,内容如下:
<?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:jee="http://www.springframework.org/schema/jee"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.1.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.springstudy.controller" />
<mvc:annotation-driven />
<mvc:resources mapping="/styles/**" location="/styles/" />
<mvc:resources mapping="/scripts/**" location="/scripts/" />
<mvc:resources mapping="/images/**" location="/images/" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
5)配置jar例如:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
</dependency>
spring mvc的环境就配置好啦