问题描述
我正在 validate() 方法中进行验证.
I am doing validation inside validate() method.
public void validate(){
if(continent.equals("-1")){
HttpServletRequest request=ServletActionContext.getRequest();
HttpSession session=request.getSession();
String s=(String)session.getAttribute("operation");
if(s.equals("edit"))
edit();
else if(s.equals("add"))
add();
addFieldError( "Continent", "Continent must be selected");
}
}
并在jsp(view)中添加表单属性validate=true
And in jsp(view) added form attribute validate=true
<s:form action="add" name="aaa" cssClass="yy" method="post" validate="true">
<s:textfield name="Code" label="Code" readonly="false" cssClass="defaultTextBox"/>
<s:textfield name="Name" label="Name" cssClass="defaultTextBox"/>
<s:select name="Continent" label="Continent" headerKey="-1" headerValue="Select" list="continentlist" cssClass="defaultTextBox"/>
<s:textfield name="IndepYear" label="Independance Year" cssClass="defaultTextBox" />
<s:submit value="Save" cssClass="login login-submit" theme="simple"/>
</s:form>
但只有服务器端验证有效.我的问题是 --> 是否可以使用 validate() 方法添加客户端验证?
But only server side validation is working. My question is -->is it not possible to add client side validation using validate() method?
推荐答案
在 Struts 2 中,Client Side Validation 有不同的含义,完全取决于 您正在使用的主题.
In Struts 2, Client Side Validation has different meanings, and totally depends on the type of theme you are using.
使用
XHTML
(默认)和CSS XHTML
,您可以使用
With
XHTML
(default) andCSS XHTML
, you can use the
这完全是客户端,基于 Javascript,不与服务器通信.
that is totally client side, Javascript based and doesn't communicate with the server.
使用 AJAX
主题,您可以运行
With the AJAX
theme instead, you can run the
这将联系服务器,运行整个验证堆栈,并且(回答您的问题)也运行您的 validate()
方法.
that will contact the server, running the whole validation Stack, and (to answer your question) running your validate()
methods too.
我个人更喜欢使用 SIMPLE
主题,完全自己处理 HTML、CSS 和 JavaScript.
I personally prefer to use the SIMPLE
theme, completely handling the HTML, the CSS and the JavaScript on my own.
由于服务器端验证是强制性的,客户端验证被认为只是多余的,有利于使页面更加用户友好,并减少高用户环境下的网络流量(您阻止不成功 -但合法 - 在他们通过电线之前请求:)
Since the server-side validation is mandatory, the client-side validation is to be considered just a surplus, positive for making the page more user-friendly, and to reduce the network traffic in high users environment (you block unsuccessfull - but legit - requests before they go through the wire :)
考虑使用 HTML5 类型和 jQuery 回退,尤其是当您面向移动设备时.
Consider using HTML5 types with fallback on jQuery, especially if you are targeting the mobile.
这篇关于客户端验证无法使用 validate()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!