`
Colin_Davis
  • 浏览: 26037 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

java spring4-servlet.xml 备忘

    博客分类:
  • Java
 
阅读更多

   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>

 

 

分享到:
评论

相关推荐

    SpringMVC项目搭建过程备忘

    在IT行业中,SpringMVC是Java企业级应用开发中广泛使用的Web框架,它属于Spring框架的一部分,用于构建可维护、可扩展的Web应用程序。本文将详细介绍SpringMVC项目的搭建过程,帮助开发者快速入门。 首先,我们需要...

    STRUTS备忘录、

    在Struts中,请求首先由一个ActionServlet拦截,它解析请求并根据配置文件(struts-config.xml)来决定调用哪个Action。Action是业务逻辑的入口点,处理完业务后,返回一个ActionForward对象,指示控制器如何转发...

    EJB备忘(整理)

    【EJB备忘(整理)】 企业级JavaBean(Enterprise JavaBeans,简称EJB)是Java平台企业版(Java EE)的一部分,它提供了一种标准的框架,用于开发和部署可扩展、安全、事务处理以及分布式的企业级应用程序。EJB是...

    JAVA软件工程师简历模版.doc

    - **技术栈**: J2EE平台下的B/S模式开发,使用Struts, Hibernate, Spring, Ajax, XML等技术,采用四层架构:Web层、控制转发层、服务层、DAO层。 - **个人贡献**: 参与文档编写、用户管理和日志管理模块开发,运用...

    实习或秋招校招-思维导图总结

    - Servlet配置:在web.xml中的配置,以及注解方式的配置。 - Filter和Listener:过滤器的拦截功能和监听器的事件监听。 10. **Java内存模型.xmind** - Java内存模型(JMM):理解主内存和工作内存的概念,以及 ...

    JAVA技术体系

    Java 的核心技术包括 Java Standard Edition (Java SE),Java Enterprise Edition (Java EE),以及 Java Micro Edition (Java ME)。本文将围绕 Java EE 的技术栈展开,深入探讨 Java 开发的不同阶段所需掌握的技术及...

    JDK中有关23个经典设计模式的示例

    在Web开发中,`javax.servlet.DispatcherServlet`作为Spring MVC框架的前端控制器。 23. 代理模式(Proxy):在JDK动态代理章节已经提到,这里不再赘述。 以上是JDK中23个经典设计模式的基本介绍和应用示例,每个...

    java面试汇总

    Java中的23种设计模式包括:工厂模式(简单工厂、工厂方法、抽象工厂)、单例模式、建造者模式、原型模式、适配器模式、装饰器模式、外观模式、享元模式、组合模式、桥接模式、职责链模式、命令模式、解释器模式、...

    进阶篇.pdf

    在服务器端,Servlet的生命周期、线程安全问题、Filter和Listener的使用,以及web.xml的配置都是Web应用开发的关键。Hibernate作为ORM框架,提供了对象关系映射、缓存机制和懒加载功能。MyBatis则允许更灵活的SQL...

Global site tag (gtag.js) - Google Analytics