熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

Struts整合spring方法(三)

2022-06-13   來源: Java開源技術 

  Spring提供了DelegatingActionProxy類用來代替struts中的動作負責在Spring配置文檔中查找對應的動作映射從而把Struts的Action與Spring分離並把struts的動作置於Spring框架的控制下
    新建一個登陸頁面loginDelegatingActionProxyjsp示例代碼如下

       <%@page contentType=text/html;charset=GBK isELIgnored=false%>
    <html>
        <head>
           <title>實現用戶登錄的Struts實例</title>
        </head>
        <body>
           <font size=> ${msg}<br></font>
           <form name=form action=/myLogin/loginDelegatingActionProxydo
               method=post>
               用戶名
               <input type=text name=username value=${userusername}/>
               <br>
               密碼
               <input type=password name=password value=${userpassword}/>
               <br>
               <input type=submit name=method value=提交 />
           </form>
        </body>
    </html>

  創建一個存儲登陸用戶信息的類Userjava該類繼承於ActionForm代碼如下

        package comzhaosoftbean;

  import orgapachestrutsactionActionForm;

  publicclass User extends ActionForm {

  private String username=null;
        private String password=null;
        public String getUsername() {
           returnusername;
        }
        publicvoid setUsername(String username) {
           thisusername = username;
        }
        public String getPassword() {
           returnpassword;
        }
        publicvoid setPassword(String password) {
           thispassword = password;
        }
    }



    在 Comzhaosoftaction中新建一個控制器LoginDelegatingActionProxyjava該類繼承於Struts的Action代碼如下

       package comzhaosoftaction;

  import javaxservlethttpHttpServletRequest;
    import javaxservlethttpHttpServletResponse;
    import orgapachestrutsactionAction;
    import orgapachestrutsactionActionForm;
    import orgapachestrutsactionActionForward;
    import orgapachestrutsactionActionMapping;

  import comzhaosoftbeanUser;
    import comzhaosoftdomainLogin;

  publicclass LoginDelegatingActionProxy extends Action {

  private Login login;

  public Login getLogin() {
           returnlogin;
        }

  publicvoid setLogin(Login login) {
           thislogin = login;
        }

  public ActionForward execute(ActionMapping mapping ActionForm form
               HttpServletRequest request HttpServletResponse response)
               throws Exception {
           String forwardJsp = login;
           getLogin()login((User) form);
           requestsetAttribute(msg logingetMsg());
           requestsetAttribute(user (User) form);
           return mappingfindForward(login);
        }

  }

  新建包comzhaosoftdomain和comzhaosoftdomainimpl在包comzhaosoftdomain中新建一個接口Loginjava用來定義登陸的邏輯處理示例代碼如下

        package comzhaosoftdomain;

  import comzhaosoftbeanUser;

  publicinterface Login {
    publicabstractvoid login(User user);
    publicabstract String getMsg();
    }



    . 在包comzhaosoftdomainimpl中建立LoginImpljava用來實現登陸的邏輯處理:

    package comzhaosoftdomainimpl;

  import comzhaosoftbeanUser;
    import comzhaosoftdomainLogin;

  publicclass LoginImpl implements Login {

  private String msg;
        public String getMsg() {
           returnthismsg;
        }

  publicvoid login(User user) {
           String username = usergetUsername();
           String password = usergetPassword();
           if (username == null && password == null) {
               msg = 請輸入用戶名和密碼;
           } elseif (equals(username) || equals(password)) {
               msg = 請輸入用戶名和密碼;
           } elseif (zhaosoftequals(username) && zhaosoftequals(password)) {
               msg = 登陸成功;
           } elseif (!zhaosoftequals(username)) {
               msg = 用戶名填寫錯誤;
           }
        }

  }

  修改struts的配置文件strutsconfigxml注冊ContextLoaderPlugIn插件實例代碼如下

     <?xml version= encoding=UTF?>
    <!DOCTYPE strutsconfig PUBLIC //Apache Software Foundation//DTD Struts Configuration //EN config__dtd>
    <strutsconfig>
        <!定義formbean>
        <formbeans>
           <formbean name=helloWorld
               type=comzhaosoftbeanHelloWorld />
           <formbean name=user type=comzhaosoftbeanUser></formbean>
        </formbeans>
        <actionmappings>
           <action path=/loginDelegatingActionProxy
               type=orgspringframeworkwebstrutsDelegatingActionProxy
               name=user>
               <forward name=login
                  path=/jsp/loginDelegatingActionProxyjsp>
               </forward>
           </action>
           <action path=/loginDelegatingActionProxyTemp
               type=orgapachestrutsactionsForwardAction
               parameter=/jsp/loginDelegatingActionProxyjsp>
           </action>
        </actionmappings>
        <plugin
           className=orgspringframeworkwebstrutsContextLoaderPlugIn>
           <setproperty property=contextConfigLocation
               value=/WEBINF/springconfigxml />
        </plugin>
    </strutsconfig>

  編寫Spring的配置文件springconfigxml放在WEBINF目錄中代碼如下

     <?xml version= encoding=UTF?>
    <!DOCTYPE beans PUBLIC //SPRING//DTD BEAN//EN beansdtd>
    <beans>
        <bean id=login class=comzhaosoftdomainimplLoginImpl>
        </bean>
        <! 注意和struts路徑的對應 >
        <bean name=/loginDelegatingActionProxy class=comzhaosoftactionLoginDelegatingActionProxy>
        <property name=login>
        <ref bean=login/>
        </property>
        </bean>
    </beans>


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28945.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.