3

I have implemented model driven validation in my application but the validation does not work with following warning.

WARNING: The visited object is null, VisitorValidator will not be able to handle validation properly. Please make sure the visited object is not null for VisitorValidator to function properly

Any idea why?

Here is my Action class.

package actions;

import beans.CarListing;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class ListCarAction extends ActionSupport implements ModelDriven{
    public String execute() {
        System.out.println("ListCarAction x" + carListing.getUrl());
        return SUCCESS;
    }

    private CarListing carListing = new CarListing();

    public Object getModel() {
        return carListing;
    }
}

Here is my ListCarAction-validation.xml

<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN"
    "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
<validators>
    <field name="model">
        <field-validator type="visitor">
            <param name="appendPrefix">false</param>
            <message>Car Listing: </message>
        </field-validator>
    </field>
</validators>

And here is the bean validator XML named CarListing-validation.xml.

<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN"
    "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
<validators>
    <field name="url">
        <field-validator type="requiredstring">
            <message>URL is required field.</message>
        </field-validator>
        <field-validator type="stringlength">
            <param name="minLength">1</param>
            <param name="minLength">30</param>
            <message>The URL must be at least 1-30 characters.</message>
        </field-validator>
    </field>
</validators>
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
Zaheer Baloch
  • 536
  • 3
  • 9
  • 28
  • what is printed in the console? – Roman C Jul 22 '14 at 18:45
  • This is printed in the comment and success result is shown for the action.. Jul 22, 2014 10:58:39 PM com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn WARNING: The visited object is null, VisitorValidator will not be able to handle validation properly. Please make sure the visited object is not null for VisitorValidator to function properly ListCarAction x – Zaheer Baloch Jul 22 '14 at 18:58
  • We don't now what is a visited object and you should debug it to tell us what is it. – Roman C Jul 22 '14 at 19:17
  • According to Struts2, if an action is using ModelDriven beans to provide automatic assignment of variables from forms then the visited is used in the action-validation.xml to refer to action's model. For now I am able to make the validations work by declaring the domain object as a simple bean within the action. Does this make sense? – Zaheer Baloch Jul 22 '14 at 23:19
  • +1, Actually it's a very good question. Please post your Interceptor Stack – Andrea Ligios Jul 23 '14 at 08:56

1 Answers1

1
  1. Ensure you are using a correctly configured Interceptor Stack for your Action, including all of the following interceptors in this order: modelDriven, params, validation. There are others interceptors among them, it's only important that they're not switching position with each other)

  2. Make sure that the CarListing-validation.xml file is put in the beans package, along the CarListing.class file, not in the actions package, where instead there is the other xml file.

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • I am unable to understand first point. Can you please explain? – NITIN RATHOUR Jun 09 '15 at 10:22
  • 1
    Struts2 filters all your requests through a stack of interceptors, each one performing a different job, many of them provided by Struts2, some of them eventually created by you. When you call an action, you are actually calling Int1, Int2, IntN, Action, IntN, Int2, Int1. If you don't touch anything, you are using the [defaultStack](https://struts.apache.org/docs/struts-defaultxml.html). If you define a stack of your, in my first point I was highlighting that you must pay attention to the order of those three interceptors. Is it clear enough ? :) – Andrea Ligios Jun 09 '15 at 10:28
  • I got what you want to say. But my problem is not solved. Please look at http://stackoverflow.com/questions/30746955/modeldriven-validation-struts2-using-xml – NITIN RATHOUR Jun 10 '15 at 04:12
  • You're right @NITINRATHOUR, there was an error in this answer, I've updated it and answered the one you linked. Feel free to upvote them if you found them useful, thanks. – Andrea Ligios Jun 10 '15 at 09:01