?
?其中java文件夹存放以后编写的java文件,resource文件夹存放service、dao、struts的配置文件,sqlmap文件夹存放编程的sqlmap文件,datasource文件夹存放数据源配置的文件。
? ?2.编写web.xml:
?
class="java"><?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>water</display-name> <context-param> <param-name>configuration</param-name> <param-value>WEB-INF/applicationContext*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <listener> <listener-class> org.springframework.web.context.request.RequestContextListener</listener-class> </listener> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:log4j.properties</param-value> </context-param> <listener> <listener-class> org.springframework.web.util.Log4jConfigListener </listener-class> </listener> <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> </filter> <filter-mapping> <filter-name>encodingfilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> </web-app>
?3.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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 导入数据源 --> <import resource="classpath:datasource/spring-datasource.xml"/> <!-- --> <import resource="classpath:com/water3/resource/user/service/spring-user-service.xml"/> <import resource="classpath:com/water3/resource/user/dao/spring-user-dao.xml"/> //以上三个import是位于resource文件夹下配置文件,在此导入总配置文件中 </beans>
?4.配置日志文件log4j.properties:
### set log levels ### log4j.rootLogger = DEBUG, stdout,D ### 输出到控制台 ### log4j.appender.stdout = org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target = System.out log4j.appender.stdout.layout = org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern = %d{ABSOLUTE} %5p %c:%L - %m%n ### 输出到日志文件 ### log4j.appender.D = org.apache.log4j.DailyRollingFileAppender log4j.appender.D.File = logs/log.log log4j.appender.D.Append = true log4j.appender.D.Threshold = DEBUG ## 输出DEBUG级别以上的日志 log4j.appender.D.layout = org.apache.log4j.PatternLayout log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n ### 保存异常信息到单独文件 ### log4j.appender.D = org.apache.log4j.DailyRollingFileAppender log4j.appender.D.File = logs/error.log ## 异常日志文件名 log4j.appender.D.Append = true log4j.appender.D.Threshold = ERROR ## 只输出ERROR级别以上的日志!!! log4j.appender.D.layout = org.apache.log4j.PatternLayout log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
?5.struts.xml文件引入公共配置:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" "http://struts.apache.org/dtds/struts-2.1.7.dtd"> <struts> <constant name="struts.action.extension" value="do"></constant><!-- 请求action时url以.do结束 --> <constant name="struts.multipart.maxSize" value="166976000" /> <constant name="struts.ui.theme" value="simple" /> <include file="struts-default.xml"></include> <include file="struts_common.xml"></include>//这个文件存放拦截器之类的公共配置 <!-- 导入struts配置 --> </include> </struts>
?6.数据文件配置water3.properties:
test.proxool.driver-class=com.mysql.jdbc.Driver test.proxool.user=root test.proxool.password=root test.proxool.alias=test_proxool test.proxool.driver-url=jdbc:mysql://XXXX:3306/test?characterEncoding=utf-8&useUnicode=true test.proxool.house-keeping-sleep-time=40000 test.proxool.house-keeping-test-sql=SELECT '' test.proxool.maximum-connection-count=20 test.proxool.minimum-connection-count=5 test.proxool.maximum-connection-lifetime=18000000 test.proxool.simultaneous-build-throttle=5 test.proxool.recently-started-threshold=40000 test.proxool.overload-without-refusal-lifetime=50000 test.proxool.maximum-active-time=60000 test.proxool.verbose=true test.proxool.trace=true test.proxool.fatal-sql-exception=Fatal error ##最少连接数 test.proxool.prototype-count=5 test.proxool.statistics-log-level=ERROR
?spring-datasource.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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:property-placeholder location="classpath:datasource/*.properties" /> <bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource"> <property name="driver"> <value>${test.proxool.driver-class}</value> </property> <property name="driverUrl"> <value>${test.proxool.driver-url}</value> </property> <property name="user"> <value>${test.proxool.user}</value> </property> <property name="password"> <value>${test.proxool.password}</value> </property> <property name="alias"> <value>${test.proxool.alias}</value> </property> <property name="maximumActiveTime"> <value>${test.proxool.maximum-active-time}</value> </property> <property name="houseKeepingSleepTime"> <value>${test.proxool.house-keeping-sleep-time}</value> </property> <property name="maximumConnectionCount"> <value>${test.proxool.maximum-connection-count}</value> </property> <property name="minimumConnectionCount"> <value>${test.proxool.minimum-connection-count}</value> </property> <property name="prototypeCount"> <value>${test.proxool.prototype-count}</value> </property> <property name="trace"> <value>${test.proxool.trace}</value> </property> <property name="statisticsLogLevel"> <value>${test.proxool.statistics-log-level}</value> </property> <property name="maximumConnectionLifetime"> <value>${test.proxool.maximum-connection-lifetime}</value> </property> </bean> <!--配置spring事务管理 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"> </property> </bean> <!--配置backendSessionFactory --> <bean id="backendSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource"> <ref local="dataSource"></ref> </property> <property name="configLocation" value="classpath:datasource/water_datasource.xml"></property> <property name="mapperLocations"> <list> <value>classpath*:/sqlmap/**/sqlmap*.xml</value> </list> </property> </bean> </beans>
?此处注意,mybatis需要配置一个别名文件water_datasource.xml,此文件一定要入如下配置。
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0 //EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <typeAliases> </typeAliases> </configuration>
?至此,项目基本搭建完毕,下图为所需的包:
?
?附:拦截器配置:
<?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> <package name="waterInterceptors" extends="struts-default"> <interceptors> <interceptor name="myauthority" class="com.water3.java.interceptor.RequestListenerInsterceptor"> </interceptor> <interceptor-stack name="mydefault"> <interceptor-ref name="defaultStack" /> <interceptor-ref name="myauthority" /> </interceptor-stack> </interceptors> <!-- 定义全局Result --> <global-results> <!-- 当返回login视图名时,转入/login.jsp页面 --> <result name="success">/index.jsp</result> </global-results> </package> </struts> public class RequestListenerInsterceptor extends AbstractInterceptor{ /** * */ private static final long serialVersionUID = 1088113971659489138L; @Override public String intercept(ActionInvocation invocation) throws Exception { ActionContext act = invocation.getInvocationContext(); System.out.println("拦截器测试"); return invocation.invoke(); } }
?公共类:
//dao基类 public class BaseDaoImpl<T, PK extends Serializable> extends SqlSessionTemplate{ public BaseDaoImpl(SqlSessionFactory sqlSessionFactory) { super(sqlSessionFactory); } } // public class ModelDrivenActionSupport<T> extends BaseActionSupport implements ScopedModelDriven<T>{ /** * */ private static final long serialVersionUID = 4711438804682348737L; private T model; private String scope; @Override public T getModel() { return model; } @Override public String getScopeKey() { return scope; } @Override public void setModel(T model) { this.model = model; } @Override public void setScopeKey(String scope) { this.scope = scope; } } // public class BaseActionSupport extends ActionSupport implements SessionAware,ServletRequestAware,ServletResponseAware{ /** * */ private static final long serialVersionUID = -5514461732932831728L; public static final String LIST = "list"; public static final String SAVE = "save"; public static final String NORESULT = "noresult"; private String priMsg; ActionContext context = ActionContext.getContext(); HttpServletRequest request; HttpServletResponse response; @SuppressWarnings("rawtypes") SessionMap session; public String param(String paramName) { return request.getParameter(paramName); } @SuppressWarnings("rawtypes") public void setSession(Map map) { this.session = (SessionMap) map; } public void setServletRequest(HttpServletRequest request) { this.request = request; } public void setServletResponse(HttpServletResponse response) { this.response = response; } public HttpServletRequest getRequest() { return request; } public HttpServletResponse getResponse() { return response; } public SessionMap getSession() { return session; } public String getPriMsg() { return priMsg; } public void setPriMsg(String priMsg) { this.priMsg = priMsg; } }
?