春风博客

春天里,百花香...

导航

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

统计

公告

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

Locations of visitors to this page

常用链接

留言簿(11)

随笔分类(224)

随笔档案(126)

个人软件下载

我的其它博客

我的邻居们

最新随笔

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜

Web页面表单域验证方式在Struts1.3.8中的使用

此文是“Web页面表单域验证方式的改进”的续篇。

示例页面:登录页面
<%@ 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">
            
<!-- 调用通用验证函数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">请填入二到三位的中文用户名</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">请填入一到十位的密码</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">如无用户点击<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">
<!--
/**
* 取得需要验证的文本框控件数组
*
*/

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">
            
<!-- 调用通用验证函数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">中文很美,请填入二到四位的中文用户名</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">请填入一到十位的密码</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">请填入一到十位的密码</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">请填入合法的邮件地址</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">
<!--

/**
* 取得表单的验证结果
*
*/

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;
    }

}


/**
* 取得需要验证的文本框控件数组
*
*/

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");
        
        
// 取得参数
        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相关处理的实用工具类
 * 
@author 何杨(heyang78@gmail.com)
 *
 * 
@since 2008-8-13 上午11:32:49
 * 
@version 1.00
 
*/

public final class ReqUtil{
    
private ReqUtil(){
        
    }

    
    
/**
     * 从页面表单中取出字段并转码
     * 用于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;
        }

    }

    
    
/**
     * 从页面表单中取出字段并转码
     * 用于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;
        }

    }

    
    
/**
     * 取得转码后的字符串,用于解决乱码问题
     * 
@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;
        }

    }

    
    
/**
     * 取得用户的機器名
     * 
@param request
     * 
@return
     
*/

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

}

posted on 2008-09-05 21:52 sitinspring 阅读(1219) 评论(0)  编辑  收藏 所属分类: Web开发


只有注册用户登录后才能发表评论。


网站导航:
 
sitinspring(http://www.blogjava.net)原创,转载请注明出处.