首页 > 代码库 > spring+struts2+ibatis整合
spring+struts2+ibatis整合
与spring+springmvc+ibatis相比,只需作相应的修改即可完成框架的整合。具体配置如下:
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" 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>SSI</display-name> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <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> </filter-mapping> </web-app>
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:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"><context:component-scan base-package="com.qs"/><mvc:annotation-driven/><mvc:default-servlet-handler/><bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="http://www.mamicode.com/com.mysql.jdbc.Driver"/> <property name="url" value="http://www.mamicode.com/jdbc:mysql://localhost:3306/test"/> <property name="username" value="http://www.mamicode.com/root"/> <property name="password" value="http://www.mamicode.com/123456"/></bean><bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="configLocation" value="http://www.mamicode.com/classpath:SqlMapConfig.xml"/> <property name="dataSource" ref="myDataSource"/></bean><bean name="baseAction" class="com.qs.action.BaseAction" scope="prototype"></bean><bean name="userAction" class="com.qs.action.UserAction" scope="prototype"></bean></beans>
struts.xml
<?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.objectFactory" value="http://www.mamicode.com/spring"/> <constant name="struts.action.extension" value="http://www.mamicode.com/action"/> <constant name="struts.devMode" value="http://www.mamicode.com/true" /> <package name="user" extends="struts-default" namespace="/user"> <action name="user_*" class="userAction" method="{1}"> <result name="SUCCESS">/index.jsp</result> </action> </package></struts>
使用<constant name=
"struts.objectFactory"
value=http://www.mamicode.com/
"spring"
/>
即将struts交给spring进行管理,需导入struts2-spring-plugin-2.3.16.3.jar
在整合过程中,针对struts2有如下几点需注意:
1、宜建BaseAction让所有action类继承,BaseAction用来提供一些仅有的属性及方法。
BaseAction
public class BaseAction extends ActionSupport implements SessionAware,ServletRequestAware,ServletResponseAware,ServletContextAware{ /** * */ private static final long serialVersionUID = 2861696572454127047L; private ServletContext context; private HttpServletRequest request; private HttpServletResponse response; private Map<String, Object> session; @Override public void setServletContext(ServletContext context) { this.context=context; } @Override public void setServletResponse(HttpServletResponse response) { this.response=response; } @Override public void setServletRequest(HttpServletRequest request) { this.request=request; } @Override public void setSession(Map<String, Object> session) { this.session=session; } public ApplicationContext getApplicationContext(){ return WebApplicationContextUtils.getWebApplicationContext(context); } public Object getObject(String beanName){ return getApplicationContext().getBean(beanName); }}
2、struts的配置文件命名需符合规范
struts-default.xml
struts-plugin.xml
struts.xml
struts.properties
web.xml
越往后,级别越高,即:前面配置和后面的相同时,后面的配置文件会把前面的覆盖掉。
3、<result>中跳转页面的路径
<result name="SUCCESS">/index.jsp</result>
/index.jsp表示的路径为:/AppName/WebContent/index.jsp
4、struts2与jsp页面的值传递
传递方式为在action定义类的属性及相应的set方法
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。