Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » Struts 2 »

Validation on java.util.List

Alex Wibowo

2007-03-13

Replies:

Hi all...

I am using Struts 2.0. Has anyone ever done validation on individual item in
java.util.List before?

Say, I have a property
        List<Customer> customers
in my Action, and Customer is defined as:

public class Customer{
  private String username;     // must not be empty
  private int    age;           // must be > 0
}

In the JSP I have the following:


<c:forEach begin="0" end="10" var="i">
  <s:textfield key="customers[${i}].username" />
  <s:textfield key="customers[${i}].age" />
</c:forEach>


How can I enforce using struts validation (preferrably through annotation)
so that:
- both username and age are compulsory
- age must be an integer


Secondly, since "age" is of type "int".. when user enter characters
(numbers) in the textfield, they get converted to 0 (somehow).
How can I prevent this?



Thanks in advance!



--
Best regards,


WiB
-------------------
©2008 junlu.com - Jax Systems, LLC, U.S.A.