javajspvalidationstruts2

How to avoid Struts 2 validation?


I have a index.jsp, from which I am calling an action class TestAction (on click of hyperlink), which have a method display() to load values for combobox from database along with execute() method, to display on page test.jsp.

On test.jsp I have some input fields along with combo boxes. On a click of a button in test.jsp, I want to validate input fields, but problem is that, when I am coming from index.jsp, that time only validation is happening and test.jsp opens with error messages.

I tried both client side validation using <ActionName>-validator.xml as well as used a server side validation, by overriding validate method. How can I avoid a validation on click of the hyperlink in index.jsp and do it on button click in 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>

Solution

  • I tried both client side validation using -validator.xml as well as used server side validation by overriding validate method.

    They're both server-side validations, both performed by the Validation Interceptor, and both can be tweaked to be run for an action alias only.

    Then with your struts.xml:

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

    And assuming that you want to validate only the first action, you can do the following:

    1. Use a testAction-testAction-validation.xml (action name + action alias), instead of just testAction-validation.xml;

    2. Use a validateTestAction(){} method instead of just validate().

    Also take a look at how the whole thing works, because you have not defined any INPUT result, and this is suspicious :)