页面验证

 

    有一JSP页面,上有一表单,提交的时候,需要对其内容进行检验。我用了两个方法,别人在这里叙述一下:

   方法一:在表现层验证,也就是在提交表单后,立即通过javascript进行验证。代码如下:

<%@ page contentType="text/html;charset=GBK"%>
<%@ taglib uri="/tags/struts-html" prefix="html"%>
<%@ taglib uri="/tags/struts-bean" prefix="bean"%>
<%@ taglib uri="/tags/struts-logic" prefix="logic"%>
<%@ taglib prefix="c" uri="/tags/c"%>
<%@ taglib uri="/tags/extremecomponents" prefix="ec"%>
<html>
<head>
<html:base />
//验证方法都在validation,js中
<script language="JavaScript" type="text/javascript" src="../../javascript/validation.js"></script>
<title>临床结局</title>
<script language="JavaScript">
    
function submitSave(){    
    
var t=document.getElementsByName("outcomeReason");  //意图取得输入框outcomeReason内容
    
var tt=document.getElementsByName("fillinDate");               // 意图取得输入框fillinDate内容
   //ValidateNumbers(t);

    //ValidateDate(tt);
    document.forms[
0].submit();
    }
  
    
</script>
</head>



<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0"> 

<html:form   action="/ClinicOutcome.do?operate=save">
<span class="applicationTitle">临床结局</span>
<table cellspacing="0" cellpadding="2" border="0" height="">
<tr><td height="2"><img src="../../images/background/spacer.gif" height="1" width="1" border=0 title="占位符"></td></tr>
</table>
<!-- 最外层table 开始 -->

<table cellspacing="7" cellpadding="0" border="0" width="100%" title=""> 
<tr>                                                                                       
       
<td>        
            <tr>
              
<td class="box">  
                
<table width="100%" border="0" cellpadding="2" cellspacing="0" title="输入字段的范围 ">
                    
<tr>
                        
<td class=label nowrap>填表日期*</td>
                        
<td  class=space nowrap>
                        
<script>popdate("fillinDate")</script>                        
                        
<input type="text" id="fillinDateid" name="fillinDate" value="${bean.fillinDate}" size="15"  maxlength="15" /></td>
                       
 <tr>
                        
<td class=label nowrap>转归原因(40字以内)</td>
                        
<td  colspan='4' align="left" class="label">
                                
<html:textarea property="outcomeReason"  cols="60" rows="2" />
                        
</td>                         
                        
<td class=space nowrap>&nbsp;</td>
                    
</tr>
                //..................省略

                 
</table>
            
</td>
          
</tr>
        
</table>
<!-- 最外层table 结束 -->

<!-- Buttons -->
<table cellspacing="0" cellpadding="0" border="0" title="按钮范围">
    
<tr>
        
<c:if test="${act != 'read' && act != 'approve'}">
            
<td class="pushButton" nowrap>
            
<p class="pushButton"><a href="javascript:submitSave();"
                name
="保存" class="pushButton" title="保存 按钮"
                onmouseover
="status=' ';return true;" onmouseout="status=' ';">&nbsp;保存&nbsp;</a></p>
            
</td>
            
<td nowrap>&nbsp;</td>
    
            
<td class="pushButton" nowrap>
            
<p class="pushButton"><a href=""
                target
="_self" class="pushButton" title="撤消所有更改"
                onmouseover
="status=' ';return true;" onmouseout="status=' ';">&nbsp;撤消所有更改&nbsp;</a></p>
            
</td>
            
<td nowrap>&nbsp;</td>
        
</c:if>
        
        
<c:if test="${act == 'approve'}">
            
<td class="pushButton" nowrap>
            
<p class="pushButton"><a href="javascript:approvePlan();"
                name
="审核" class="pushButton" title="审核 按钮"
                onmouseover
="status=' ';return true;" onmouseout="status=' ';">&nbsp;审核&nbsp;</a></p>
            
</td>
        
