rss· 投稿· 设为首页· 加入收藏· 繁體版
当前位置: 火魔网 » 程序开发 » JavaEE

struts2.18与spring2.5的整合实现(IOC实现)


说一下,以下错误弄了不时间,加上网上版本各有不同: XmlConfigurationProvider这里有一个属性:objectFactory,看是否指向org.apache.struts2.spring.StrutsSpringObjectFactory,若不是 加载失败,将会出包Action class not found的错误(ioc失败),因为默认是使用org.apache.struts2.impl.StrutsObjectFactory加载的, 这里主要是说明如何使用spring的ioc强大功能
web.xml: <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5"  xmlns="http://java.sun.com/xml/ns/javaee"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:/com/ray/config/spring/context.xml </param-value> </context-param>    <listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener>      <welcome-file-list>     <welcome-file>index.jsp</welcome-file>   </welcome-file-list>   <filter>    <filter-name>struts2</filter-name>    <filter-class>    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter    </filter-class>    <init-param>             <param-name>config</param-name>            <!-- 配置装载struts.xml路径,其中struts.xml放在/src/struts/下,正式开发时候,应include到struts.xml中-->             <param-value>struts-default.xml,com/ray/config/struts/struts.xml</param-value>         </init-param>   </filter>   <filter-mapping>    <filter-name>struts2</filter-name>    <url-pattern>/*</url-pattern>   </filter-mapping>      <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>      </web-app>
   struts.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <!--不知为何这句话一定写在这里,很多文档说可以写到struts.properties中写成...=spring, 我试了不知为何不何,那为朋友知道给我信息啦,struts.properties在src目录下, 没这句话,将会用org.apache.struts2.impl.StrutsObjectFactory加载action,就无法使用spring的ioc 使用这话,一定要把struts2-spring-plugin-2.1.8.1.jar放到classpath中 可以使用以下<include file="struts-plugin.xml"/>这句话,替换 <constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" /> 因为在spring插件中已有定义,<include file="struts-plugin.xml"/>这句话会默认加载所有插件的配置,如json之类的 --> <constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" />
    <package name ="Struts2_IoC" extends ="struts-default"> <action name ="chat" class ="chatAction"> <result name="test000">test001/userlist.jsp</result> </action > </package > </struts>    
context.xml: <?xml version="1.0" encoding="UTF-8"?> <beans  default-autowire="byName" xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 <bean id ="chatService" class ="com.ray.spring.bean.ChatServiceImpl"/>   <!--使用prototype,每次调用都会重新生成,这个也是struts2的解决线程共享问题吧(区别struts1(同一个action对象)),也就是说不是单例-->  <bean id ="chatAction" class ="com.ray.spring.action.ChatAction" scope="prototype">   <property name="chatService" ref ="chatService"/>  </bean>     <bean id ="helloAction" class ="com.ray.test.action.TestAction" scope="prototype"/>   </beans>
jsp: <%@ page contentType=" text/html; charset=UTF-8"%>  <%@ taglib prefix = "s" uri="/struts-tags"%>  <html>  <head>      <title > User List </title>  </head> 
<body>      <h2> User List </h2>      <ol>      <s:iterator value ="userNames">          <li><s:property /></li>      </s:iterator >      </ol>  </body>  </html>
service: /**  *   */ package com.ray.spring.bean;
import java.util.Set;
/**  * @author ray.zhang  *  */ public interface ChatService { Set <String> getUserNames(); }
/**  *   */ package com.ray.spring.bean;
import java.util.HashSet; import java.util.Set;
/**  * @author ray.zhang  *   */ public class ChatServiceImpl implements ChatService {
/* * (non-Javadoc) *  * @see com.ray.spring.bean.ChatService#getUserNames() */ public Set<String> getUserNames() { Set<String> users = new HashSet<String>(); users.add(" Max "); users.add(" Scott "); users.add(" Bob "); return users; } action: package com.ray.spring.action;
import java.util.Set;
import com.opensymphony.xwork2.ActionSupport; import com.ray.spring.bean.ChatService;
public class ChatAction extends ActionSupport {
/** *  */ private static final long serialVersionUID = -7015241741492337948L;
private ChatService chatService; private Set<String> userNames;
public void setChatService(ChatService chatService) { this.chatService = chatService; }
public Set<String> getUserNames() { return userNames; }
@Override public String execute() { userNames = chatService.getUserNames(); return "test000"; } jar: struts的基本包(我的是struts2.18); spring的基本包(2.5) struts2-spring-plugin-2.1.8.1.jar,这个是ioc的包
以上参考:http://www.blogjava.net/max/archive/2006/12/28/90548.html
顶一下
(0)
踩一下
(0)