<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    zhyiwww
    用平實的筆,記錄編程路上的點點滴滴………
    posts - 536,comments - 394,trackbacks - 0
    已經(jīng)有很多人寫過文章,來介紹和說明這個問題了。
    我在學(xué)習(xí)和使用之后,就也作了一下總結(jié)。
    第一步:
    配置和struts環(huán)境

    第二步:
    載入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的控制權(quán),也就是取得ApplicationContext。

    首先,如果我們了解了插件初始化的結(jié)果的話,那么我們看:
    java.util.Enumeration e = this.getServlet().getServletContext().getAttributeNames();
    ??? ???
    ??? ??? while(e.hasMoreElements()){
    ??? ??? ??? String s = (String)e.nextElement();
    ??? ??? ???
    ??? ??? ??? System.out.println(s);
    ??? ??? }
    ??? ???
    這一段程序?qū)@示所有的初始化的參數(shù)變量,顯示如下:
    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初始化后的結(jié)果,也就是,spring初始化后的結(jié)果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給我們提供的接口,我們在構(gòu)建action繼承自ActionSuport,那么,我們就可以通過下面的方法來訪問:
    ??? ??? ApplicationContext ctx = this.getWebApplicationContext();
    ??? ??? System.out.println("the application context is :? " + ctx);

    接下來,我們就可以測試一下,我們的spring.
    先創(chuàng)建一個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>

    好了,調(diào)用一下吧:

    創(chuàng)建一個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>

    好了,發(fā)布,測試一下,結(jié)果如下:

    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




    |----------------------------------------------------------------------------------------|
                               版權(quán)聲明  版權(quán)所有 @zhyiwww
                引用請注明來源 http://m.tkk7.com/zhyiwww   
    |----------------------------------------------------------------------------------------|
    posted on 2007-07-05 18:27 zhyiwww 閱讀(1165) 評論(0)  編輯  收藏 所屬分類: j2ee
    主站蜘蛛池模板: 免费国产美女爽到喷出水来视频| 国产亚洲日韩一区二区三区| 亚洲成av人片在线天堂无| 免费的一级黄色片| a级毛片免费网站| 亚洲第一二三四区| 国产一级淫片a视频免费观看| 99精品视频在线观看免费| 亚洲国产中文在线视频| 亚洲A∨午夜成人片精品网站| 无码精品国产一区二区三区免费 | 午夜一级免费视频| 国产在线精品观看免费观看| 亚洲综合久久成人69| 国产一精品一aⅴ一免费| 日本免费污片中国特一级| 亚洲JIZZJIZZ妇女| 亚洲天天在线日亚洲洲精| 日本免费一区尤物| 最近免费中文字幕mv电影| 免费视频成人国产精品网站| 日木av无码专区亚洲av毛片| 免费毛片网站在线观看| 久久久久国产精品免费免费不卡 | 亚洲精品国产suv一区88| 亚洲av永久无码精品秋霞电影影院 | 猫咪www免费人成网站| 久久久久亚洲AV无码专区首JN | 亚洲综合久久1区2区3区| 亚洲成av人片一区二区三区| 又大又硬又爽又粗又快的视频免费| 美女被免费网站视频在线| 亚洲乱码在线视频| 亚洲av无码乱码国产精品fc2 | 亚洲国产精品网站久久| 亚洲精品午夜无码专区| 国产国产成年年人免费看片| av无码免费一区二区三区| 日本视频在线观看永久免费| 国产精品亚洲一区二区在线观看| 亚洲黄色片在线观看|