码迷,mamicode.com
首页 > 编程语言 > 详细

SpringMVC+Maven开发框架搭建

时间:2015-08-04 14:54:27      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:

文章只是利用maven将springMVC的框架搭建进行记录。

1.maven项目中pom.xml文件中的代码:

Xml代码 技术分享 技术分享
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.     <groupId>spring-web</groupId>  
  5.     <artifactId>Spring-Web-Maven</artifactId>  
  6.     <packaging>war</packaging>  
  7.     <version>0.0.1-SNAPSHOT</version>  
  8.     <name>Spring-Web-Maven Maven Webapp</name>  
  9.     <url>http://maven.apache.org</url>  
  10.   
  11.     <dependencies>  
  12.         <dependency>  
  13.             <groupId>junit</groupId>  
  14.             <artifactId>junit</artifactId>  
  15.             <version>3.8.1</version>  
  16.             <scope>test</scope>  
  17.         </dependency>  
  18.   
  19.         <dependency>  
  20.             <groupId>javax.servlet.jsp</groupId>  
  21.             <artifactId>jsp-api</artifactId>  
  22.             <version>2.2.1-b03</version>  
  23.         </dependency>  
  24.         <dependency>  
  25.             <groupId>javax.servlet</groupId>  
  26.             <artifactId>servlet-api</artifactId>  
  27.             <version>3.0-alpha-1</version>  
  28.         </dependency>  
  29.         <dependency>  
  30.             <groupId>javax.servlet</groupId>  
  31.             <artifactId>jstl</artifactId>  
  32.             <version>1.2</version>  
  33.         </dependency>  
  34.   
  35.         <!-- Spring -->  
  36.         <dependency>  
  37.             <groupId>org.springframework</groupId>  
  38.             <artifactId>spring-orm</artifactId>  
  39.             <version>4.1.4.RELEASE</version>  
  40.         </dependency>  
  41.   
  42.         <!-- Spring MVC -->  
  43.         <dependency>  
  44.             <groupId>org.springframework</groupId>  
  45.             <artifactId>spring-webmvc</artifactId>  
  46.             <version>4.1.4.RELEASE</version>  
  47.         </dependency>  
  48.   
  49.         <!-- aspectJ -->  
  50.         <dependency>  
  51.             <groupId>org.aspectj</groupId>  
  52.             <artifactId>aspectjweaver</artifactId>  
  53.             <version>1.8.4</version>  
  54.         </dependency>  
  55.   
  56.         <dependency>  
  57.             <groupId>commons-fileupload</groupId>  
  58.             <artifactId>commons-fileupload</artifactId>  
  59.             <version>1.2.2</version>  
  60.         </dependency>  
  61.         <dependency>  
  62.             <groupId>commons-io</groupId>  
  63.             <artifactId>commons-io</artifactId>  
  64.             <version>2.1</version>  
  65.         </dependency>  
  66.           
  67.     </dependencies>  
  68.   
  69.     <build>  
  70.         <finalName>Spring-Web-Maven</finalName>  
  71.     </build>  
  72. </project>  
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>spring-web</groupId>
	<artifactId>Spring-Web-Maven</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>Spring-Web-Maven Maven Webapp</name>
	<url>http://maven.apache.org</url>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>jsp-api</artifactId>
			<version>2.2.1-b03</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>3.0-alpha-1</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>

		<!-- Spring -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>4.1.4.RELEASE</version>
		</dependency>

		<!-- Spring MVC -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>4.1.4.RELEASE</version>
		</dependency>

		<!-- aspectJ -->
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>1.8.4</version>
		</dependency>

		<dependency>
			<groupId>commons-fileupload</groupId>
			<artifactId>commons-fileupload</artifactId>
			<version>1.2.2</version>
		</dependency>
		<dependency>
			<groupId>commons-io</groupId>
			<artifactId>commons-io</artifactId>
			<version>2.1</version>
		</dependency>
		
	</dependencies>

	<build>
		<finalName>Spring-Web-Maven</finalName>
	</build>
</project>

 2、web.xml文件中的代码:

