struts2 validator后台校验?页面如何写?
展开全部
服务端校验可分为两种:1)编码校验 2)配置文件校验
楼上写的是配置文件的校验,我就写个编码校验吧 自己看看喜欢哪个就用哪个吧
UserAction继承了ActionSupport类,只要重写validate()方法便可实现编码校验
public void validate(){
if(null == username || "".equals(username)){
this.addFieldError("username","用户名不能为空 !");
return;
}else if(username.length() < 6 || username.length() > 12){
this.addFieldError("username","用户名长度必须为6-12位 !");
return;
}
if(null == password || "".equals(password )){
this.addFieldError("password ","密码不能为空!");
return;
}else if(password .length() < 6 || password .length() > 12){
this.addFieldError("password ","密码长度必须为6-12位!");
return;
}
}
楼上写的是配置文件的校验,我就写个编码校验吧 自己看看喜欢哪个就用哪个吧
UserAction继承了ActionSupport类,只要重写validate()方法便可实现编码校验
public void validate(){
if(null == username || "".equals(username)){
this.addFieldError("username","用户名不能为空 !");
return;
}else if(username.length() < 6 || username.length() > 12){
this.addFieldError("username","用户名长度必须为6-12位 !");
return;
}
if(null == password || "".equals(password )){
this.addFieldError("password ","密码不能为空!");
return;
}else if(password .length() < 6 || password .length() > 12){
this.addFieldError("password ","密码长度必须为6-12位!");
return;
}
}
展开全部
<%@ page contentType="text/html; charset=utf-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>
Test Struts2 Validator FrameWork
</title>
</head>
<body>
<p>This is Struts2 Validator FrameWork Test.</p>
<s:form action="POA902_afterValidate.html?menuId=1302"><!-- validate="true" -->
<table border="1px" width="800px">
<tr height="42px">
<td>
(非空):
</td>
<td>
<input type="text" name="value_notnull" value="${value_notnull}">
</td>
<td width="360px">
<font color="red"><s:fielderror><s:param>value_notnull</s:param></s:fielderror></font>
</td>
</tr>
<tr height="42px">
<td>
(0~10整数):
</td>
<td>
<input type="text" name="value_int" value="${value_int}">
</td>
<td width="360px">
<font color="red"><s:fielderror><s:param>value_int</s:param></s:fielderror></font>
</td>
</tr>
<tr height="42px">
<td>
(0~10小数):
</td>
<td>
<input type="text" name="value_double" value="${value_double}">
</td>
<td width="360px">
<font color="red"><s:fielderror><s:param>value_double</s:param></s:fielderror></font>
</td>
</tr>
<tr height="42px">
<td>
(email):
</td>
<td>
<input type="text" name="value_email" value="${value_email}">
</td>
<td width="360px">
<font color="red"><s:fielderror><s:param>value_email</s:param></s:fielderror></font>
</td>
</tr>
<tr height="42px">
<td>
(2011-01-03 12:45:36):
</td>
<td>
<input type="text" name="value_datetime" value="${value_datetime}">
</td>
<td width="360px">
<font color="red"><s:fielderror><s:param>value_datetime</s:param></s:fielderror></font>
</td>
</tr>
<tr height="42px">
<td>
(长度3):
</td>
<td>
<input type="text" name="value_length" value="${value_length}">
</td>
<td width="360px">
<font color="red"><s:fielderror><s:param>value_length</s:param></s:fielderror></font>
</td>
</tr>
<tr height="42px">
<td>
(字母数字下划线):
</td>
<td>
<input type="text" name="value_pwd" value="${value_pwd}">
</td>
<td width="360px">
<font color="red"><s:fielderror><s:param>value_pwd</s:param></s:fielderror></font>
</td>
</tr>
<tr height="42px">
<td>
(输入"error"有惊喜):
</td>
<td>
<input type="text" name="flag" value="${flag}">
</td>
<td width="360px">
<font color="red"><s:actionerror/></font>
</td>
</tr>
<tr height="42px">
<td>
(输入"msg"有惊喜):
</td>
<td>
<input type="text" name="msg" value="${msg}">
</td>
<td width="360px">
<font color="red"><s:actionmessage/></font>
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="提交">
</td>
</tr>
</table>
<table>
<s:iterator value="sampleData">
<tr>
<td>
<s:property/>
</td>
</tr>
</s:iterator>
</table>
</s:form>
</body>
</html>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>
Test Struts2 Validator FrameWork
</title>
</head>
<body>
<p>This is Struts2 Validator FrameWork Test.</p>
<s:form action="POA902_afterValidate.html?menuId=1302"><!-- validate="true" -->
<table border="1px" width="800px">
<tr height="42px">
<td>
(非空):
</td>
<td>
<input type="text" name="value_notnull" value="${value_notnull}">
</td>
<td width="360px">
<font color="red"><s:fielderror><s:param>value_notnull</s:param></s:fielderror></font>
</td>
</tr>
<tr height="42px">
<td>
(0~10整数):
</td>
<td>
<input type="text" name="value_int" value="${value_int}">
</td>
<td width="360px">
<font color="red"><s:fielderror><s:param>value_int</s:param></s:fielderror></font>
</td>
</tr>
<tr height="42px">
<td>
(0~10小数):
</td>
<td>
<input type="text" name="value_double" value="${value_double}">
</td>
<td width="360px">
<font color="red"><s:fielderror><s:param>value_double</s:param></s:fielderror></font>
</td>
</tr>
<tr height="42px">
<td>
(email):
</td>
<td>
<input type="text" name="value_email" value="${value_email}">
</td>
<td width="360px">
<font color="red"><s:fielderror><s:param>value_email</s:param></s:fielderror></font>
</td>
</tr>
<tr height="42px">
<td>
(2011-01-03 12:45:36):
</td>
<td>
<input type="text" name="value_datetime" value="${value_datetime}">
</td>
<td width="360px">
<font color="red"><s:fielderror><s:param>value_datetime</s:param></s:fielderror></font>
</td>
</tr>
<tr height="42px">
<td>
(长度3):
</td>
<td>
<input type="text" name="value_length" value="${value_length}">
</td>
<td width="360px">
<font color="red"><s:fielderror><s:param>value_length</s:param></s:fielderror></font>
</td>
</tr>
<tr height="42px">
<td>
(字母数字下划线):
</td>
<td>
<input type="text" name="value_pwd" value="${value_pwd}">
</td>
<td width="360px">
<font color="red"><s:fielderror><s:param>value_pwd</s:param></s:fielderror></font>
</td>
</tr>
<tr height="42px">
<td>
(输入"error"有惊喜):
</td>
<td>
<input type="text" name="flag" value="${flag}">
</td>
<td width="360px">
<font color="red"><s:actionerror/></font>
</td>
</tr>
<tr height="42px">
<td>
(输入"msg"有惊喜):
</td>
<td>
<input type="text" name="msg" value="${msg}">
</td>
<td width="360px">
<font color="red"><s:actionmessage/></font>
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="提交">
</td>
</tr>
</table>
<table>
<s:iterator value="sampleData">
<tr>
<td>
<s:property/>
</td>
</tr>
</s:iterator>
</table>
</s:form>
</body>
</html>
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
给你 一个user的,其它自己写。只处理了非空
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" "">
<validators>
<field name="name">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>用户名不能为空!</message>
</field-validator>
</field>
<field name="age">
<field-validator type="requiredstring">
<message>年龄不能为空!</message>
</field-validator>
</field>
</validators>
注意需要在和action同一个包下。
注意该XML的命名需要是UserAction-validation.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" "">
<validators>
<field name="name">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>用户名不能为空!</message>
</field-validator>
</field>
<field name="age">
<field-validator type="requiredstring">
<message>年龄不能为空!</message>
</field-validator>
</field>
</validators>
注意需要在和action同一个包下。
注意该XML的命名需要是UserAction-validation.xml
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-01-06
展开全部
function testisNum(object)
{
var s =document.getElementById(object.id).value;
if(s!="")
{
if(isNaN(s))
{
alert("请输入数字");
object.value="";
object.focus();
}
}
}
{
var s =document.getElementById(object.id).value;
if(s!="")
{
if(isNaN(s))
{
alert("请输入数字");
object.value="";
object.focus();
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询