You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Steven Hansen <ru...@berkeley.edu> on 2009/04/30 21:11:59 UTC

Struts 2: Redisplaying Invalid Form and Displaying Errors


Hi,

I'm new to Struts 2 and am still trying to figure out some of the basics.

The first problem I'm having, is with an action that uses ModelDriven.  
The model that I'm using has 2 fields that are both required, one is a 
string and one is an int.  I'm stuck on two things.  The first is that 
when validation fails, no error messages are being displayed.  I was 
under the impression that the <s:textfield> tag should be able to 
determine when the model has an error on its matching field (via the 
value stack) and display and error message.  If I uses attributes in my 
Action instead of ModelDriven, then the textfield tag display error 
messages correctly.

The next problem, is that if I don't enter a value for the Age field 
(int), when the form is redisplayed the Age field has been populated 
with 0.  If the Age field is left empty, I'd like the field to stay 
empty when the form is redisplayed.

The model, action and view I'm using are below.  Any help is greatly 
appreciated.

Thanks,
Steven



@Validation
public class User {

    private int age;
    private String name;   
   
   @RequiredFieldValidator(message="Age is required.")
    public int getAge() {
        return age;
    }

    public void setAge(int _age) {
        this.age = _age
    }
   
    @RequiredFieldValidator(message="Name is required.")
    public int getName() {
        return this.name;
    }
   
    public void setName(int _name) {
        this.name = _name;
    }  
}


Action:

@Validation
public class CreateUserAction extends ActionSupport implements 
ModelDriven<User> {
   
    private User user = new User();
   
    @VisitorFieldValidator(message="")
    public User getModel() {
        return this.user;
    }

    public String execute() {
        return "input";
    }

}


View:

  <h2>New User</h2>
  <s:form action="create">
    <s:textfield name="age" label="Age" required="true"/> 
    <s:textfield name="name" label="Name" required="true"/>   
    <s:submit/>
  </s:form> 





---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Struts 2: Redisplaying Invalid Form and Displaying Errors

Posted by Steven Hansen <ru...@berkeley.edu>.
Thanks Dave,

Your suggestion fixed it!

Best,
Steven



Dave Newton wrote:
> Steven Hansen wrote:
>> The next problem, is that if I don't enter a value for the Age field 
>> (int), when the form is redisplayed the Age field has been populated 
>> with 0.  If the Age field is left empty, I'd like the field to stay 
>> empty when the form is redisplayed.
>
> Use Integer, not int: the default value for an "int" is 0; the default 
> value for an Integer is null. The form field will display the value of 
> the property.
>
> (Not sure about the first one and can't check now.)
>
> Dave
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Struts 2: Redisplaying Invalid Form and Displaying Errors

Posted by Dave Newton <ne...@yahoo.com>.
Steven Hansen wrote:
> The next problem, is that if I don't enter a value for the Age field 
> (int), when the form is redisplayed the Age field has been populated 
> with 0.  If the Age field is left empty, I'd like the field to stay 
> empty when the form is redisplayed.

Use Integer, not int: the default value for an "int" is 0; the default 
value for an Integer is null. The form field will display the value of 
the property.

(Not sure about the first one and can't check now.)

Dave


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Struts 2: Redisplaying Invalid Form and Displaying Errors

Posted by dusty <du...@yahoo.com>.
I think for modelDriven you need to set the appendPrefix parameter of the
Visitor validator to false.  Essentially, struts is adding an error to
fieldErrors like ("model.field","error message") rather than
("field","errormessage") and so the freemarker templates can't match up your
fieldname to any entries in the fieldErrors map.



Steven Hansen wrote:
> 
> 
> 
> Hi,
> 
> I'm new to Struts 2 and am still trying to figure out some of the basics.
> 
> The first problem I'm having, is with an action that uses ModelDriven.  
> The model that I'm using has 2 fields that are both required, one is a 
> string and one is an int.  I'm stuck on two things.  The first is that 
> when validation fails, no error messages are being displayed.  I was 
> under the impression that the <s:textfield> tag should be able to 
> determine when the model has an error on its matching field (via the 
> value stack) and display and error message.  If I uses attributes in my 
> Action instead of ModelDriven, then the textfield tag display error 
> messages correctly.
> 
> The next problem, is that if I don't enter a value for the Age field 
> (int), when the form is redisplayed the Age field has been populated 
> with 0.  If the Age field is left empty, I'd like the field to stay 
> empty when the form is redisplayed.
> 
> The model, action and view I'm using are below.  Any help is greatly 
> appreciated.
> 
> Thanks,
> Steven
> 
> 
> 
> @Validation
> public class User {
> 
>     private int age;
>     private String name;   
>    
>    @RequiredFieldValidator(message="Age is required.")
>     public int getAge() {
>         return age;
>     }
> 
>     public void setAge(int _age) {
>         this.age = _age
>     }
>    
>     @RequiredFieldValidator(message="Name is required.")
>     public int getName() {
>         return this.name;
>     }
>    
>     public void setName(int _name) {
>         this.name = _name;
>     }  
> }
> 
> 
> Action:
> 
> @Validation
> public class CreateUserAction extends ActionSupport implements 
> ModelDriven<User> {
>    
>     private User user = new User();
>    
>     @VisitorFieldValidator(message="")
>     public User getModel() {
>         return this.user;
>     }
> 
>     public String execute() {
>         return "input";
>     }
> 
> }
> 
> 
> View:
> 
>   <h2>New User</h2>
>   <s:form action="create">
>     <s:textfield name="age" label="Age" required="true"/> 
>     <s:textfield name="name" label="Name" required="true"/>   
>     <s:submit/>
>   </s:form> 
> 
> 
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Struts-2%3A-Redisplaying-Invalid-Form-and-Displaying-Errors-tp23322541p23348666.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org