Avoiding Struts2 Validation

I have index.jsp

one from which I am calling an action class TestAction

(when the hyperlink is clicked) that has a method (display) to load the values ​​for the combobox from the database, along with the execute method, to be displayed on the page test.jsp

.

In test.jsp

I have multiple input fields along with combo.On fields on a button click on test.jsp I want to validate the input fields, but the problem is when I go from index.jsp it is only time validation and test.jsp

error messages are opened.

I tried client side validation using <ActionName>-validator.xml

as well as server side validation overriding the validation method. How to avoid the check when clicking the hyperlink on index.jsp and do it by clicking the button on test.jsp.

Here is my code:

index.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
   pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
   <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Data</title>

</head>
<body>
<s:form>

   <a href="<s:url action="displayAction.action"/>" >Display Data</a><br>

</s:form>
</body>
</html>

      

test.jsp:

     <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
           pageEncoding="ISO-8859-1"%>
        <%@ taglib prefix="s" uri="/struts-tags"%>
        <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
        "http://www.w3.org/TR/html4/loose.dtd">
        <html>
        <head>
        <title>Data</title>
     <script type="text/javascript">


           function openPopUp(){
                document.getElementById('testForm').setAttribute('target', '_blank');
                document.getElementById('testForm').setAttribute('action', 'testAction.action');
            }
            </script>
        <s:head theme="ajax" debug="true"/>
        </head>
        <body bgcolor="white">

        <s:form validate="true" id="testForm">
        <div>
      <s:actionerror/>
   </div>
        <table>

        //Mapping of data from database

        </table>
        <s:submit id="submitButton" value="Display Chart" align="center" onclick="openPopUp();"/>
        </s:form>
        </body>
        </html>

      

struts.xml

<action name="testAction" 
        class="testAction" 
        method="execute">
             <result name="success" type="chart">
                <param name="value">chart</param>
                <param name="type">jpeg</param>
                <param name="width">600</param>
                <param name="height">400</param>
            </result> 
</action>

<action name="displayAction" 
        class="testAction" 
        method="display">
            <result name="success">test.jsp</result>
</action>

      

+3


source to share


2 answers


I tried client side validation with -validator.xml

as well as server side validation by overriding the validation method.

They are both server side checks, both are performed by a validation interceptor, and can be configured to run only on an action alias .

Then with your struts.xml:

<action name="testAction" 
       class="testAction" 
      method="execute">
...
</action>

<action name="displayAction" 
       class="testAction" 
      method="display">
...
</action>

      



And if you only want to check the first action, you can do the following:

  • Use testAction-testAction-validation.xml

    (action name + action alias) rather than just testAction-validation.xml

    ;

  • Use method validateTestAction(){}

    instead validate()

    .

Also see how things work , because you haven't defined any INPUT result, and that's suspicious :)

+1


source


If you don't want to validate any action, just put the annotation @SkipValidation

in the action method. This answer shows how to use this annotation and this answer shows an alternative approach .



+2


source







All Articles