<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
    已經有很多人寫過文章,來介紹和說明這個問題了。
    我在學習和使用之后,就也作了一下總結。
    第一步:
    配置和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://m.tkk7.com/zhyiwww   
    |----------------------------------------------------------------------------------------|
    posted on 2007-07-05 18:27 zhyiwww 閱讀(1165) 評論(0)  編輯  收藏 所屬分類: j2ee
    主站蜘蛛池模板: 亚洲专区中文字幕| 日韩亚洲欧洲在线com91tv| 7777久久亚洲中文字幕蜜桃| 日韩免费视频一区二区| 亚洲2022国产成人精品无码区| a级毛片免费完整视频| 亚洲春色在线视频| 美丽姑娘免费观看在线观看中文版| 亚洲AV中文无码乱人伦下载| 性xxxxx大片免费视频| 亚洲视频一区二区在线观看| 美女视频黄a视频全免费| 亚洲av无码久久忘忧草| 成熟女人特级毛片www免费| 亚洲色大情网站www| 免费二级毛片免费完整视频| 免费一区二区三区在线视频| 在线亚洲午夜理论AV大片| 一级毛片免费播放| 国产亚洲精品VA片在线播放| 午夜亚洲av永久无码精品 | 精品亚洲成在人线AV无码| 久久久久国色AV免费观看性色| 亚洲中文字幕一区精品自拍| 少妇亚洲免费精品| 毛片在线全部免费观看| 亚洲人成网站18禁止久久影院| 成年人免费网站在线观看| 成人特级毛片69免费观看| 色拍自拍亚洲综合图区| 国产成人精品高清免费| 中文字幕无码免费久久| 亚洲成年人电影在线观看| 全黄性性激高免费视频| 久久99毛片免费观看不卡| 亚洲综合一区二区三区四区五区| 亚洲国产日韩在线观频| 国产在线jyzzjyzz免费麻豆| 亚洲乱码中文字幕在线| 亚洲av无码无在线观看红杏| 日韩免费观看视频|