</c:if>
        
        
<td class="pushButton" nowrap>
            
<p class="pushButton"><a href="javascript:window.history.back()" target="_self" class="pushButton" title="返回" >&nbsp;返回&nbsp;</a></p>
        
</td>

    
</tr>
    
<tr><td height="2"><img src="/cdms/images/background/spacer.gif" height="22" width="60" border=0 ></td></tr>
</table>

<html:hidden property="act"/>
<html:hidden property="memberid"/>
<html:hidden property="resultid"/>
<html:hidden property="planid"/>
<html:hidden property="followupid"/>
<html:hidden property="detailid"/>

</html:form>
</body>

</html>

除去验证功能,上面代码没有任何问题!

但是,发现采用document.getElementsByName取到的内容不对(我尝试用alert(t[0])显示,显示不出来。用write也写不到页面上,一片空白,相信取得的内容是null)。

用javascipt不是我的第一选择,第一,javascript,基本上来说,我一窍不通,仅能看懂,第二,发现调试极其不方便。所以,我选择了方法二,在action中检验。

本来一切都显得得心应手,看看下面代码。

protected void ValidateOutcomeForm(HttpServletRequest request, ActionForm actionForm) throws Exception
{
    ClinicOutcomeForm form
=(ClinicOutcomeForm)actionForm;
    ApplicationException exp 
= new ApplicationException();        
    
//输入字段类型检验
    ValidationUtil.vldRequired(exp,form.getFillinDate(),"填表时间");
    ValidationUtil.vldRequired(exp,form.getFillinDoctor(),
"填表医师");
    ValidationUtil.vldMaxLength(exp,form.getDialyseResumeReason(),
40,"恢复透析原因");
    ValidationUtil.vldMaxLength(exp,form.getOutcomeReason(),
40,"转归原因");
        
    
//System.err.println(Calendar.getInstance().getTime());        
    
//System.err.println(Calendar.getInstance().get(Calendar.MONTH));        
    验证时间前后关系
            
        
    
//ValidateUtil中比较时间的函数精度不够,相差一天两天他比较不出来!
    ValidationUtil.compareDates(exp,form.getFillinDate(),DateUtil.getCurrentDate().toString(),"填表日期在今天之后,");
    ValidationUtil.compareDates(exp,form.getDialyseResumeDate(),DateUtil.getCurrentDate().toString(),
"恢复透析日期在今天之后,");
    ValidationUtil.compareDates(exp,form.getDialyseStartDate(),DateUtil.getCurrentDate().toString(),
"开始透析日期在今天之后,");
    ValidationUtil.compareDates(exp,form.getOutcomeDate(),DateUtil.getCurrentDate().toString(),
"转归日期在今天之后,");
    
if(form.getDialyseStartDate().length()!=0 &&  form.getOutcomeDate().length()!=0)
    
{        
        ValidationUtil.compareDates(exp,form.getDialyseStartDate(),form.getOutcomeDate(),
"开始透析日期在转归日期之后,");
    }

    
if(form.getOutcomeDate().length()!=0 && form.getDialyseResumeDate().length()!=0)
    
{
        ValidationUtil.compareDates(exp,form.getOutcomeDate(),form.getDialyseResumeDate(),
"转归日期在恢复透析日期之后,");
    }
        
    
if (exp.getErrorMsgList() != null{
        
throw exp;
    }

}
    

 

里面用到的函数compareDates都是一些实现检验功能的函数,也没有问题的。但是,问题来了!

比如一个输入框,数据表中对应类型BigDecimal,在表单中应该填入数字。但是,如果输入的内容不是数字,则通过form.get***取出来的内容都是null。这就导致当这个输入框不输入内容的时候,检验函数也把他当输入非法内容来处理,抛出异常。解决的办法就是强制每一项都是必填,但是,这是不合理的!有另外一个办法就是在定义数据表的时候,每一项的类型都定义为vchar。有点野蛮,不理想!

还想不到有好的方法来解决这个问题,怎么办呢?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值