Reputation: 1217
I am wondering if anybody can throw any ideas on the following.
Got an Action
class with validate()
method returning JSP input
result with actionErrors
, fieldErrors
when validations fail.
I would like to get only the validation errors (JSON data?) and display the errors on the existing page and highlight the fields, rather than refreshing a whole JSP with the new response JSP.
JQuery is used on client side, Struts 2 as MVC framework, Liferay as Portal server.
The following is the code that I tried:
public void validate() {
setActionErrors(validateData(this));
}
struts.xml
:
<result-types>
<result-type name="json" class="org.apache.struts2.json.JSONResult"/>
</result-types>
<interceptors>
<interceptor name="json" class="org.apache.struts2.json.JSONInterceptor"/>
</interceptors>
<action name="saveDataAsync" class="MyActionClass" method="addDataAsync">
<result name="success">/jsp/addDataAsync.jsp</result>
<result name="input" type="json">
</result>
</action>
JSP:
<portlet:actionURL name="saveData" var="saveDataActionURL">
<portlet:param name="struts.portlet.action" value="/saveDataAsync"/>
</portlet:actionURL>
JS:
jQuery.ajax({
type: 'POST',
url: "<%=saveDataActionURL%>",
data: jQuery("#form").serialize(),
dataType: 'json',
success: function(data) {
alert("data" + data);
}
});
After execution of validate()
method, the flow is executing a default method of action class, even when there are errors added to errorlist, and set through setActionErrors()
method. I could see resultcode: as "input"
and result: as "jsonresult"
when the interceptors and invoke()
methods get called.
I really appreciate any help on this problem.
Upvotes: 0
Views: 1772
Reputation: 1
You can configure INPUT
result to your action of type json
and use includeProperties
parameter to specify generated JSON.
<result name="input" type="json">
<param name="ignoreHierarchy">false</param>
<param name="includeProperties">^actionErrors\[\d+\],^fieldErrors\['\w+'\]\[\d+\]</param>
</result>
The action class should extend ActionSupport
, so these properties will be available.
Upvotes: 1