Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Struts 2 Validator Question

 
Kelvin Tan
Greenhorn
Posts: 10
Python Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

I have this question about the validator in the Struts2 framework.

Say I have the following,

Action Class:
Book.java


Video.java


Book-validation.xml (with the following codes)


add.jsp



struts.xml



Now the question is, why does the validator validates the field "title" that in the Add Video section? Doesn't the validator only validate fields that
will be invoked by the addBook.action only?

I'm confused by how the validator works, (sorry I only picked up Struts 2 recently and has no prior experience with any J2EE framework) hopefully I can get some help from the Ranch.

Thank you guys.
Best Regards.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you saying that if you click on the "Submit Media" button it's validating the title? I'm a little skeptical, since the Video class doesn't extend ActionSupport and shouldn't support validation at *all*.
 
Kelvin Tan
Greenhorn
Posts: 10
Python Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your reply David.

The problem is whenever the "Submit Publication" button is clicked, the validator also validates the "title" field for the Video section. Which is something I don't understand.

Since the validation xml is named under "Book-validation.xml", isn't it suppose to validate only fields that belong to the Book action class? Or am I having the wrong concept, that the validation just looks at the field name in the jsp and validate it as long as it matches?

 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not validating the video title--it's displaying the validation message for book title, because you have no way of differentiating between the two title fields. S2 validation is field-based, not form-based. You have the same field name in two forms. The action's fieldErrors() map is at the page level (not form). So the <s:textfield> tag checks for "title" errors--which it finds. Twice.
 
Kelvin Tan
Greenhorn
Posts: 10
Python Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh..ok so I understand.

Thank you for your help.

Another question, is there any solution to get round this problem?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Name the fields differently? Use a bean-per-form? Don't use two forms on the same page? Use two submit buttons and a single form?
 
Kelvin Tan
Greenhorn
Posts: 10
Python Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much David.

Appreciate it =)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic