简体   繁体   中英

How to avoid Struts2 validation

I have a index.jsp from which I am calling an action class TestAction (on click of hyperlink) which have 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 click of button on 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 server side validation by overriding validate method.How can avoid validation on click of hyperlink on index.jsp and can do it on button click 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>

If you don't want to validate some action, just put @SkipValidation annotation on the action method. This answer shows how to use this annotation , and this answer shows an alternative approach .

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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM