`
whp0731
  • 浏览: 170599 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Sturt2+spring整合

阅读更多

一、相应的spring的jar
Struts2-spring-plugin.jar
spring.jar

1、了解Struts2-spring-plugin.jar包中的struts-plugin.xml配置文件

<struts>
    <bean type="com.opensymphony.xwork2.ObjectFactory" name="spring" class="org.apache.struts2.spring.StrutsSpringObjectFactory" />
    
    <!--  Make the Spring object factory the automatic default -->
    <constant name="struts.objectFactory" value="spring" />

    <package name="spring-default">
        <interceptors>
            <interceptor name="autowiring" class="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor"/>
            <interceptor name="sessionAutowiring" class="org.apache.struts2.spring.interceptor.SessionContextAutowiringInterceptor"/>
        </interceptors>
    </package>    
</struts>

 

2、按如下步骤
1)将Struts2-spring-plugin.jar拷贝到lib下
2)增加配置文件applicationContex.xml(可以如下获得:利用myeclipse自动增加的功能-->在工程上点击右键选择“Add Spring Capabilities”,然后选择spring2.0选择spring2.0 core Libraries,选择 Copy check Library contents to project folder)

 

3、官方参考资料
http://struts.apache.org/2.1.6/index.html页面上的plugin图标
进入到http://cwiki.apache.org/S2PLUGINS/spring-plugin.html

http://struts.apache.org/2.x/docs/spring-plugin.html

 

二、示例
1、建立业务逻辑包service和包serviceImp
他们分别内涵LoginService和LoginServiceImp

package com.test.service;
public interface LoginService
{
 public boolean isLogin(String username, String password);
}



package com.test.service.impl;
import com.test.service.LoginService;
public class LoginServiceImpl implements LoginService
{
 public boolean isLogin(String username, String password)
 {
  if ("hello".equals(username) && "world".equals(password))
  {
   return true;
  }

  return false;
 }

}

 

2、LoginAction.java
(该类加入了LoginService接口,通过spring来注入该服务,)

import com.opensymphony.xwork2.ActionSupport;
import com.test.service.LoginService;
public class LoginAction extends ActionSupport
{
 private String username;
 private String password;

 private LoginService loginService;

 public String getUsername()
 {
  return username;
 }

 public void setUsername(String username)
 {
  this.username = username;
 }

 public String getPassword()
 {
  return password;
 }

 public void setPassword(String password)
 {
  this.password = password;
 }

 public void setLoginService(LoginService loginService)
 {
  this.loginService = loginService;
 }

 @SuppressWarnings("unchecked")
 public String execute() throws Exception
 {
  if (loginService.isLogin(username, password))
  {
   return SUCCESS;
  }
  else
  {
   return INPUT;
  }
        }
}

 

3、spring配置文件
(下面scope="prototype",表示请求一个实例化一个,非单例的,线程安全的!)

<?xml version="1.0" encoding="UTF-8"?>
<beans
 xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="loginService" class="com.test.service.impl.LoginServiceImpl"></bean>
<bean id="loginAction" class="com.test.action.LoginAction" scope="prototype">
 <property name="loginService">
  <ref local="loginService"/>
 </property>
</bean>
</beans>

 
4、在原来的struts.xml文件修改(此处的class名字"loginAction"需要和上面spring配置中的id的"loginAction"一致)

<action name="login" class="loginAction">
   <result name="success">/result.jsp</result>
   <result name="input">/login2.jsp</result>
</action>

 

5、让spring容器在启动时加载进来
在web.xml可以通过servert或监听器两种方式来加载
现在的servert2.4以后版本一般可以用监听器比较简单:<listener>
在web.xml文件中加入以下内容:

 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>

 
这样系统就会默认加载web-INF下  的applicationContext.xml文件了
但是如果spring有多个配置文件需要加载
处理方法:增加以下配置

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>多个配置文件名(中间用逗号隔开)</param-value>
</context-param>

 

6、此时可以看到struts2的加载顺序,他会先加载strust-defualt.xml,接着会加载struts-plugin.xml,组后加载struct.xml
后加载的配置信息可以覆盖前面加载的配置信息。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics