一,下载struct 2 : http://struts.apache.org/download.cgi#struts2315
Structs-2.3.15.1-all.zip: Full Distribution
须要的包有:
commons-fileupload-1.3.jar,commons-io-2.0.1.jar,
commons-lang3-3.1.jar,freemarker-2.3.19.jar,javassist-3.11.0.GA.jar,
ognl-3.0.6.jar,struts2-core-2.3.15.1.jar, xwork-core-2.3.15.1.jar
如果加入spring还须要struts2-spring-plugin-2.0.jar,
通过myeclipse导入spring时如图: spring2.5
你须要在web.xml中添加:
<!-- spring 配置 --> <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>
<!-- struts2配置 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
现在你的struts 2+spring 就可以添加配置文件了
Struts2中添加spring支持: <constantname="struts.objectFactory"value="spring"/>
Struts2 的配置其实是系统的过滤器的配置,struts2在这里是过滤作用,依据Http协议的协议头跳到相应的配置页面中。
有的是 struts2 配置的类是:org.apache.struts2.dispatcher.FilterDispatcher 所以 注意 与 StrutsPrepareAndExecuteFilter的区别
以后,就可以在spring中的applicationContext.xml中添加bean配置类了.如下
applicationContext.xml
<bean id="loginAction" class="com.alphavor.kjh.action.LoginAction" scope="prototype" />
以上就是登录实例的Action:对于多用户登录时你必须配置spring的scope属性为prototype.struts.xml
<action name="login" class="loginAction">
<result name="input">/login.jsp</result>
<result name="error">/login.jsp</result><!-- 系统管理员 -->
<result name="success">index.action</result><!-- 普通查看员 -->
<result name="view">view.action</result>
</action>
这样即刻.
则你在网页上提交时就 http://localhost/project/tologin.Action?user=??&pass=***即可。
当然,这里没有使用自己的spring,而是使用myeclipse自己的spring.
以后就须要添加数据库操作了,比如: Hibernate等等.使用spring初始化c3p0即可.
二, Spring 配置数据库连接:
<!-- 使用jdbc.properties配置文件 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties" />
</bean>
<!-- 数据源配置开始 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass">
<value>${datasource.driverClassName}</value>
</property>
<property name="jdbcUrl">
<value>${datasource.url}</value>
</property>
<property name="user">
<value>${datasource.username}</value>
</property>
<property name="password">
<value>${datasource.password}</value>
</property>
<property name="acquireIncrement">
<value>${c3p0.acquireIncrement}</value>
</property>
<property name="initialPoolSize">
<value>${c3p0.initialPoolSize}</value>
</property>
<property name="minPoolSize">
<value>${c3p0.minPoolSize}</value>
</property>
<property name="maxPoolSize">
<value>${c3p0.maxPoolSize}</value>
</property>
<property name="maxIdleTime">
<value>${c3p0.maxIdleTime}</value>
</property>
<property name="idleConnectionTestPeriod">
<value>${c3p0.idleConnectionTestPeriod}</value>
</property>
<property name="maxStatements">
<value>${c3p0.maxStatements}</value>
</property>
<property name="numHelperThreads">
<value>${c3p0.numHelperThreads}</value>
</property>
</bean>
三、加入Log日志
<!-- Log4j配置 -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/logConfig.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
logConfig.properties:
这个就是你的
log4j
的配置文件了。
四、你的数据库管理了