Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I managed to solve the issue. I will explain how using a simple custom validator for age field, which must be > 18 for an Employee. It is supposed next that the validator was already declared in validators.xml and mapped on the action and that the message in case of ValidationException is "An employee should be older than 18.".</p> <p>Using Firebug, I figured out that the id of the error area in the form is FormError. It is possible to configure a callback function errorTextFormat in jqgrid, in order to get a response from the server and process it. In the jqgrid configuration, one could write</p> <pre><code> errorTextFormat : errorFormat, </code></pre> <p>with</p> <pre><code>var errorFormat = function(response) { var text = response.responseText; $('#FormError').text(text); //sets the text in the error area to the validation //message from the server return text; }; </code></pre> <p>The problem is now that the server will send implicitly a response containing the whole exception stack trace. To deal with it, I decided to create a new result type.</p> <pre><code>public class MyResult implements Result { /** * */ private static final long serialVersionUID = -6814596446076941639L; private int errorCode = 500; public void execute(ActionInvocation invocation) throws Exception { ActionContext actionContext = invocation.getInvocationContext(); HttpServletResponse response = (HttpServletResponse) actionContext .get("com.opensymphony.xwork2.dispatcher.HttpServletResponse"); Exception exception = (Exception) actionContext .getValueStack().findValue("exception"); response.setStatus(getErrorCode()); try { PrintWriter out = response.getWriter(); out.print(exception.getMessage()); } catch (IOException e) { throw e; } } /** * @return the errorCode */ public int getErrorCode() { return errorCode; } /** * @param errorCode the errorCode to set */ public void setErrorCode(int errorCode) { this.errorCode = errorCode; } } </code></pre> <p>It must also be configured in struts.xml as follows:</p> <pre><code>&lt;package name="default" abstract="true" extends="struts-default"&gt; ... &lt;result-types&gt; &lt;result-type name="validationError" class="exercises.ex5.result.MyResult"&gt; &lt;/result-type&gt; &lt;/result-types&gt; ... &lt;action name="myaction"&gt; ... &lt;result name="validationException" type="validationError"&gt;&lt;/result&gt; &lt;exception-mapping result="validationException" exception="java.lang.Exception"&gt;&lt;/exception-mapping&gt; &lt;/action&gt; ... &lt;/package&gt; </code></pre> <p>These are the steps I followed to get a validation error message in the add/edit window and now it works.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload