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.aygfsteel.com/zhyiwww   
          |----------------------------------------------------------------------------------------|
          posted on 2007-07-05 18:27 zhyiwww 閱讀(1167) 評論(0)  編輯  收藏 所屬分類: j2ee
          主站蜘蛛池模板: 会东县| 永安市| 东乌珠穆沁旗| 景宁| 天镇县| 定西市| 翁源县| 婺源县| 皮山县| 徐汇区| 化州市| 独山县| 富宁县| 石嘴山市| 时尚| 阳高县| 定兴县| 长兴县| 龙南县| 乾安县| 呼和浩特市| 新津县| 辉县市| 宁国市| 湖州市| 三原县| 昌邑市| 西乌珠穆沁旗| 鄂伦春自治旗| 河东区| 大新县| 鲜城| 鹿邑县| 灵武市| 金乡县| 山丹县| 汪清县| 茌平县| 海原县| 延川县| 棋牌|