服务端的数据校验测试是成功的,可是客户端的校验总是不对,参考了struts官方文档和书,调试通过了,总结一下:
1.jsp页面的s:form表单添加validate="true" 并且action中的Action路径不需要加.action
2.确保form表单的主题是xhtml或其他支持客户端校验的主题,默认是xhtml,实在不行就在form表单加上theme="xhtml"(主要就是看主题包下面有没有validation.js这个文件了,查看主题包在struts-core这个jar包下的template.xhtml template.simple等等文件夹。)
3.引用官方文档的话
A correct action and namespace attributes must be provided to the <s:form> tag. For example, if the action named "quizClient" is defined in the "/validation" namespace, the form must be configured as:
如果quizClient定义在"/validation"命名空间下,那么表单的配置必须如下:
<s:form method="post" validate="true" action="quizClient" namespace="/validation"> <s:textfield label="Name" name="name"/> <s:textfield label="Age" name="age"/> <s:textfield label="Favorite color" name="answer"/> <s:submit/> </s:form>
While the following will "work" in the sense that the form will function correctly, client-side validation will not. That is because Struts must know the exact namespace and action (rather than a URL) to properly support validation.
如何按照以下配置,那么服务端校验是正常的,客户端校验确不行,struts必须知道action确切的命名空间而不仅仅是一个URL
<s:form method="post" validate="true" action="/validation/quizClient.action"> <s:textfield label="Name" name="name"/> <s:textfield label="Age" name="age"/> <s:textfield label="Favorite color" name="answer"/> <s:submit/> </s:form>
4.校验配置的xml文件中,不要使用key的方式引用国际化资源,会报错,使用${getText("pass.requried")}的表达式的方式来引用国际化资源,能正常显示。
说明namespace必须和url分开来写,才能进行准确的客户端校验