Xml代码 技术分享 技术分享
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  5.     id="WebApp_ID" version="2.5">  
  6.   
  7.     <display-name>Spring-Web-Maven</display-name>  
  8.   
  9.     <welcome-file-list>  
  10.         <welcome-file>view/index.jsp</welcome-file>  
  11.     </welcome-file-list>  
  12.   
  13.     <!-- 加载所有的配置文件 -->  
  14.     <context-param>  
  15.         <param-name>contextConfigLocation</param-name>  
  16.         <param-value>classpath*:applicationContext-*.xml</param-value>  
  17.     </context-param>  
  18.   
  19.     <!-- 配置Spring监听 -->  
  20.     <listener>  
  21.         <listener-class>  
  22.                      org.springframework.web.context.ContextLoaderListener  
  23.                 </listener-class>  
  24.     </listener>  
  25.   
  26.     <!-- 配置SpringMVC -->  
  27.     <servlet>  
  28.         <servlet-name>springMVC</servlet-name>  
  29.         <servlet-class>  
  30.                          org.springframework.web.servlet.DispatcherServlet  
  31.                 </servlet-class>  
  32.         <init-param>  
  33.             <param-name>contextConfigLocation</param-name>  
  34.             <param-value>classpath*:applicationContext-*.xml</param-value>  
  35.         </init-param>  
  36.         <load-on-startup>1</load-on-startup>  
  37.     </servlet>  
  38.     <servlet-mapping>  
  39.         <servlet-name>springMVC</servlet-name>  
  40.         <url-pattern>/</url-pattern>  
  41.     </servlet-mapping>  
  42.   
  43.     <!-- 配置字符集 -->  
  44.     <filter>  
  45.         <filter-name>encodingFilter</filter-name>  
  46.         <filter-class>  
  47.                         org.springframework.web.filter.CharacterEncodingFilter  
  48.                 </filter-class>  
  49.         <init-param>  
  50.             <param-name>encoding</param-name>  
  51.             <param-value>UTF-8</param-value>  
  52.         </init-param>  
  53.         <init-param>  
  54.             <param-name>forceEncoding</param-name>  
  55.             <param-value>true</param-value>  
  56.         </init-param>  
  57.     </filter>  
  58.     <filter-mapping>  
  59.         <filter-name>encodingFilter</filter-name>  
  60.         <url-pattern>/*</url-pattern>  
  61.     </filter-mapping>  
  62.   
  63.     <!-- 配置Session -->  
  64.     <!-- <filter>  
  65.         <filter-name>openSession</filter-name>  
  66.         <filter-class>  
  67.                   org.springframework.orm.hibernate4.support.OpenSessionInViewFilter  
  68.                 </filter-class>  
  69.     </filter>  
  70.     <filter-mapping>  
  71.         <filter-name>openSession</filter-name>  
  72.         <url-pattern>/*</url-pattern>  
  73.     </filter-mapping> -->  
  74. </web-app>  
<?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="WebApp_ID" version="2.5">

	<display-name>Spring-Web-Maven</display-name>

	<welcome-file-list>
		<welcome-file>view/index.jsp</welcome-file>
	</welcome-file-list>

	<!-- 加载所有的配置文件 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:applicationContext-*.xml</param-value>
	</context-param>

	<!-- 配置Spring监听 -->
	<listener>
		<listener-class>
                     org.springframework.web.context.ContextLoaderListener
                </listener-class>
	</listener>

	<!-- 配置SpringMVC -->
	<servlet>
		<servlet-name>springMVC</servlet-name>
		<servlet-class>
                         org.springframework.web.servlet.DispatcherServlet
                </servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath*:applicationContext-*.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>springMVC</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

	<!-- 配置字符集 -->
	<filter>
		<filter-name>encodingFilter</filter-name>
		<filter-class>
                        org.springframework.web.filter.CharacterEncodingFilter
                </filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<!-- 配置Session -->
	<!-- <filter>
		<filter-name>openSession</filter-name>
		<filter-class>
                  org.springframework.orm.hibernate4.support.OpenSessionInViewFilter
                </filter-class>
	</filter>
	<filter-mapping>
		<filter-name>openSession</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping> -->
</web-app>

 3、applicationContext-spring-servlet.xml中的代码:

Xml代码 技术分享 技术分享
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"  
  4.     xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"  
  5.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
  6.     xmlns:jaxws="http://cxf.apache.org/jaxws"  
  7.     xsi:schemaLocation="  
  8.         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd  
  9.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  10.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd  
  11.         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
  12.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
  13.         http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">  
  14.   
  15.     <!-- ? -->  
  16.     <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">  
  17.         <property name="messageConverters">  
  18.             <list>  
  19.                 <bean  
  20.                     class="org.springframework.http.converter.StringHttpMessageConverter">  
  21.                     <property name="supportedMediaTypes">  
  22.                         <list>  
  23.                             <value>  
  24.                                                                  text/plain;charset=UTF-8  
  25.                                                         </value>  
  26.                             <value>  
  27.                                                                  text/html;charset=UTF-8  
  28.                                                         </value>  
  29.                         </list>  
  30.                     </property>  
  31.                 </bean>  
  32.             </list>  
  33.         </property>  
  34.     </bean>  
  35.   
  36.     <!-- 处理静态文件 -->  
  37.     <mvc:default-servlet-handler />  
  38.   
  39.     <!-- 开启注解 -->  
  40.     <mvc:annotation-driven />  
  41.   
  42.     <!-- 使用 component-scan后,可以将此注解移除-->  
  43.     <context:annotation-config />  
  44.   
  45.     <!-- 注解扫描包 -->  
  46.     <context:component-scan base-package="com.wang" />  
  47.   
  48.     <!-- 定义视图解析器 -->  
  49.     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  50.         <property name="order" value="1" />  
  51.         <property name="prefix" value="/view/" />  
  52.         <property name="suffix" value=".jsp"></property>  
  53.     </bean>  
  54.   
  55.     <!-- 上传文件大小控制 -->  
  56.     <bean id="multipartResolver"   
  57.                 class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
  58.         <property name="maxUploadSize" value="2097152000" />  
  59.     </bean>  
  60.   
  61.     <!-- mvc拦截器,这个拦截器类要实现HandlerInterceptor这个接口 -->  
  62.     <mvc:interceptors>  
  63.         <mvc:interceptor>  
  64.             <mvc:mapping path="/**" />  
  65.             <bean class="com.wang.interceptor.TestInterceptor" />  
  66.         </mvc:interceptor>  
  67.     </mvc:interceptors>  
  68.   
  69.     <!-- aspectJ -->  
  70.     <aop:aspectj-autoproxy />  
  71.     <bean id="LogManageAop" class="com.wang.aop.LogManageAop" />  
  72.     <aop:config>  
  73.         <aop:aspect ref="LogManageAop">  
  74.             <aop:pointcut id="logPointCut"   
  75.                                expression="(execution(* com.wang.controller.*.*(..)))" />  
  76.             <aop:after pointcut-ref="logPointCut" method="saveLog" />  
  77.         </aop:aspect>  
  78.     </aop:config>  
  79.   
  80. </beans>   
<?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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
		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-3.0.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
		http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

	<!-- ? -->
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="messageConverters">
			<list>
				<bean
					class="org.springframework.http.converter.StringHttpMessageConverter">
					<property name="supportedMediaTypes">
						<list>
							<value>
                                                                 text/plain;charset=UTF-8
                                                        </value>
							<value>
                                                                 text/html;charset=UTF-8
                                                        </value>
						</list>
					</property>
				</bean>
			</list>
		</property>
	</bean>

	<!-- 处理静态文件 -->
	<mvc:default-servlet-handler />

	<!-- 开启注解 -->
	<mvc:annotation-driven />

	<!-- 使用 component-scan后,可以将此注解移除-->
	<context:annotation-config />

	<!-- 注解扫描包 -->
	<context:component-scan base-package="com.wang" />

	<!-- 定义视图解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="order" value="1" />
		<property name="prefix" value="/view/" />
		<property name="suffix" value=".jsp"></property>
	</bean>

	<!-- 上传文件大小控制 -->
	<bean id="multipartResolver" 
                class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="maxUploadSize" value="2097152000" />
	</bean>

	<!-- mvc拦截器,这个拦截器类要实现HandlerInterceptor这个接口 -->
	<mvc:interceptors>
		<mvc:interceptor>
			<mvc:mapping path="/**" />
			<bean class="com.wang.interceptor.TestInterceptor" />
		</mvc:interceptor>
	</mvc:interceptors>

	<!-- aspectJ -->
	<aop:aspectj-autoproxy />
	<bean id="LogManageAop" class="com.wang.aop.LogManageAop" />
	<aop:config>
		<aop:aspect ref="LogManageAop">
			<aop:pointcut id="logPointCut" 
                               expression="(execution(* com.wang.controller.*.*(..)))" />
			<aop:after pointcut-ref="logPointCut" method="saveLog" />
		</aop:aspect>
	</aop:config>

</beans> 

 

java企业级通用权限安全框架源码 SpringMVC mybatis or hibernate+ehcache shiro druid bootstrap HTML5

【java框架源码下载】

SpringMVC+Maven开发框架搭建

标签:

原文地址:http://www.cnblogs.com/jixiantiaozhans/p/4701759.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!