有一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 />
<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> </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=' ';"> 保存 </a></p>
</td>
<td nowrap> </td>
<td class="pushButton" nowrap>
<p class="pushButton"><a href=""
target="_self" class="pushButton" title="撤消所有更改"
onmouseover="status=' ';return true;" onmouseout="status=' ';"> 撤消所有更改 </a></p>
</td>
<td nowrap> </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=' ';"> 审核 </a></p>
</td>
</c:if>
<td class="pushButton" nowrap>
<p class="pushButton"><a href="javascript:window.history.back()" target="_self" class="pushButton" title="返回" > 返回 </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。有点野蛮,不理想!
还想不到有好的方法来解决这个问题,怎么办呢?
1388

被折叠的 条评论
为什么被折叠?



