隨筆 - 303  文章 - 883  trackbacks - 0
          <2008年2月>
          272829303112
          3456789
          10111213141516
          17181920212223
          2425262728291
          2345678

          歡迎光臨! 
          閑聊 QQ:1074961813

          隨筆分類(357)

          我管理的群

          公共blog

          • n維空間
          • Email : java3d@126.com 群 : 12999758

          參與管理的論壇

          好友的blog

          我的其他blog

          朋友的網站

          搜索

          •  

          最新評論

          正如很多J2ee一樣,這個程序功能非常簡單就是實現登陸驗證,代碼也十分簡單,但是,編寫的時候經常出錯,原因是一些小地方沒注意到。這里提供程序的下載并,提供部分重點容忽視代碼的解釋。

          如何讓spring和struts融合在一起?寫代碼過程中一篇來自IBM的文章給了我很大的幫助,下面對我的這個程序做簡要說明

          該步驟可以在兩個地方實現
          第一,每個web工程都有一個web.xml配置文件,這個文件在Tomcat啟動的時候會被Tomcat加載,和很多配置文件的原理一樣,這個文件為Tomcat和Tomcat建立了一個通訊渠道,所以,我們需要在里面做些配置,通過它去告訴Tomcat服務器,我們這個web工程里用了struts和spring,具體的方法是:
          告訴它我們用了stucts

              <servlet>
                  
          <servlet-name>action</servlet-name>
                  
          <servlet-class>
                      org.apache.struts.action.ActionServlet
                  
          </servlet-class>
                  
          <init-param>
                      
          <param-name>config</param-name>
                      
          <param-value>/WEB-INF/struts-config.xml</param-value>
                  
          </init-param>
                  
          <init-param>
                      
          <param-name>debug</param-name>
                      
          <param-value>3</param-value>
                  
          </init-param>
                  
          <init-param>
                      
          <param-name>detail</param-name>
                      
          <param-value>3</param-value>
                  
          </init-param>
                  
          <load-on-startup>0</load-on-startup>
              
          </servlet>
              
          <servlet-mapping>
                  
          <servlet-name>action</servlet-name>
                  
          <url-pattern>*.lusm</url-pattern>
              
          </servlet-mapping>

          和它說我們用了spring

              <servlet>
                  
          <servlet-name>context</servlet-name>
                  
          <servlet-class>
                      org.springframework.web.context.ContextLoaderServlet
                  
          </servlet-class>
                  
          <load-on-startup>1</load-on-startup>
              
          </servlet>
              
          <context-param>
                  
          <param-name>contextConfigLocation</param-name>
                  
          <param-value>/WEB-INF/applicationContext.xml</param-value>
              
          </context-param>
          其實就是把struts和spring對應的兩個xml配置文件,注入到里面,這樣web容器就知道我們用了struts和spring啦!!!

          第二、在代碼中整合srping和struts。
          我們記得在寫struts的****Action的時候,這個Action通常繼承自org.apache.struts.action.Action ,但現在情況不同啦!!!正所謂識時務者為英雄,既然我們要使用sping了,就不可墨守成規,實際使用中我們讓Action繼承org.springframework.web.struts.ActionSupport,注意這對我們后面的,通過spring的ApplicationContext con =  getWebApplicationContext(); 起了關鍵作用,通過它我們可以讓spring通過web.xml獲取/WEB-INF/applicationContext.xml獲取配置信息,從而獲取一個通過spring實例化的bean---user,接著,我們拿出struts中實例化的另一個實例(通過我們提交的數據,實例化的bean)userForm下面是代碼:(注意其中的new String(  )方法)

          /*
           * Generated by MyEclipse Struts
           * Template path: templates/java/JavaClass.vtl
           
          */

          package com.lusm.struts.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 com.lusm.struts.form.UserForm;

          import org.springframework.context.ApplicationContext;
          import org.springframework.web.struts.ActionSupport;

          public class UserLoginAction extends ActionSupport{
              
          /*
               * Generated Methods
               
          */

              
          //這個是必然會執行的
              public ActionForward execute(ActionMapping mapping, ActionForm form,
                      HttpServletRequest request, HttpServletResponse response) 
          {
                  ApplicationContext con 
          =  getWebApplicationContext(); 
                  UserForm userForm 
          = (UserForm) form;
                  
                  UserForm user 
          = (UserForm)con.getBean("User");
                  
                  
          if(new String(userForm.getName()).equals(new String(user.getName()))
                          
          &&
                     
          new String(userForm.getPasswd()).equals(new String(user.getPasswd())))
                  
          {
                      System.out.println(
          "驗證成功");
                      
          return mapping.findForward("success");
                  }

                  
          else{
                      System.out.println(
          "驗證失敗");
                      
          return mapping.findForward("error");
                  }

              }

          }

          運行結果:



          其中的jar太大了請自己去官方下載:


          程序下載:
             __download__




          地震讓大伙知道:居安思危,才是生存之道。
          posted on 2008-02-26 18:33 小尋 閱讀(1092) 評論(1)  編輯  收藏 所屬分類: j2se/j2ee/j2me

          FeedBack:
          # re: Structs && Spring 登陸實驗 2008-03-22 18:02 幻想~@@~

          如何在servlet使用session,以上面的代碼為例
          UserLoginAction.java里做如下更改:

           

          //>>>>>>>>>>部分代碼>>>>>>>>>>>>>>>>>
                  HttpSession session = request.getSession(true);
                  response.setContentType("text/html");
                  
                  UserForm userForm 
          = (UserForm) form;
                  
                  UserForm user 
          = (UserForm)con.getBean("User");
                  
                  
          if(new String(userForm.getName()).equals(new String(user.getName()))
                          
          &&
                     
          new String(userForm.getPasswd()).equals(new String(user.getPasswd())))
                  
          {
                      session.setAttribute(" username ",user.getName());
                      System.out.println("驗證成功");
                      
          return mapping.findForward("success");
                  }

          //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


          在所有jsp或servlet頁面都可以使用

          ${username}//將username的值取出來,這里是"lusm"
            回復  更多評論
            
          主站蜘蛛池模板: 恩施市| 信宜市| 米泉市| 石棉县| 铁岭县| 鹤庆县| 龙门县| 长汀县| 西畴县| 大厂| 博湖县| 象山县| 莱西市| 长汀县| 新巴尔虎右旗| 长乐市| 福州市| 英山县| 镇平县| 鱼台县| 茂名市| 固始县| 梁山县| 广灵县| 临洮县| 遵义县| 江门市| 盱眙县| 巴林右旗| 祥云县| 宁河县| 洛川县| 永修县| 台中县| 社旗县| 凭祥市| 沿河| 景德镇市| 方城县| 独山县| 石景山区|