在maven项目中搭建SpringMvc

1.pom文件

<span style="white-space:pre">	</span><properties>
		<spring.version>3.2.4.RELEASE</spring.version>
		<hibernate.version>4.2.5.Final</hibernate.version>
		<spring-data-jpa.version>1.3.4.RELEASE</spring-data-jpa.version>
		<slf4j.version>1.7.5</slf4j.version>
		<struts.version>2.3.15.1</struts.version>
		<cxf.version>2.7.7</cxf.version>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>
 
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.11</version> 
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.data</groupId>
			<artifactId>spring-data-jpa</artifactId>
			<version>${spring-data-jpa.version}</version>
			<exclusions>
				<exclusion>
					<groupId>junit</groupId>
					<artifactId>junit-dep</artifactId>
				</exclusion>
				<exclusion>
					<groupId>org.springframework</groupId>
					<artifactId>spring-aop</artifactId>
				</exclusion>
				<exclusion>
					<groupId>org.springframework</groupId>
					<artifactId>spring-asm</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</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-jdbc</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${spring.version}</version>
		</dependency>


		<!-- WEB begin -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.data</groupId>
			<artifactId>spring-data-redis</artifactId>
			<version>1.4.1.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>redis.clients</groupId>
			<artifactId>jedis</artifactId>
			<version>2.5.2</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.session</groupId>
			<artifactId>spring-session</artifactId>
			<version>1.0.0.RELEASE</version>
		</dependency>  
		
	</dependencies>

这中间有点不需要的,我也没去掉

然后加入tomcat插件

<span style="white-space:pre">	</span><build>
		<plugins>
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>tomcat-maven-plugin</artifactId>
				<version>1.0-beta-1</version>
			</plugin>
		</plugins>
		
		<finalName>testWeb</finalName>
	</build>




在web.xml文件中配置

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  
  <listener> 
     <listener-class>
          org.springframework.web.context.ContextLoaderListener
     </listener-class> 
	</listener> 
	
	  <!-- spring容器配置 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:applicationContext.xml</param-value>
	</context-param>
	
	
  <servlet>
		<servlet-name>dispatch</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:*springmvcContext.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>dispatch</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

applicationContext.xml和springmvcContext.xml都在src/main/resources目录下面

applicationContext.xml文件内容

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 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:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context.xsd 
      http://www.springframework.org/schema/mvc 
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
	
	<!-- 启动注解驱动的Spring MVC功能,注册请求url和注解POJO类方法的映射 -->
	<mvc:annotation-driven />
	<mvc:default-servlet-handler />
	
	
</beans> 

springmvcContext.xml 的内容如下
<pre name="code" class="html"><?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 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:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context.xsd 
      http://www.springframework.org/schema/mvc 
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

	<!-- 启动注解驱动的Spring MVC功能,注册请求url和注解POJO类方法的映射 -->
	<mvc:annotation-driven />
	<mvc:default-servlet-handler />

	<!-- 启动包扫描功能,以便注册带有@Controller、@Service、@repository、@Component等注解的类成为spring的bean -->
	<context:component-scan base-package="com.test.control">
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
	</context:component-scan>

	<!-- 对模型视图名称的解析,在WEB-INF/jsp目录下找对应的jsp文件 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
	<!--放过/scripts下的静态文件 -->
	<mvc:resources mapping="/scripts/**" location="/scripts/" />
	<mvc:resources mapping="/images/**" location="/images/" />
	<mvc:resources mapping="/css/**" location="/css/" />
	<mvc:resources mapping="/file/**" location="/file/" />
</beans> 



要注意

<!-- 启动包扫描功能,以便注册带有@Controller、@Service、@repository、@Component等注解的类成为spring的bean -->
	<context:component-scan base-package="com.test.control">
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
	</context:component-scan>
base-package的值,是你自己的包名


然后写个control  

package com.test.control;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@Scope("prototype")
@RequestMapping("/indexControl")
public class IndexControl {

	@RequestMapping(value="/index.do")
	public String fdIndex(){
		System.out.println("yes ");
		return "index/index";
	}
}


在WEB-INF/jsp目录下面新加一个index.jsp文件


在默认的index.jsp文件中加上

<jsp:forward page="indexControl/index.do"></jsp:forward>


现在基本上基础的配置都ok了

然后run as 中添加 tomcat:run -Dmaven.tomcat.port=9181

启动后,在浏览器中输入

http://localhost:9181/testWeb/

就可以访问了。over

整个项目的结构是







posted on 2015-12-25 12:15  woqiaoxun  阅读(253)  评论(0编辑  收藏  举报