Struts_spring整合
第一步:建立WEB工程Spring_Struts2
第二步:
①导入struts2支持的JAR包:将
struts-2.1.8.1-all\struts-2.1.8.1\apps\struts2-blank-2.1.8.1\WEB-INF\lib下的所有JAR包(6个jar包,是struts必要的包)拷入到工程目录下的WEB-INF/lib目录下,如果没有看到你拷入的JAR包需要强制加载(这一步指的是如果包导入到lib下后仍然存在WEB-INF/lib下而没有在工程下出现Referenced Liberaries这一项则需要选中lib文件夹,单击右键,BuildPath,Configure Build Path,在弹出的对话框中选择Order and Export,Select All,确定)。
②此外还需要struts2-spring-plugin-2.1.8.1.jar支持包,整合spring与struts2所必需的JAR包,它位于struts-2.1.8.1-all\struts-2.1.8.1\lib目录下
第三步:拷贝配置文件web.xml,struts.xml,它们分别位于路径:
①struts-2.1.8.1-all\struts-2.1.8.1\apps\struts2-blank-2.1.8.1\WEB-INF(web.xml所在位置)拷贝Web.xml文件到工程的Web-INF路径下。Web-INF路径下是受保护的,不会被访问到,保证数据的安全。
②struts-2.1.8.1-all\struts-2.1.8.1\apps\struts2-blank-2.1.8.1\WEB-INF\classes(struts.xml所在位置)拷贝struts.xml文件到工程的src文件下并删除<Struts>标签内的内容,它用于配置action。内部有一个包含文件<includefile="com/ibm/struts/conf/struts_user.xml"/>,这段代码表示,struts.xml中的包含文件是com/ibm/struts/conf包下的struts_user.xml。
第四步:配置struts.xml文件
解决乱码问题:<constant name="struts.i18n.encoding"value="gbk"></constant>
第五步:导入spring支持JAR包,具体过程如下:
修改spring配置文件applicationContext.xml头部信息如下:
<beansxmlns="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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
</beans>
请参考
spring-framework-2.5.6-with-dependencies\spring-framework-2.5.6\samples\jpetstore\war\WEB-INF下的applicationContext.xml文件头部信息
第六步:包含其它的spring配置文件
<import resource="applicationContext-common.xml"/>
<importresource="applicationContext-action.xml"/>
<import resource="applicationContext-dao.xml"/>
第七步:完善web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4"xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!-- 配置上下文参数,加载applicationContext.xml文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<!-- 配置struts2过滤器StrutsPrepareAndExecuteFilter -->
<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>/*</url-pattern>
</filter-mapping>
<!-- 配置加载spring配置文件的监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置web程序的欢迎页面 -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
第八步:书写UserAction.java与实体类Users.java文件,都要有注释,如:
/**
* @作者
* @版本1.0
* @说明
* @参数
* @返回值
* @完成时间
* @更新时间
* @抛出异常
*/
UserAction.java 中是增删改查等操作。Users.java中是简单javaBean,即pojo。
第九步:配置struts_user.xml文件,如:
<?xml version="1.0"encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="users1"extends="struts-default">
<!-- 用户登录功能 -->
<actionname="login" class="userAction"method="login">
<resultname="success"type="dispatcher">/manager.jsp</result>
</action>
<!-- 用户登录功能 -->
<actionname="addUser" class="userAction"method="addUser">
<resultname="success"type="dispatcher">/manager.jsp</result>
</action>
<!-- 用户登录功能 -->
<actionname="updateUser" class="userAction" method="updateUser">
<resultname="success"type="dispatcher">/manager.jsp</result>
</action>
<!-- 用户登录功能 -->
<actionname="deleteUser" class="userAction"method="deleteUser">
<resultname="success"type="dispatcher">/manager.jsp</result>
</action>
</package>
</struts>
第十步:修改页面的name属性,获得属性的方法是user.username,user.password等
注意:
当涉及到js验证时,需要用到id这一属性
<td width="67%" height="22"align="left" ><input name="isadminhelp"type="checkbox" onClick="javascript:adminChecked();"></td>
<input type="hidden" id="isadmin"name="user.isadmin" value="否">