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/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
http://www.springframework.org/schema/mvc
http://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="com.mysql.jdbc.Driver"/>
	<property name="url" value="jdbc:mysql://localhost:3306/test"/>
	<property name="username" value="root"/>
	<property name="password" value="123456"/>
</bean>

<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
	<property name="configLocation" value="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="spring"/>
	<constant name="struts.action.extension" value="action"/>
	<constant name="struts.devMode" value="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="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方法

 

spring+struts2+ibatis整合,布布扣,bubuko.com

spring+struts2+ibatis整合

上一篇:玩转VC++ 实现Wifi on与wifi off


下一篇:(原)python爬虫入门(2)---排序爬取的辽宁科技大学热点新闻