3

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>
Roman C
  • 49,761
  • 33
  • 66
  • 176
Neha arora
  • 85
  • 8

2 Answers2

2

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.

Community
  • 1
  • 1
Roman C
  • 49,761
  • 33
  • 66
  • 176
1

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

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • Thanks for you response Andrea.I have understood the concept,but it is not working as per my requirements.Will update you further on this...Thanks again. – Neha arora Nov 26 '14 at 20:53
  • Andrea...I have done some modification in the test.jsp..please have a look.Now on click of button,I am calling action through a javasccript as I want to display chart in new window.So what is happening that when I click on the button ,it validates as per logic in Action (validate method),but same page opens in popup and error message is displayed there instead of original page.So what I need is..on click of button,if validation fails,it should not open pop up but should display error message on same page.. – Neha arora Nov 29 '14 at 23:07
  • Andrea..Do you have any suggestion pls. – Neha arora Nov 30 '14 at 16:18
  • Sounds like a new question to me :) BTW I'm on the phone now, open a new question and I'll try to answer tomorrow – Andrea Ligios Nov 30 '14 at 16:56