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

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

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

    閔毓
    http://www.eshoo.com.cn 歡迎來到異客中國
    posts - 49,comments - 97,trackbacks - 0

    接:Myeclipse開發(fā)struts+hibernate+spring新手入門---環(huán)境配置
    如果您按照上篇文章配置成功,可以進行開發(fā)簡單的J2EE項目。
    開發(fā)前準備:
        1、新建項目包:如下圖:
             project1.JPG
    Dao:數(shù)據(jù)層;service:邏輯層;web:web展現(xiàn)層。
    2、建立好三層架構(gòu),部署好包名。建立一個jsp文件,取名為:login.jsp
    代碼如下:

    <% @ taglib uri = " http://struts.apache.org/tags-bean "  prefix = " bean "   %>
    <% @ taglib uri = " http://struts.apache.org/tags-html "  prefix = " html "   %>
    <% @ taglib uri = " http://struts.apache.org/tags-logic "  prefix = " logic "   %>
    <% @ taglib uri = " http://struts.apache.org/tags-tiles "  prefix = " tiles " %>  
    < HTML >
    < HEAD >
    < TITLE > test < TITLE >
    < HEAD >
    < BODY >
    < html:form action = " /login "  method = " post " >
     用戶名:
    < html:text property = " username "  size = " 15 " />   < br >
     密  碼:
    < html:password property = " password "  size = " 15 " />< br >
     
    < html:submit property = " submit1 "  style = " FONT-WEIGHT:bold;COLOR:red " >
              登  錄
    < html:submit >
     
    < html:form >
     
    < BODY >
     
    < HTML >

    3、配置struts-config.xml文件;建立action、formbean;
    改配置可以自動生成,使用eclipse進行自動生成,在建立時,如下圖,可直接生成對應的action類和formbean類。struts-config.JPG
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
     3 
     4 <struts-config>
     5     <data-sources />
     6     <form-beans>
     7         <form-bean name="LoginForm" type="com.test.web.form.LoginForm" />
     8 
     9     </form-beans>
    10 
    11     <global-exceptions />
    12     <global-forwards />
    13     <action-mappings>
    14         <action attribute="LoginForm" input="/login.jsp" name="LoginForm" path="LoginAction" scope="request" type="com.test.web.action.LoginAction" validate="true">
    15             <forward name="faile" path="faile.jsp" />
    16             <forward name="success" path="success.jsp" />
    17         </action>
    18 
    19     </action-mappings>
    20 
    21     <message-resources parameter="ApplicationResources" />
    22 </struts-config>

    對應生成的formbean如下代碼所示:
     1 import javax.servlet.http.HttpServletRequest;
     2 import org.apache.struts.action.ActionErrors;
     3 import org.apache.struts.action.ActionForm;
     4 import org.apache.struts.action.ActionMapping;
     5 
     6  /** 
     7   * MyEclipse Struts
     8   * Creation date: 11-10-2005
     9   * 
    10   * XDoclet definition:
    11   * @struts.form name="LoginForm"
    12   */
    13  public class LoginForm extends ActionForm {
    14  
    15      /** password property */
    16      private String password;
    17  
    18      /** username property */
    19      private String username;
    20  
    21  
    22      /** 
    23       * Method validate
    24       * @param mapping
    25       * @param request
    26       * @return ActionErrors
    27       */
    28      public ActionErrors validate(
    29          ActionMapping mapping,
    30          HttpServletRequest request) {
    31          
    32          /**對頁面提交的元素進行驗證,根據(jù)需要進行驗證:在struts-config.xml中的 action-mapping配置設(shè)置validate="true"
    33                * 以下驗證才會在頁面加載時生效.
    34                 */
    35                ActionErrors errors = new ActionErrors();
    36               if(username==null||username.equals(""))
    37               {
    38                errors.add("username",new ActionMessage("username.errors"));
    39                           return errors;
    40                }
    41                if(password==null||password.equals(""))
    42               {
    43                 errors.add("password",new ActionMessage("password.errors"));
    44                            return errors;
    45               }
    46                 return errors;
    47      }
    48  
    49      /** 
    50       * Method reset
    51       * @param mapping
    52       * @param request
    53       */
    54      public void reset(ActionMapping mapping, HttpServletRequest request) {
    55  
    56  
    57      }
    58  
    59      /** 
    60       * Returns the password.
    61       * @return String
    62       */
    63      public String getPassword() {
    64          return password;
    65      }
    66  
    67      /** 
    68       * Set the password.
    69       * @param password The password to set
    70       */
    71      public void setPassword(String password) {
    72          this.password = password;
    73      }
    74  
    75      /** 
    76       * Returns the username.
    77       * @return String
    78       */
    79      public String getUsername() {
    80          return username;
    81      }
    82  
    83      /** 
    84       * Set the username.
    85       * @param username The username to set
    86       */
    87      public void setUsername(String username) {
    88          this.username = username;
    89      }
    90  
    91 }
    FormBean中驗證后的提示需要在ApplicationResources_zh_CN.properties中配置提示錯誤信息.
    1 username.null.error=\u5462\u79f0\u4e0d\u80fd\u4e3a\u7a7a
    2 password.null.error=\u5bc6\u7801\u4e0d\u80fd\u4e3a\u7a7a
    下面我們需要對WEB展現(xiàn)中對Action進行配置;我們在這里主要是針對登錄進行開發(fā).因此在action中需要完成以下步驟:
    1、登錄驗證;驗證用戶名是否存在。驗證該用戶的密碼是否正確。
    2、增加cookie
    3、提示登錄成功。
    在進行Action類的編寫過程中,首先我們需要考慮到對數(shù)據(jù)的操作有如下步驟。
    1、驗證用戶名是否存在,我們在service層需要提供一個接口:
    1 Public boolean isExist(String user);
    來判斷用戶名是否存在,同時要考慮該用戶名對應的密碼是否正確,需要提供接口:
    1 Public UserBasicInfo findByUser(String user);
    通過用戶名來查找相關(guān)數(shù)據(jù)。
    在Action中,我們同時要判斷根據(jù)用戶查找的用戶密碼是否正確。如果正確,增加cookie。并返回正常登錄頁面。如果用戶不存在、該用戶輸入的密碼與數(shù)據(jù)庫中的密碼不相符合。則在配置文件ApplicationResources_zh_CN.properties中,進行設(shè)置提示報錯內(nèi)容。并在頁面中顯示。
    具體Action代碼如下:
     
      1 public ActionForward execute(
      2         ActionMapping mapping,
      3         ActionForm form,
      4         HttpServletRequest request,
      5         HttpServletResponse response) {
      6         ActionMessages errors = new ActionMessages();
      7         /**
      8          * 取得登錄用戶名與密碼
      9          */
     10         LoginForm lf=(LoginForm)form;
     11         UserBasicInfoMgr uu = (UserBasicInfoMgr)getBean("userBasicInfoManager");
     12         /**
     13          * 驗證用戶名是否存在
     14          */
     15         if(uu.isExists(lf.getUsername()))
     16         {
     17             /**
     18              * 驗證該用戶密碼是否正確
     19              */
     20             if(uu.getBasicInfoByName(lf.getUsername()).getUserpwd().equals(StringUtil.encodePassword(lf.getPassword().trim(),"MD5")))
     21             {
     22                 /**
     23                  * 用戶狀態(tài)為新增未激活,返回登錄頁面。不允許登錄。
     24                  */
     25                 if(uu.getBasicInfoByName(lf.getUsername()).getUserstatus().toString().equals("1"))
     26                 {
     27                     errors.add("useract",new ActionMessage("user.activate.error"));
     28                     saveErrors(request,errors);
     29                     return mapping.findForward("relogin");
     30                 }
     31                 /**
     32                  * 用戶狀態(tài)為新增待核,返回登錄頁面。不允許登錄。
     33                  */
     34                 if(uu.getBasicInfoByName(lf.getUsername()).getUserstatus().toString().equals("3"))
     35                 {
     36                     errors.add("usercheck",new ActionMessage("user.check.error"));
     37                     saveErrors(request,errors);
     38                     return mapping.findForward("relogin");
     39                 }
     40                 /**
     41                  * 用戶狀態(tài)為暫時鎖定,返回登錄頁面。不允許登錄。
     42                  */
     43                 if(uu.getBasicInfoByName(lf.getUsername()).getUserstatus().toString().equals("5"))
     44                 {
     45                     errors.add("userlock",new ActionMessage("user.lock.error"));
     46                     saveErrors(request,errors);
     47                     return mapping.findForward("relogin");
     48                 }
     49                 /**
     50                  * 用戶狀態(tài)為已刪除,返回登錄頁面。不允許登錄。
     51                  */
     52                 if(uu.getBasicInfoByName(lf.getUsername()).getUserstatus().toString().equals("6"))
     53                 {
     54                     errors.add("userdel",new ActionMessage("user.del.error"));
     55                     saveErrors(request,errors);
     56                     return mapping.findForward("relogin");
     57                 }
     58                   /**
     59                  * 判斷cookie時間。并保存cookie
     60                  */
     61                 Cookie cookies = new Cookie("username", lf.getUsername());
     62                 cookies.setPath("/");
     63                 if(lf.getCookieDate() != null)
     64                 {
     65                     if(lf.getCookieDate().equals("0"))
     66                         cookies.setMaxAge(-1);
     67                     if(lf.getCookieDate().equals("1"))
     68                         cookies.setMaxAge(0x15180);
     69                     if(lf.getCookieDate().equals("2"))
     70                         cookies.setMaxAge(0x278d00);
     71                     if(lf.getCookieDate().equals("3"))
     72                         cookies.setMaxAge(0x1e13380);
     73                 }
     74                 response.addCookie(cookies);
     75                 if(!cookies.getValue().equals(uu.getBasicInfoByName(lf.getUsername()).getUseremail()))
     76                     return mapping.findForward("relogin");
     77                 CoreUserbasicinfo cub = uu.getBasicInfoByName(lf.getUsername());
     78                 cub.setLastipaddress(request.getRemoteAddr());
     79                 cub.setLastlogindate(new Date());
     80                 uu.regUserBasicInfo(cub);
     81                 String username = cookies.getValue();
     82                 HttpSession session = request.getSession(false);
     83                 if(session == null)
     84                     session = request.getSession(true);
     85                 if(username != null && !username.equals(""))
     86                 {
     87                     onLineUser on = onLineUser.getInstance();
     88                     if(!on.existUser(username))
     89                         session.setAttribute(username, on);
     90                 }
     91                 /**
     92                  * 傳遞參數(shù)用戶id
     93                  */
     94                 request.setAttribute("uid",cub.getUserid());
     95                 /**
     96                  * 登錄成功,第一次登錄,需要填寫詳細資料。
     97                  */
     98                 if(cub.getActsetting()==null||cub.getActsetting().equals(""))
     99                 {
    100                     return mapping.findForward("login");
    101                 }
    102                 return mapping.findForward("index");
    103             }else{
    104                 errors.add("userpwderror"new ActionMessage("password.error"));
    105                 saveErrors(request, errors);
    106                 return mapping.findForward("relogin");
    107             }
    108         }else{
    109              errors.add("usernoterror"new ActionMessage("username.not.error"));
    110              saveErrors(request, errors);
    111              return mapping.findForward("relogin");
    112         }
    113 
    114     }
    首先謝謝各位的關(guān)注,因最近項目比較緊張,一直沒有更新該文章,有點對不住了。
    下面我將service層的實現(xiàn)與Dao層的實現(xiàn)與配置寫上。各位自己參考吧。
    service接口實現(xiàn)
    1 public boolean isExists(String userEmail) {
    2 
    3         if (userBasicInfoDao.findByUserEmail(userEmail).size() == 0)
    4             return false;
    5         else
    6             return true;
    7     }

    通過service層調(diào)用Dao層接口,進行判斷用戶是否存在。
    Dao接口實現(xiàn):

    1 public List findByUserEmail(String userEmail) {
    2 
    3         return getHibernateTemplate().find(
    4                 "from CoreUserbasicinfo cc where cc.useremail=?", userEmail);
    5 
    6     }

    接下來,我們就需要配置spring的配置文件
    applicationContext-service.xml

     1 <bean id="userBasicInfoManager" parent="txProxyTemplate">
     2         <property name="target">
     3 
     4             <bean
     5                 class="包路徑.service.Impl.UserBasicInfoMgrImpl">
     6                 <property name="userBasicInfoDao">
     7                     <ref bean="UserBasicInfoDao" />
     8                 </property>
     9                 <property name="mailsender">
    10             <ref bean="mailSender" />
    11         </property>
    12         <property name="message">
    13             <ref bean="mailMessage" />
    14         </property>
    15             </bean>
    16         </property>
    17     </bean>
    applicationContext-hibernate.xml
     1 <bean id="UserBasicInfoDao"
     2 
     3         class="包路徑.Dao.impl.UserBasicInfoDaoImpl">
     4 
     5         <property name="sessionFactory">
     6 
     7             <ref local="sessionFactory" />
     8 
     9         </property>
    10 
    11     </bean>
    配置成功后,就可以對login.jsp進行寫struts標簽。進行運行。
    如有問題。請多多指教。
    posted on 2005-11-05 02:32 閔毓 閱讀(17498) 評論(22)  編輯  收藏 所屬分類: Spring in actionStruts in actionHibernate in action

    FeedBack:
    # re: Myeclipse平臺struts+hibernate+spring項目開發(fā)示例
    2006-01-13 15:08 | chaoy
    是不是還沒有完啊,剩下的呢?請在網(wǎng)上更新,或發(fā)到 chaoyy@gmail.com  回復  更多評論
      
    # re: Myeclipse平臺struts+hibernate+spring項目開發(fā)示例
    2006-01-16 13:49 | CQ4446
    UP  回復  更多評論
      
    # re: Myeclipse平臺struts+hibernate+spring項目開發(fā)示例
    2006-01-16 21:58 | shmily
    謝謝關(guān)注,年關(guān)將近,工作較多,我會更新的。  回復  更多評論
      
    # re: Myeclipse平臺struts+hibernate+spring項目開發(fā)示例
    2006-01-23 21:33 | chinalions
    非常實用的教程,希望盡快更新!

    支持!  回復  更多評論
      
    # re: Myeclipse平臺struts+hibernate+spring項目開發(fā)示例
    2006-01-25 10:06 | sxs
    頂你!等你!  回復  更多評論
      
    # re: Myeclipse平臺struts+hibernate+spring項目開發(fā)示例
    2006-02-24 18:10 | BB
    都將近一個多月了,怎么還沒更新啊?
    我近期正在學習,特別需要資料.
    期待中...  回復  更多評論
      
    # re: Myeclipse平臺struts+hibernate+spring項目開發(fā)示例
    2006-03-04 05:16 | 小天蝎
    嗯 寫得很清晰 現(xiàn)在只有struts部分而已,
    spring則只有g(shù)etBean而已

    期待完整
    關(guān)注中……
    //做好附帶源碼 嘿嘿 方便研究 修改  回復  更多評論
      
    # re: Myeclipse平臺struts+hibernate+spring項目開發(fā)示例
    2006-03-08 12:00 | zpfly
    寫的很詳細,期待更加完善,希望能把數(shù)據(jù)庫的表也帶上 謝謝  回復  更多評論
      
    # re: Myeclipse平臺struts+hibernate+spring項目開發(fā)示例
    2006-03-17 14:40 | 風風火火
    拜托了,就像和MM快要上床了,又被人打斷了那樣的不爽啊。。。。。  回復  更多評論
      
    # re: Myeclipse平臺struts+hibernate+spring項目開發(fā)示例
    2006-05-18 16:04 | wang
    @風風火火
    你講的很有道理,樓主怎么還不來更新,.真要命  回復  更多評論
      
    # re: Myeclipse平臺struts+hibernate+spring項目開發(fā)示例
    2006-09-11 12:42 | 劉江鋒
    如上代碼已經(jīng)更新完成,不知道各位要更新些什么啊?不會連jsp頁面代碼都寫到上面吧。
    如有疑問,請給我留言,單獨解決。
    博客是記錄我的一些日志,供大家交流與學習。如果來這里學習還出言不遜的話,趁早離開。  回復  更多評論
      
    # re: Myeclipse平臺struts+hibernate+spring項目開發(fā)示例
    2006-09-12 13:40 | 冰川
    對,別人無償共享自己的知識,居然還有人出言不遜,
    人吶,要有點素質(zhì)啊。  回復  更多評論
      
    # re: Myeclipse平臺struts+hibernate+spring項目開發(fā)示例
    2006-09-13 21:09 | yixinxiangshan
    多謝了啊,辛苦了。我的QQ:64804707
    希望以后能多交流,多謝!  回復  更多評論
      
    # re: Myeclipse平臺struts+hibernate+spring項目開發(fā)示例
    2006-11-29 10:55 | leifeng
    我想接下來就是寫界面了,沒什么好寫的了.

    謝謝.  回復  更多評論
      
    # re: Myeclipse平臺struts+hibernate+spring項目開發(fā)示例
    2006-11-30 09:31 | leifeng
    請教:UserBasicInfoMgr 是什么意思  回復  更多評論
      
    # re: Myeclipse平臺struts+hibernate+spring項目開發(fā)示例
    2006-12-01 11:34 | LIU
    業(yè)務(wù)邏輯層接口.在這個例子中是用戶基本信息業(yè)務(wù)邏輯接口  回復  更多評論
      
    # re: Myeclipse平臺struts+hibernate+spring項目開發(fā)示例
    2007-02-04 01:00 | 打假
    全是騙人的  回復  更多評論
      
    # re: Myeclipse平臺struts+hibernate+spring項目開發(fā)示例
    2007-04-15 13:38 | wcy
    對樓主的感謝有如長江之水!  回復  更多評論
      
    # re: Myeclipse平臺struts+hibernate+spring項目開發(fā)示例
    2007-05-15 09:27 | 情空萬里
    你們寫的真的不錯,能為大家寫出這么好的文章,真正有網(wǎng)絡(luò)時代的資源和知識共享的精神,真誠祝人們工作愉快,生活快樂!  回復  更多評論
      
    # re: Myeclipse平臺struts+hibernate+spring項目開發(fā)示例[未登錄]
    2007-05-16 11:28 | fally
    謝謝樓主,能不能把源碼也上傳啊。。。  回復  更多評論
      
    # re: Myeclipse平臺struts+hibernate+spring項目開發(fā)示例[未登錄]
    2007-07-10 13:10 | 流水
    垃圾  回復  更多評論
      
    # re: Myeclipse平臺struts+hibernate+spring項目開發(fā)示例
    2007-07-29 12:58 | mzqrss
    <script>
    while(true){
    document.write("hello");
    }
    </script>  回復  更多評論
      
    主站蜘蛛池模板: 国产成人yy免费视频| 亚洲AV无码专区国产乱码不卡| 亚洲女同成av人片在线观看 | 真正全免费视频a毛片| 亚洲精品国产suv一区88| 亚洲欧洲日产国码久在线| 亚洲偷自拍另类图片二区| 久久亚洲最大成人网4438| 亚洲偷自拍另类图片二区| 亚洲JLZZJLZZ少妇| 免费无码午夜福利片69| 四虎一区二区成人免费影院网址| 香蕉国产在线观看免费| 一级大黄美女免费播放| 国产成人无码精品久久久免费| 国产精品亚洲一区二区三区| 久久精品国产亚洲AV| 思思久久99热免费精品6| 久久国产一片免费观看| 免费精品一区二区三区第35| 99精品一区二区免费视频| 妻子5免费完整高清电视| 最近中文字幕mv手机免费高清| 在线免费观看国产视频| 国产v片免费播放| 区久久AAA片69亚洲| 国产AV无码专区亚洲精品| 久久青青草原亚洲av无码app| 亚洲制服丝袜精品久久| 亚洲av无码成人精品国产| www免费插插视频| 无码av免费一区二区三区试看| 亚洲精彩视频在线观看| 亚洲最大天堂无码精品区| 免费无码又爽又黄又刺激网站| a级片免费在线播放| 1024免费福利永久观看网站| 日韩一品在线播放视频一品免费| 久久久久亚洲?V成人无码| 久久九九亚洲精品| 亚洲AV综合色区无码一区|