一路拾遺
          Collect By Finding All The Way ......
          posts - 81,comments - 41,trackbacks - 0

          Struts實(shí)質(zhì)上就是在JSP Model 2的基礎(chǔ)上實(shí)現(xiàn)的一個(gè)MVC框架。由一組相互協(xié)作的類、Servlet以及JSP TagLib組成。Struts的核心組件主要包括:ActionServlet 、Action、 ActionMapping 、ActionForward、 ActionForm 、ActionError 及Struts標(biāo)簽庫(kù)組成。

          Struts 對(duì)于一個(gè)出學(xué)者來(lái)說(shuō),確實(shí)有一點(diǎn)困難,感覺是無(wú)形的,抓不住各個(gè)組件之間的邏輯,本實(shí)例主要是運(yùn)用Myeclipse開發(fā)Struts的用戶登錄程序,把無(wú)形的Struts變成有形的。

          本應(yīng)用程序主要由以下5個(gè)文件組成:login.jsp 登錄頁(yè)面,success.jsp提交成功后的頁(yè)面,如果提交失敗,在次返回登錄頁(yè)面;LoginForm.java(ActionForm,存放用戶提交信息),LoginAction.java(Action,簡(jiǎn)單的處理用戶登錄事件);及數(shù)據(jù)庫(kù)連接文件DBConnect.java。

          下面我們又要開始了。。。

          S1。 新建J2EE工程的web Project工程,工程名為:LoginStrutsDemo

           

          然后給這個(gè)項(xiàng)目添加Struts框架必要的文件.在我們項(xiàng)目名上點(diǎn)擊右鍵,選擇MyEclipes --> Add Struts Capabilities...彈出對(duì)話框如下:

           

          其中Struts config path就是我們的struts配置文件,URL pattern我們選擇*.do,Default application resource為我們默認(rèn)的資源文件地方,你可以選擇它的存儲(chǔ)位置,我們?cè)谶@里保持默認(rèn)。點(diǎn)擊Finish后,項(xiàng)目結(jié)構(gòu)如下圖:

           

          然后雙擊打開/WEB-INF/web.xml文件,為其添加標(biāo)簽庫(kù)。可以在Design界面或Source界面下添寫一個(gè)jsp-config標(biāo)簽,如下圖:

           

           

          然后,右擊OutLine框的jsp-config標(biāo)簽,添加三個(gè)taglib子標(biāo)簽,添好之后如下頁(yè)面:

           

          或直接在Web.xml文件的Source 中將下面代碼添加至 </webapp> 上面:

              <jsp-config>
             
          <taglib>
              
          <taglib-uri>/tags/struts-html</taglib-uri>
              
          <taglib-location>/WEB-INF/struts-html.tld</taglib-location>
             
          </taglib>
             
          <taglib>
              
          <taglib-uri>/tags/struts-bean</taglib-uri>
              
          <taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
             
          </taglib>
             
          <taglib>
              
          <taglib-uri>/tags/struts-logic</taglib-uri>
              
          <taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
             
          </taglib>
            
          </jsp-config>


          S2。打開struts-config.xml文件,點(diǎn)擊這個(gè)界面左下角的Design進(jìn)入可視化設(shè)計(jì)界面。我們先建立success.jsp文件如下:

           

          把JSP托到面版中,彈出如下對(duì)話框:

           

          File Name successs.jsp

           完成后,struts-config.xml文件自動(dòng)被更新,可視化界在上也出現(xiàn)了剛新建的JSP模塊。新建的jsp文件也打開了。覆蓋所有的<%@ taglib ...... ,用我們開始在/WEB-INF/web.xml中定義的:
              
            然后在<body></body>中添加:
            Hello ,<bean:write name="userName" scope="request" /> .
          這里將request中的屬性u(píng)serName輸出在頁(yè)面上,所以等下我們?cè)趌oginAction中,登錄成功后要設(shè)置一個(gè)相關(guān)屬性。

          S3。下面來(lái)開始我們最后三個(gè)文件的設(shè)計(jì)吧。在Struts-config.xml的Design模式中,在畫版的空白區(qū)域點(diǎn)右鍵,選擇New --> New Form, Action and JSP 彈出ActionForm的選項(xiàng)面板,我們按圖上輸入相關(guān)值,如下:

           

          因?yàn)槲覀冞@只是簡(jiǎn)單的演示一個(gè)登錄片段,所以不用驗(yàn)證用戶信息是否合法,所以將 Option Details的method選項(xiàng)卡的新建方法去掉,如圖:


           

          接下來(lái)選擇 Optional Details的JSP選項(xiàng)卡,我們選中Create JSP form? 這一步myeclipse將為我們創(chuàng)建一個(gè)簡(jiǎn)單的與用戶交互的登錄頁(yè)面:

           

          點(diǎn)Next,進(jìn)入Action選項(xiàng)面板.將Option Details的Form選項(xiàng)卡中Validate Form取消選擇,如圖:
           

          在添加兩個(gè)全局Forward,如下:

           

          點(diǎn)擊Finish完成。在Struts-config.xml的Design中,可以看到圖所示:

           

           最后,簡(jiǎn)單的修改一下login.jsp,將所有<%@ taglib ...%>替換為:
            <%@ taglib uri="/tags/struts-html" prefix="html"%>
            <%@ taglib uri="/tags/struts-bean" prefix="bean"%>

          S4。運(yùn)用MySQL建立數(shù)據(jù)庫(kù)腳本如下:

           

           Create database strutsuser; 
          User strutsuser; 
          Create table users( 
          Username 
          varchar(10primary key
          Password 
          varchar(10
          ); 
          Insert into users values(‘chen’,’123456’); 

           

          然后,添加數(shù)據(jù)庫(kù)驅(qū)動(dòng)。右鍵點(diǎn)擊工程名LoginStrutsDemo,選擇PropertiesàJava Build PathàLiberariesàAdd External JARs…

           

          點(diǎn)擊OK之后,在把MySQL數(shù)據(jù)庫(kù)驅(qū)動(dòng)文件拷貝到/WEB-INF/lib路徑下面,F5刷新。


           S5
          。建立數(shù)據(jù)庫(kù)連接文件:DBConnection.java

           

          代碼如下:

           

          package com.chen.struts.action;
          import java.sql.
          *;

          public class DBConnection {
              String sDBDriver 
          = "com.mysql.jdbc.Driver";
              String sConnstr 
          = "jdbc:mysql://192.168.1.103:3306/strutsUser?useUnicode=true&characterEncoding=GBK";
              Connection connect 
          = null;
              ResultSet rs 
          = null;
              Statement stmt 
          = null;
              
          public DBConnection()
              {
                      try
                      {
                              Class.forName(sDBDriver);
                      }
                      catch(ClassNotFoundException ex)
                      {
                              System.err.println(ex.getMessage());
                      }
              }

              
          public ResultSet executeQuery(String sql)
              {
                      try
                      {
                              this.connect 
          = DriverManager.getConnection(sConnstr,"root","");
                              this.stmt 
          = this.connect.createStatement();
                              rs 
          = stmt.executeQuery(sql);
                      }
                      catch(SQLException ex)
                      {
                              System.err.println(ex.getMessage());
                      }
                      
          return rs;
              }

              
          public int executeUpdate(String sql)
              {
                      
          int result = 0;
                      try
                      {
                              this.connect 
          = DriverManager.getConnection(sConnstr,"root","");
                              this.stmt 
          = this.connect.createStatement();
                              result 
          = stmt.executeUpdate(sql);
                      }
                      catch(SQLException ex)
                      {
                              System.err.println(ex.getMessage());
                      }
                      
          return result;
              }
          }


          修改LoginAction 如下所示:

          package com.chen.struts.action;


          import java.sql.
          *;
          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.http.HttpServletResponse;
          import org.apache.struts.action.Action;
          import org.apache.struts.action.ActionForm;
          import org.apache.struts.action.ActionForward;
          import org.apache.struts.action.ActionMapping;
          import com.chen.struts.form.LoginForm;

          /*
           * MyEclipse Struts
           * Creation date: 04-25-2007
           * 
           * XDoclet definition:
           * @struts.action path="/login" name="loginForm" input="/login.jsp" scope="request"
           * @struts.action-forward name="failture" path="/login.jsp"
           * @struts.action-forward name="success" path="/success.jsp"
           
          */



          public class LoginAction extends Action {
              
          /*
               * Generated Methods
               
          */

              
          /*
               * Method execute
               * @param mapping
               * @param form
               * @param request
               * @param response
               * @return ActionForward
               
          */

              
          public ActionForward execute(ActionMapping mapping, ActionForm form,
                     HttpServletRequest request, HttpServletResponse response) {
                 LoginForm loginForm 
          = (LoginForm) form;
                 String name 
          =loginForm.getUserName();
                 String password 
          =loginForm.getPassword();
                 DBConnection  myDb
          =new DBConnection();
                 ResultSet rs
          =null;
                 
          int result=0;

                 
          //String sql="select count(*as count from users where username = '"+name+"' and password = '"+password+"'";
                 String sql 
          = "select count(*as count from users where username = '"+name+"' and password = '"+password+"'";
                 try{
                     rs
          =myDb.executeQuery(sql);
                     
          if(rs.next()){
                        result
          =rs.getInt("count");
                     }
                 }catch(SQLException ex){
                     ex.printStackTrace();
                 }

                 
          if(result>0){
                     request.setAttribute("userName", name);
                     
          return mapping.findForward("success");
                 }

                 
          return mapping.findForward("failure");
              }
          }


          最后,部署MyEclipse J2EE Project 到服務(wù)器上,在啟動(dòng)MyEcplisp服務(wù)器,打開內(nèi)置瀏覽器,輸入:http://localhost:8080/LoginStrutsDemo/login.jsp,好了,就可以了 呵呵,終于完成了。

          posted on 2008-07-25 21:14 胖胖泡泡 閱讀(438) 評(píng)論(0)  編輯  收藏

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 黄大仙区| 斗六市| 合肥市| 大连市| 彭州市| 长沙市| 皋兰县| 伊春市| 玉树县| 哈巴河县| 华坪县| 始兴县| 日照市| 永和县| 永年县| 珠海市| 乌海市| 剑阁县| 海城市| 钦州市| 松江区| 新蔡县| 宿迁市| 壤塘县| 汤原县| 株洲县| 尖扎县| 阿尔山市| 宁津县| 江达县| 黑山县| 漳州市| 三台县| 五家渠市| 巴彦县| 土默特左旗| 文昌市| 娄底市| 陕西省| 桂阳县| 开封市|