zhyiwww
用平实的笔,记录编程路上的点点滴滴………
posts - 536,comments - 394,trackbacks - 0
已经有很多人写过文章,来介绍和说明这个问题了。
我在学习和使用之后,就也作了一下总结。
第一步:
配置和struts环境

第二步:
载入spring包
第三步:
在struts-config.xml中作spring插件配置,代码如下:

  <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
    <set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml" />
  </plug-in>
 
第四步:
在action中使用spring.

要使用spring,首先就要取得spring的控制权,也就是取得ApplicationContext。

首先,如果我们了解了插件初始化的结果的话,那么我们看:
java.util.Enumeration e = this.getServlet().getServletContext().getAttributeNames();
       
        while(e.hasMoreElements()){
            String s = (String)e.nextElement();
           
            System.out.println(s);
        }
       
这一段程序将显示所有的初始化的参数变量,显示如下:
org.apache.catalina.jsp_classpath
org.apache.struts.action.MESSAGE
org.apache.struts.util.PREFIXES
org.springframework.web.struts.ContextLoaderPlugIn.CONTEXT.
org.apache.catalina.WELCOME_FILES
org.apache.struts.action.ACTION_SERVLET
org.apache.struts.action.FORWARDS
org.apache.struts.action.REQUEST_PROCESSOR
javax.servlet.context.tempdir
org.apache.struts.action.MAPPINGS
org.apache.struts.action.SERVLET_MAPPING
org.apache.struts.action.FORM_BEANS
org.apache.struts.action.PLUG_INS
org.apache.catalina.resources
org.apache.struts.action.MODULE

蓝色的部分就是spring初始化后的结果,也就是,spring初始化后的结果WebApplicationContext保存在
org.springframework.web.struts.ContextLoaderPlugIn.CONTEXT.
变量里面。那么我们就可以使用如下的方式来取得此对象:
        ApplicationContext ctx = (ApplicationContext)this.getServlet().getServletContext().getAttribute("org.springframework.web.struts.ContextLoaderPlugIn.CONTEXT.");
        System.out.println("ctx  is  :  " + ctx);

还有一个方法,就是使用spring给我们提供的接口,我们在构建action继承自ActionSuport,那么,我们就可以通过下面的方法来访问:
        ApplicationContext ctx = this.getWebApplicationContext();
        System.out.println("the application context is :  " + ctx);

接下来,我们就可以测试一下,我们的spring.
先创建一个bean:
package org.zy.pro.demo.sd.bean;

public class User {

    private String username;

    private String password;

    public String getPassword() {
        return password;
    }

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

    public String getUsername() {
        return username;
    }

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

}
然后再spring的配置文件里面配置,如下:
<?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="user" class="org.zy.pro.demo.sd.bean.User">
        <property name="username">
            <value>everybody</value>
        </property>
       
        <property name="password">
            <value>everybodypassword</value>
        </property>
    </bean>

</beans>

好了,调用一下吧:

创建一个action,

package org.zy.pro.demo.sd.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.FileSystemResourceLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.GenericWebApplicationContext;
import org.springframework.web.context.support.ServletContextResourceLoader;
import org.springframework.web.struts.ActionSupport;
import org.zy.pro.demo.sd.bean.User;

public class Bus extends ActionSupport {

    @Override
    public ActionForward execute(ActionMapping arg0, ActionForm arg1, HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
        // TODO Auto-generated method stub
       
        ApplicationContext ctx = this.getWebApplicationContext();
        System.out.println("the application context is :  " + ctx);
       
        User user = (User) ctx.getBean("user");
        System.out.println("username is : "+user.getUsername());
   
        return super.execute(arg0, arg1, arg2, arg3);
    }

}

然后再struts-config.xml文件里面配置action如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">

<struts-config>
  <data-sources />
  <form-beans />
  <global-exceptions />
  <global-forwards />
  <action-mappings >
    <action path="/map" type="org.zy.pro.demo.sd.action.MapAction" />
    <action path="/bus" type="org.zy.pro.demo.sd.action.Bus" /> 
  </action-mappings>
 
  <message-resources parameter="org.zy.pro.demo.sd.ApplicationResources" />
 
  <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
    <set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml" />
  </plug-in>
 
</struts-config>

好了,发布,测试一下,结果如下:

the application context is :  org.springframework.web.context.support.XmlWebAppl
icationContext: display name [WebApplicationContext for namespace 'action-servle
t']; startup date [Thu Jul 05 18:23:56 CST 2007]; root of context hierarchy; con
fig locations [/WEB-INF/applicationContext.xml]
username is : everybody




|----------------------------------------------------------------------------------------|
                           版权声明  版权所有 @zhyiwww
            引用请注明来源 http://www.blogjava.net/zhyiwww   
|----------------------------------------------------------------------------------------|
posted on 2007-07-05 18:27 zhyiwww 阅读(1148) 评论(0)  编辑  收藏 所属分类: j2ee

只有注册用户登录后才能发表评论。


网站导航: