目录结构:
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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 对静态文件的拦截处理方法 --> <!-- <mvc:default-servlet-handler /> --> <!-- 启用spring mvc 注解 --> <context:annotation-config /> <!-- 设置使用注解的类所在的jar包 --> <context:component-scan base-package="com.geostar.gfstack.operationcenter.*.action,com.geostar.gfstack.operationcenter.*.*.action,com.geostar.gfstack.operationcenter.*.*.*.action,com.geostar.gfstack.operationcenter.*.*.*.*.action"></context:component-scan> <!-- 自动注入处理器映射器和适配器 和springmvc拦截器--> <mvc:annotation-driven></mvc:annotation-driven> <!-- 完成请求和注解POJO的映射 <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> --> <!-- 对转向页面的路径解析。prefix:前缀, suffix:后缀 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/platform/" /> <property name="suffix" value=".jsp" /> </bean> <!-- 上传文件MultipartResolver处理器 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="UTF-8"></property> <property name="maxUploadSize" value="1024000"></property> </bean> </beans>
applicationContext-database.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName"> <value>${jdbc.driver}</value> </property> <property name="url"> <value>${jdbc.url}</value> </property> <property name="username"> <value>${jdbc.username}</value> </property> <property name="password"> <value>${jdbc.password}</value> </property> <property name="maxActive"> <value>${jdbc.maxActive}</value> </property> <property name="maxIdle"> <value>${jdbc.maxIdle}</value> </property> <property name="maxWait"> <value>${jdbc.maxWait}</value> </property> <property name="logAbandoned"> <value>${jdbc.logAbandoned}</value> </property> <property name="removeAbandoned"> <value>${jdbc.removeAbandoned}</value> </property> <property name="removeAbandonedTimeout"> <value>${jdbc.removeAbandonedTimeout}</value> </property> <property name="testWhileIdle"> <value>${jdbc.testWhileIdle}</value> </property> <property name="timeBetweenEvictionRunsMillis"> <value>${jdbc.timeBetweenEvictionRunsMillis}</value> </property> <property name="validationQuery"> <value>${jdbc.validationQuery}</value> </property> </bean> <!-- Hibernate配置 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource"/> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> ${hibernate.dialect} </prop> <prop key="hibernate.show_sql"> ${hibernate.show_sql} </prop> <prop key="hibernate.query.factory_class"> ${hibernate.query.factory_class} </prop> <prop key="current_session_context_class"> ${current_session_context_class} </prop> <prop key="hibernate.hbm2ddl.auto"> update </prop> </props> </property> <property name="packagesToScan"> <list> <!-- 添加解析XML的扫描路径 --> <value>com.geostar.gfstack.operationcenter.simple.model</value> </list> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> <property name="globalRollbackOnParticipationFailure" value="false"/> </bean> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="save*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="use*" propagation="REQUIRED"/> <tx:method name="freed*" propagation="REQUIRED"/> <tx:method name="*" propagation="REQUIRED" read-only="true"/> </tx:attributes> </tx:advice> <aop:config> <aop:advisor pointcut="execution(* com.geostar.gfstack.operationcenter.*.service.*Service.*(..))" advice-ref="txAdvice"/> <aop:advisor pointcut="execution(* com.geostar.gfstack.operationcenter.*.*.service.*Service.*(..))" advice-ref="txAdvice"/> <aop:advisor pointcut="execution(* com.geostar.gfstack.operationcenter.*.*.*.service.*Service.*(..))" advice-ref="txAdvice"/> <aop:advisor pointcut="execution(* com.geostar.gfstack.operationcenter.*.*.*.*.service.*Service.*(..))" advice-ref="txAdvice"/> </aop:config> </beans>
applicationContext.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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.geostar.gfstack.operationcenter"/> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:config/GeoStar.properties</value> </list> </property> </bean> <import resource="classpath:config/spring/applicationContext-database.xml"/> </beans>
GeoStar.properties
#MySQL|Oracle #jdbc.db.type=Oracle jdbc.db.type=Mysql #apache dbcp jdbc jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc\:mysql\://db-exte.gfstack.geo\:3306/operationcenter jdbc.username=gfstack jdbc.password=gfstack jdbc.maxActive=10 jdbc.maxIdle=3 jdbc.maxWait=500 jdbc.logAbandoned=false jdbc.removeAbandoned=true jdbc.removeAbandonedTimeout=5000 jdbc.timeBetweenEvictionRunsMillis=120000 jdbc.testWhileIdle=true jdbc.validationQuery=select 1 from dual hibernate.dialect=org.hibernate.dialect.MySQL5Dialect hibernate.show_sql=false hibernate.query.factory_class=org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory current_session_context_class=thread
log4j.properties
log4j.rootLogger=INFO,CONSOLE,APPLOG log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender log4j.appender.CONSOLE.Target=System.out log4j.appender.CONSOLE.Threshold=INFO log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p [%t] %c %x - %m%n log4j.appender.APPLOG=org.apache.log4j.DailyRollingFileAppender log4j.appender.APPLOG.File=${catalina.home}/logs/geostack-simple.log log4j.appender.APPLOG.DatePattern=‘.‘yyyy-MM-dd log4j.appender.APPLOG.Append=true log4j.appender.APPLOG.layout=org.apache.log4j.PatternLayout log4j.appender.APPLOG.layout.ConversionPattern=[%d{yyyy-MM-dd hh:mm:ss}]-[%5p] [%l] %m%n log4j.logger.com.geostar.gfstack=ALL,CONSOLE,APPLOG log4j.logger.com.geostar.geostack=ALL,CONSOLE,APPLOG log4j.additivity.com.geostar.gfstack=false log4j.additivity.com.geostar.geostack=false
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" 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>geostack-dispatch</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:config/spring/applicationContext.xml</param-value> </context-param> <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> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:config/spring/spring-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
SimpleAction.java
package com.geostar.gfstack.operationcenter.simple.action; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; @Controller @RequestMapping("simpleAction") public class SimpleAction { @ResponseBody @RequestMapping("test") public void test(){ HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse(); response.setCharacterEncoding("UTF-8"); response.setContentType("text/html"); response.setHeader("Content-type", "text/html;charset=utf-8"); response.setCharacterEncoding("UTF-8"); response.setContentType("text/html"); response.setHeader("Access-Control-Allow-Origin", "*"); PrintWriter write = null; try { write = response.getWriter(); } catch (IOException e) { e.printStackTrace(); } write.print("test"); } }
时间: 2024-10-10 20:50:24