java spring4-servlet.xml 备忘

   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:c="http://www.springframework.org/schema/c" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
		http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
		http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
		">

	<context:annotation-config />

	<context:component-scan base-package="***.actions" />

	<!-- <mvc:annotation-driven /> -->

	<!-- 映射静态资源URL -->
	<mvc:resources mapping="/styles/**" location="/styles/" />
	<mvc:resources mapping="/scripts/**" location="/scripts/" />
	<mvc:resources mapping="/resources/**" location="/resources/" />

	<mvc:interceptors>
		<!-- 全局 拦截器/过滤器 -->
		<mvc:interceptor>
			<mvc:mapping path="/**" />
			<!-- path=/** 为过滤所有请求, so 此处需要忽略掉静态资源URL -->
			<mvc:exclude-mapping path="/styles/**" />
			<mvc:exclude-mapping path="/scripts/**" />
			<mvc:exclude-mapping path="/resources/**" />
			<bean class="***.adapter.PrivilegeHandlerInterceptorAdapter" />
		</mvc:interceptor>
		<!-- 当前拦截器主要过滤静态资源,可以不要 -->
		<mvc:interceptor>
			<mvc:mapping path="/styles/**" />
			<mvc:mapping path="/scripts/**" />
			<mvc:mapping path="/resources/**" />
			<bean class="***.adapter.ResourceHandlerInterceptorAdapter" />
		</mvc:interceptor>
	</mvc:interceptors>

	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/" />
		<property name="suffix" value=".jsp" />
	</bean>

	<!-- spring4.* 和 3.* 貌似改用 RequestMappingHandlerMapping -->
	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
		<property name="useSuffixPatternMatch" value="false" />
		<property name="useRegisteredSuffixPatternMatch" value="false" />
		<property name="useTrailingSlashMatch" value="true" />
	</bean>

	<!-- spring4.* 和 3.* 貌似改用 RequestMappingHandlerAdapter -->
	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
		<property name="cacheSeconds" value="0" />
		<property name="messageConverters">
			<list>
				<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>
			</list>
		</property>
	</bean>

	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="maxUploadSize" value="524288000"></property>
	</bean>

</beans>

 

   PrivilegeHandlerInterceptorAdapter.java

/**
 * 
 */
package ***.adapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

/**
 * @author Colin
 *
 */
public class PrivilegeHandlerInterceptorAdapter extends HandlerInterceptorAdapter { 

	/**
	 * 
	 */
	public PrivilegeHandlerInterceptorAdapter() {
		// TODO Auto-generated constructor stub
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.web.servlet.handler.HandlerInterceptorAdapter#preHandle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object)
	 */
	@Override
	public boolean preHandle( HttpServletRequest request, HttpServletResponse response, Object handler ) throws Exception {

		System.out.println( request.getRequestURI() );
		System.out.println( request.getRequestURL() );

		// ** 匹配到需要拦截的 URL    静态资源为 instanceof ResourceHttpRequestHandler
		if ( handler instanceof HandlerMethod ) {

			HandlerMethod method = ( HandlerMethod ) handler;
			String className = method.getBean().getClass().getName(); // ** Controller 的类名
			String methodName = method.getMethod().getName(); // ** Controller 里执行的方法名称

		}

		return super.preHandle( request, response, handler );
	}

}

 

 

   context.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:c="http://www.springframework.org/schema/c" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
		http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
			http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
			http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
		http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
		">

	<context:property-placeholder location="classpath:config/datasource-mysql.properties" />
	
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" p:driverClassName="${jdbc.driverClassName}" p:url="${mysql.url}" p:username="${mysql.username}" p:password="${mysql.password}" />

	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource" />

	<tx:annotation-driven transaction-manager="transactionManager" />

	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="transactionFactory">
			<bean class="org.mybatis.spring.transaction.SpringManagedTransactionFactory" />
		</property>
		<property name="mapperLocations">
			<list>
				<value>classpath:config/mybatis/*/Dal-*.xml</value>
			</list>
		</property>
	</bean>

	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" p:sqlSessionFactoryBeanName="sqlSessionFactory" p:basePackage="com.defence.chat.meetya.dal" />

	<!-- 应用程序包, 要扫描的根目录包名 -->
	<context:component-scan base-package="***">
		<!-- 跳过扫描的 Controllers 所在的包 -->
		<context:exclude-filter type="regex" expression="***.act" />
	</context:component-scan>

</beans>

 

 

你可能感兴趣的:(java spring4-servlet.xml 备忘)