页面代码:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib uri="/struts-tags" prefix="s" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <s:fielderror fieldName="p1"/> <form method="post" action="r.action"> <input name="p1" type="text"/> <input name="p2" type="text"/> <input type="submit"/> </form> </body> </html>
Action类代码:
package qnhost.gw.action; import com.opensymphony.xwork2.ActionSupport; public class TestAction extends ActionSupport{ @Override public String execute() throws Exception { return "OK"; } }
TestAction-r-validation.xml代码
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd"> <validators> <!-- 重复密码段校验--> <field name="p1"> <field-validator type="fieldexpression"> <param name="expression"> <![CDATA[(p1==p2)]]> </param> <message>two password should be the same! </message> </field-validator> </field> </validators>
现在的情况是不管怎么输入,较验都是成功的!是哪里错了?
补充下,如果把(p1==p2)换成p1.equals(p2),则不管怎么输入,校验都是不通过的。
(1)非字段校验:
<validator type="fieldexpression">
<param name="fieldName">myField</param>
<param name="expression"><![CDATA[#myCreditLimit > #myGirfriendCreditLimit]]></param>
<message>My credit limit should be MORE than my girlfriend</message>
</validator>
(2)字段校验:
<field name="myField">
<field-validator type="fieldexpression">
<param name="expression"><![CDATA[#myCreditLimit > #myGirfriendCreditLimit]]></param>
<message>My credit limit should be MORE than my girlfriend</message>
</field-validator>
</field>