1.struts.xml零配置+我的拦截器,直接CODING吧。
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <!-- 请求参数的编码方式 --> <constant name="struts.i18n.encoding" value="UTF-8" /> <!-- 该属性指定需要Struts 2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts2处理。 如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开。 --> <constant name="struts.action.extension" value="action,,"/> <!-- 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭 --> <constant name="struts.serve.static.browserCache" value="false"/> <!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开 --> <constant name="struts.configuration.xml.reload" value="true"/> <!-- 开发模式下使用,这样可以打印出更详细的错误信息 --> <constant name="struts.devMode" value="true" /> <!-- 是否开启动态方法调用 --> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <!-- 默认的视图主题 --> <constant name="struts.ui.theme" value="simple" /> <!-- 与spring框架整合 --> <constant name="struts.objectFactory" value="spring" /> <!-- 指定允许上传的文件最大字节数。默认值是2097152(2M) --> <constant name="struts.multipart.maxSize" value="2097152"/> <!-- 设置上传文件的临时文件夹,默认使用javax.servlet.context.tempdir --> <!-- <constant name="struts.multipart.saveDir " value="d:/uploads/tmp"/> --> <constant name="struts.convention.result.path" value="/WEB-INF/page/" /> <constant name="struts.custom.i18n.resources" value="globalMessages"/> <package name="myStruts" extends="struts-default"> <interceptors> <interceptor-stack name="myInterceptor"> <interceptor-ref name="fileUpload"> <param name="allowedTypes">image/x-png,image/pjpeg,image/gif,image/jpeg,image/png</param> <param name="maximumSize">2097152</param> </interceptor-ref> <interceptor-ref name="defaultStack"/> </interceptor-stack> </interceptors> <default-interceptor-ref name="myInterceptor"/> </package> </struts>
2.strus2 + spring + jpa 的web.xml配置
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- ===========对Spring容器实例化 start=========== --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- ===========对Spring容器实例化 end============ --> <!-- ===========spring编码过滤器配置 start============ --> <filter> <filter-name>characterEncodingFilter</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> <filter-name>Spring OpenEntityManagerInViewFilter</filter-name> <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>Spring OpenEntityManagerInViewFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <!-- 配置Struts2框架的核心Filter --> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> <!-- 配置Struts2的核心Filter的实现类 --> <!-- <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> --> <!-- 在web.xml中配置常量的方法,但建议不要在该文件中配置常量 <init-param> <param-name>struts.custom.il8n.resources</param-name> <param-value>myMessageResource</param-value> </init-param> --> </filter> <filter-mapping> <filter-name>struts2</filter-name> <!-- 匹配所有请求 --> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
3.JPA的配置文件
在src文件夹下新建文件夹META-INF,再在该文件夹下新建persistence.xml文件。配置内容
<?xml version="1.0" encoding="UTF-8" ?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0"> <!-- name 实体管理器工厂名,用于创建实体管理类对象 EntityManagerFactory factory = Persistence .createEntityManagerFactory("joysport"); transaction-type 事物类型 配置本地化事物,这里我没有闹清楚 --> <persistence-unit name="joysport" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <properties> <!-- MySQL数据库方言 --> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" /> <property name="hibernate.max_fetch_depth" value="3" /> <property name="hibernate.hbm2ddl.auto" value="update" /> <property name="hibernate.jdbc.fetch_size" value="18" /> <property name="hibernate.jdbc.batch_size" value="10" /> <property name="hibernate.show_sql" value="true" /> <property name="hibernate.format_sql" value="true" /> </properties> </persistence-unit> </persistence> <!-- 没有和spring整合时,把下面的配置加入到<properties>里 --> <!-- //MySQL数据库驱动 <property name="hibernate.driverClassName" value="com.mysql.jdbc.Driver"/> //数据库用户名 <property name="hibernate.connection.username" value="root" /> //密码 <property name="hibernate.connection.password" value="root" /> //连接URL <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3308/joysport?useUnicode=true&characterEncoding=UTF-8" /> -->
4.applicationContext.xml配置(基于spring2.5)
<?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" 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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- 注解支持 --> <context:annotation-config /> <!-- 扫描位置 --> <context:component-scan base-package="com" /> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" > <property name="driverClassName" value="com.mysql.jdbc.Driver" /><!-- org.gjt.mm.mysql.Driver --> <property name="url" value="jdbc:mysql://localhost:3308/enjoysport?useUnicode=true&characterEncoding=UTF-8" /><!-- mpros --> <property name="username" value="root" /> <property name="password" value="admin" /> <!-- 连接池启动时的初始值 --> <property name="initialSize" value="1" /> <!-- 连接池的最大值 --> <property name="maxActive" value="500" /> <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 --> <property name="maxIdle" value="2" /> <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 --> <property name="minIdle" value="1" /> </bean> <!-- Spring集成JPA --> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" /> <property name="loadTimeWeaver"> <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" /> </property> </bean> <!-- Spring针对Hibernate的事务管理器 --> <bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean> <!-- 事物注解支持 --> <tx:annotation-driven transaction-manager="txManager" /> </beans>
5.我常用的Action的注解
1.@ParentPackage("struts-default")<--->等价于struts.xml的 extends="struts-default",通常此处可配置拦截器,
即配置@ParentPackage("myStruts"),既可以拦截一下Action了。
2.@Namespace,命名空间
3.@Results({@Result(name="success",loction="index.jsp")})
6.配置返回json数据格式,通过struts自动封装对象,产生json对象。
@ParentPackage("json-default") @Namespace("/control/client/product") @Results({}) .... @Action(value="/getproducts",results={ @Result(name="products", type = "json")}) public String getproducts() { .... }