春風(fēng)博客

          春天里,百花香...

          導(dǎo)航

          <2008年9月>
          31123456
          78910111213
          14151617181920
          21222324252627
          2829301234
          567891011

          統(tǒng)計(jì)

          公告

          MAIL: junglesong@gmail.com
          MSN: junglesong_5@hotmail.com

          Locations of visitors to this page

          常用鏈接

          留言簿(11)

          隨筆分類(224)

          隨筆檔案(126)

          個(gè)人軟件下載

          我的其它博客

          我的鄰居們

          最新隨筆

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          Web頁面表單域驗(yàn)證方式在Struts1.3.8中的使用

          此文是“Web頁面表單域驗(yàn)證方式的改進(jìn)”的續(xù)篇。

          示例頁面:登錄頁面
          <%@ page contentType="text/html; charset=UTF-8"%>
          <%@ taglib uri="/WEB-INF/tld/struts-html.tld" prefix="html"%>
          <%@ taglib uri="/WEB-INF/tld/struts-logic.tld" prefix="logic"%>
          <%@ taglib uri="/WEB-INF/tld/struts-bean.tld" prefix="bean"%>

          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
          <html>
          <head>
          <title>登錄頁面--BeyondDiscuz</title>
          <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
          <link rel="stylesheet" rev="stylesheet" href="web/css/style.css" type="text/css" />
          <script src="web/js/formchecker.js" type="text/javascript"></script>
          <script src="web/js/ajax.js" type="text/javascript"></script>
          </head>

          <body>
              
          <div id="bodyDiv">
                  
          <div id="header">
                      
          <jsp:include page="/web/page/branch/header.jsp"/>
                  
          </div>
                  
          <div id="menubar">
                      
          <jsp:include page="/web/page/branch/menubar.jsp"/>
                  
          </div>
                  
          <div id="content">
                      
          <!-- 調(diào)用通用驗(yàn)證函數(shù)checkForm -->
                      
          <html:form action="/Login.do" onsubmit="return checkForm(getCheckArray());">
                      
          <table class="block" cellspacing="1" cellpadding="0" bgcolor="#f7f7f7">
                          
          <tr height="30">
                              
          <td colspan="4" bgcolor="#d6e0ef">
                              
          &nbsp;<font face=webdings color=#ff8c00>8</font><b>&nbsp;歡迎登錄BeyondDiscuz論壇</b>
                              
          </td>
                          
          </tr>    
                          
          <tr height="20">
                              
          <td bgcolor="#f7f7f7" width="200" align="right"></td> 
                              
          <td bgcolor="#f7f7f7" align="left">
                                  
          <div id="msg">
                                      
          <logic:present name="msg">
                                          
          <bean:write name="msg"/>
                                      
          </logic:present>
                                  
          </div>                
                              
          </td> 
                          
          </tr>            
                          
          <tr height="40">
                              
          <td bgcolor="#f7f7f7" width="200" align="right">用戶名</td> 
                              
          <td bgcolor="#f7f7f7" align="left">
                                     
          <html:text property="name" size="16" maxlength="16"
                                         onfocus
          ="this.style.backgroundColor='#e6e6e6'" 
                                         onblur
          ="this.style.backgroundColor='#ffffff'"/>
                                  
          <font color=red>&nbsp;(必填)</font>
                                  
          <span id="nameMsg" class="feedbackHide">請?zhí)钊攵饺坏闹形挠脩裘?/span></span>
                              
          </td> 
                          
          </tr>
                          
                          
          <tr height="40">
                              
          <td bgcolor="#f7f7f7" width="200" align="right">密碼</td> 
                              
          <td bgcolor="#f7f7f7" align="left">
                                  
          <html:password property="pswd" size="16" maxlength="16"
                                         onfocus
          ="this.style.backgroundColor='#e6e6e6'" 
                                         onblur
          ="this.style.backgroundColor='#ffffff'"/>
                                  
          <font color=red>&nbsp;(必填)</font>
                                  
          <span id="pswdMsg" class="feedbackHide">請?zhí)钊胍坏绞坏拿艽a</span>
                              
          </td> 
                          
          </tr>
                          
                          
          <tr height="40">
                              
          <td bgcolor="#f7f7f7" width="200" align="right"></td> 
                              
          <td bgcolor="#f7f7f7" align="left">
                                  
          <html:submit property="submit" value="登錄論壇" />
                              
          </td> 
                          
          </tr>
                          
                          
          <tr height="40">
                              
          <td bgcolor="#f7f7f7" width="200" align="right"></td> 
                              
          <td bgcolor="#f7f7f7" align="left">如無用戶點(diǎn)擊<href='ShowPage.do?page=register'>這里</a>注冊</td> 
                          
          </tr>
                      
          </table>
                      
          <br/>
                      
          </html:form>
                  
          </div>        
              
          </div>
              
          <div id="footer">
                  
          <jsp:include page="/web/page/branch/footer.jsp"/>
              
          </div>
          </body>
          </html>

          <script LANGUAGE="JavaScript">
          <!--
          /**
          * 取得需要驗(yàn)證的文本框控件數(shù)組
          *
          */

          function getCheckArray(){
              
          var arr=new Array();
              
              arr[
          0]=createToBeCheckedObj("name","nameMsg","[\\u4E00-\\u9FA5]{2,3}","true");
              arr[
          1]=createToBeCheckedObj("pswd","pswdMsg",".{1,10}","true");

              
          return arr;
          }

          //-->
          </script>

          示例頁面:注冊頁面
          <%@ page contentType="text/html; charset=UTF-8"%>

          <%@ taglib uri="/WEB-INF/tld/struts-html.tld" prefix="html"%>
          <%@ taglib uri="/WEB-INF/tld/struts-logic.tld" prefix="logic"%>
          <%@ taglib uri="/WEB-INF/tld/struts-bean.tld" prefix="bean"%>

          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
          <html>
          <head>
          <title>注冊頁面--BeyondDiscuz</title>
          <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
          <link rel="stylesheet" rev="stylesheet" href="web/css/style.css" type="text/css" />
          <script src="web/js/formchecker.js" type="text/javascript"></script>
          <script src="web/js/ajax.js" type="text/javascript"></script>
          </head>

          <body>
              
          <div id="bodyDiv">
                  
          <div id="header">
                      
          <jsp:include page="/web/page/branch/header.jsp"/>
                  
          </div>
                  
          <div id="menubar">
                      
          <jsp:include page="/web/page/branch/menubar.jsp"/>
                  
          </div>
                  
          <div id="content">
                      
          <!-- 調(diào)用通用驗(yàn)證函數(shù)checkForm -->
                      
          <html:form action="/Register.do" onsubmit="return getCheckResult();">
                      
          <table class="block" cellspacing="1" cellpadding="0" bgcolor="#f7f7f7">
                          
          <tr height="30">
                              
          <td colspan="4" bgcolor="#d6e0ef">
                              
          &nbsp;<font face=webdings color=#ff8c00>8</font><b>&nbsp;您很快將成為BeyondDiscuz論壇的一員</b>
                              
          </td>
                          
          </tr>    
                          
          <tr height="20">
                              
          <td bgcolor="#f7f7f7" width="200" align="right"></td> 
                              
          <td bgcolor="#f7f7f7" align="left">
                                  
          <div id="msg">
                                      
          <logic:present name="msg">
                                          
          <bean:write name="msg"/>
                                      
          </logic:present>            
                                  
          </div>            
                              
          </td> 
                          
          </tr>            
                          
          <tr height="40">
                              
          <td bgcolor="#f7f7f7" width="200" align="right">用戶名</td> 
                              
          <td bgcolor="#f7f7f7" align="left">
                                  
          <html:text property="name" size="16" maxlength="16"
                                         onfocus
          ="this.style.backgroundColor='#e6e6e6'" 
                                         onblur
          ="this.style.backgroundColor='#ffffff'"/>
                                  
          <font color=red>&nbsp;(必填)</font>
                                  
          <span id="nameMsg" class="feedbackHide">中文很美,請?zhí)钊攵剿奈坏闹形挠脩裘?/span></span>
                              
          </td> 
                          
          </tr>
                          
                          
          <tr height="40">
                              
          <td bgcolor="#f7f7f7" width="200" align="right">密碼</td> 
                              
          <td bgcolor="#f7f7f7" align="left">
                                  
          <html:password property="pswd" size="16" maxlength="16"
                                         onfocus
          ="this.style.backgroundColor='#e6e6e6'" 
                                         onblur
          ="this.style.backgroundColor='#ffffff'"/>
                                  
          <font color=red>&nbsp;(必填)</font>
                                  
          <span id="pswdMsg" class="feedbackHide">請?zhí)钊胍坏绞坏拿艽a</span>
                              
          </td> 
                          
          </tr>
                          
                          
          <tr height="40">
                              
          <td bgcolor="#f7f7f7" width="200" align="right">再次輸入密碼</td> 
                              
          <td bgcolor="#f7f7f7" align="left">
                                  
          <html:password property="repswd" size="16" maxlength="16"
                                         onfocus
          ="this.style.backgroundColor='#e6e6e6'" 
                                         onblur
          ="this.style.backgroundColor='#ffffff'"/>
                                  
          <font color=red>&nbsp;(必填)</font>
                                  
          <span id="repswdMsg" class="feedbackHide">請?zhí)钊胍坏绞坏拿艽a</span>
                              
          </td> 
                          
          </tr>
                          
                          
          <tr height="40">
                              
          <td bgcolor="#f7f7f7" width="200" align="right">郵件</td> 
                              
          <td bgcolor="#f7f7f7" align="left">
                                  
          <html:text property="email" size="16" maxlength="16"
                                         onfocus
          ="this.style.backgroundColor='#e6e6e6'" 
                                         onblur
          ="this.style.backgroundColor='#ffffff'"/>
                                  
          <font color=red>&nbsp;(必填)</font>
                                  
          <span id="emailMsg" class="feedbackHide">請?zhí)钊牒戏ǖ泥]件地址</span>
                              
          </td> 
                          
          </tr>
                          
                          
          <tr height="40">
                              
          <td bgcolor="#f7f7f7" width="200" align="right"></td> 
                              
          <td bgcolor="#f7f7f7" align="left">
                                  
          <html:submit property="submit" value="注冊為論壇用戶" />
                              
          </td> 
                          
          </tr>
                      
          </table>
                      
          <br/>
                      
          </html:form>
                  
          </div>        
              
          </div>
              
          <div id="footer">
                  
          <jsp:include page="/web/page/branch/footer.jsp"/>
              
          </div>
          </body>
          </html>

          <script LANGUAGE="JavaScript">
          <!--

          /**
          * 取得表單的驗(yàn)證結(jié)果
          *
          */

          function getCheckResult(){
              
          if(checkForm(getCheckArray())==true){
                  
          var pswd=$("pswd").value;
                  
          var repswd=$("repswd").value;
                  
                  
          if(pswd!=repswd){
                      $(
          "msg").innerHTML="兩次密碼必需相等";
                      
          return false;
                  }

                  
                  
          return true;
              }

              
          else{
                  
          return false;
              }

          }


          /**
          * 取得需要驗(yàn)證的文本框控件數(shù)組
          *
          */

          function getCheckArray(){
              
          var arr=new Array();
              
              arr[
          0]=createToBeCheckedObj("name","nameMsg","[\\u4E00-\\u9FA5]{2,4}","true");
              arr[
          1]=createToBeCheckedObj("pswd","pswdMsg",".{1,10}","true");
              arr[
          2]=createToBeCheckedObj("repswd","repswdMsg",".{1,10}","true");
              arr[
          2]=createToBeCheckedObj("email","emailMsg","\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\.\\w+([-.]\\w+)*","true");

              
          return arr;
          }

          //-->
          </script>

          loginAction
          package com.heyang.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.heyang.action.base.BizBaseAction;
          import com.heyang.domain.User;
          import com.heyang.service.UserService;
          import com.heyang.util.ReqUtil;
          import com.heyang.util.SpringUtil;

          /**
           * 用于用戶登錄的Action
           * 
          @author 何楊(heyang78@gmail.com)
           *
           * 
          @since 2008-8-29 上午09:00:48
           * 
          @version 1.00
           
          */

          public final class LoginAction extends BizBaseAction {
              
          public ActionForward process(ActionMapping mapping, ActionForm form,
                      HttpServletRequest request, HttpServletResponse response)
                      
          throws Exception {
                  request.setCharacterEncoding(
          "UTF-8");
                  
                  
          // 取得參數(shù)
                  String name = ReqUtil.getFormProperty("name",form);
                  String pswd 
          = ReqUtil.getFormProperty("pswd",form);
                      
                  
          // 取得Service
                  UserService service=SpringUtil.getUserService();        
                  
                  
          // 取得用戶
                  User user=service.getUser(name, pswd);
                  request.getSession().setAttribute(
          "user", user);
                  
          return new ActionForward("/ShowBlocks.do");
              }

          }

          從頁面取值的輔助類
          package com.heyang.util;

          import javax.servlet.http.HttpServletRequest;

          import org.apache.commons.beanutils.PropertyUtils;
          import org.apache.struts.action.ActionForm;

          /**
           * 用于Request相關(guān)處理的實(shí)用工具類
           * 
          @author 何楊(heyang78@gmail.com)
           *
           * 
          @since 2008-8-13 上午11:32:49
           * 
          @version 1.00
           
          */

          public final class ReqUtil{
              
          private ReqUtil(){
                  
              }

              
              
          /**
               * 從頁面表單中取出字段并轉(zhuǎn)碼
               * 用于Jsp
               * 
          @param paramName
               * 
          @param request
               * 
          @return
               
          */

              
          public static String getFormParam(String paramName,HttpServletRequest request){
                  
          try{
                      
                      
          return new String(request.getParameter(paramName).getBytes("ISO-8859-1"),"UTF-8");
                  }

                  
          catch(Exception ex){
                      
          return null;
                  }

              }

              
              
          /**
               * 從頁面表單中取出字段并轉(zhuǎn)碼
               * 用于Struts
               * 
          @param paramName
               * 
          @param request
               * 
          @return
               
          */

              
          public static String getFormProperty(String propertyName,ActionForm form){
                  
          try{
                      String property 
          = (String) PropertyUtils.getSimpleProperty(form, propertyName);
                      
          return new String(property.getBytes("ISO-8859-1"),"UTF-8");
                  }

                  
          catch(Exception ex){
                      
          return null;
                  }

              }

              
              
          /**
               * 取得轉(zhuǎn)碼后的字符串,用于解決亂碼問題
               * 
          @param str
               * 
          @return
               
          */

              
          public static String getDecodeStr(String str){
                  
          try{
                      
          return new String(str.getBytes("ISO-8859-1"),"UTF-8");
                  }

                  
          catch(Exception ex){
                      
          return null;
                  }

              }

              
              
              
          /**
               * 取得用戶的IP地址
               * 
          @param request
               * 
          @return
               
          */

              
          public static String getIp(HttpServletRequest request){
                  String ip
          =request.getRemoteAddr();
                  String originIp
          =request.getHeader("x-forwarded-for");
                  
                  
          if(originIp==null || originIp.length()==0){
                      
          return ip;
                  }

                  
          else{
                      
          return originIp;
                  }

              }

              
              
          /**
               * 取得用戶的機(jī)器名
               * 
          @param request
               * 
          @return
               
          */

              
          public static String getClientname(HttpServletRequest request){
                  
          return request.getRemoteHost();        
              }

          }

          posted on 2008-09-05 21:52 sitinspring 閱讀(1228) 評論(0)  編輯  收藏 所屬分類: Web開發(fā)

          sitinspring(http://www.aygfsteel.com)原創(chuàng),轉(zhuǎn)載請注明出處.
          主站蜘蛛池模板: 克什克腾旗| 大方县| 灵寿县| 河西区| 蓬溪县| 赫章县| 庐江县| 运城市| 昆山市| 个旧市| 长治县| 宜丰县| 柳江县| 兴海县| 秦皇岛市| 富锦市| 茂名市| 洪雅县| 巫山县| 临沭县| 龙海市| 河南省| 司法| 石河子市| 罗江县| 泗水县| 南汇区| 肇源县| 林西县| 秭归县| 莱芜市| 大方县| 广灵县| 陆河县| 留坝县| 平阳县| 长乐市| 大埔县| 河池市| 兰州市| 图片|