Can you tell me how can I get the action exceptions , convert and send them as json errors. I am using the strust2 jquery plugin to validate my forms with ajax. The framework will do it automatically by defining the sj:submit validate=true
and annotating the actions
<s:form action="login">
<s:textfield name="username" />
<s:textfield name="password" />
<sj:submit button="true" validate="true" />
</s:form>
The action is:
@Validations(requiredStrings =
{ @RequiredStringValidator(fieldName = "username", key = "validate.required"),
@RequiredStringValidator(fieldName = "password", key = "validate.required")}
public String confirm(){
service.canLogin(username,password)
}
The canLogin
may throws exception, eg. when it can not connect to database or shomething.
To serialze the excption I added below:
<global-results>
<result name="error.core" type="json">
<param name="ignoreHierarchy">false</param>
<param name="includeProperties">
actionErrors\[\d+\], fieldErrors\..+$,actionMessages\[\d+\]</param>
</result>
</global-results>
When I submit the form with invalid data, the form validates correctly. When an exception rasies the page is not serializing json results.
During debug I found that two json calls are made to server, one with:
- All form data's
- struts.enableJSONValidation
- struts.validateOnly
and one only with all for data's