You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Tauri Valor <su...@gmail.com> on 2008/03/17 08:41:07 UTC

Struts2 Annotation based Validation

Using Annotation based Validation, I want to try the following:

The Validation Error message can be retrieved in our jsp using :

<s:actionerror />
<s:fielderror /> 

Do I have any alternative method of using the above tags ? 
For eg. can I retrieve the error using session.getAttribute(actionerror) ? 

Are actionerror and fielderror stored in a request or a session object ?

Please help me understand this .

Thanks,
Tauri.

-- 
View this message in context: http://www.nabble.com/Struts2-Annotation-based-Validation-tp16089678p16089678.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


Re: Struts2 Annotation based Validation

Posted by Tauri Valor <su...@gmail.com>.
Thanks Lukasz!

I tried request.getAttribute("actionerror") and
request.getAttribute("fielderror") in my jsp but it did not work.

Am I doing it the wrong way ? 









Lukasz Lenart wrote:
> 
> Hi,
> 
> Error messages are stored in request, you have to subclass
> ActionSupport and add some mechanism to store messages in session.
> 
> 
> Regards
> -- 
> Lukasz
> 
> http://www.linkedin.com/in/lukaszlenart
> 
> ---------------------------------------------------------------------
> 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/Struts2-Annotation-based-Validation-tp16089678p16090926.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


Re: Struts2 Annotation based Validation

Posted by Tauri Valor <su...@gmail.com>.
I get the following log in my console:


2008-Mar-18 16:21:17,671 - DEBUG freemarker.cache - Could not find template
in cache, creating new one;
id=[template/xhtml/fielderror.ftl[en_US,UTF-8,parsed] ]
2008-Mar-18 16:21:17,671 - DEBUG freemarker.cache -
template/simple/fielderror.ftl[en_US,UTF-8,parsed] cached copy not yet
stale; using cached.



Does it have anything to do with caching of the page?


Thanks,
Tauri.




Tauri Valor wrote:
> 
> Hi Lukasz,
> 
> Thanks for responding..
> 
> Snippet of Jsp:
> 
> 
> <form name="myForm" method="post" action="MyAction.do" validate="true"/> 
> 
> 
> <tr>
> 									<td width="115">First Name</td>
> 									<td width="142"><input type="text" name="firstName"
> id="firstName" class="form_field"></td>
> 									
> 									<td>
> 									
> 									<s:fielderror>
> 									  <s:param>firstName</s:param>
> 									</s:fielderror>
> 									</td>
> 	
> 
> 
> My Struts-config:
> 
> <action name="MyAction" class="activationPendingAction">
> 			<result name="input" type="tiles">MyForm</result>
> 			<result name="failure" type="tiles">MyForm</result>
> 			<result name="success" type="tiles">SuccessPage</result>
> 		</action>
> 
> 
> 
> 
> 
> Thats the snippet of my code.
> 
> 
> Thanks,
> Tauri.
> 
> 
> Lukasz Lenart wrote:
>> 
>>>  But when I enter the value in the textbox and click submit again, the
>>>  validation error for next is displayed correctly but entered value
>>>  disappears in the textbox which is abnormal .
>> 
>> Maybe you are redirecting to the next page? Could you paste snippet
>> for your struts.xml and jsp?
>> 
>> 
>> Regards
>> -- 
>> Lukasz
>> 
>> ---------------------------------------------------------------------
>> 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/Struts2-Annotation-based-Validation-tp16089678p16116284.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


Re: Struts2 Annotation based Validation

Posted by Lukasz Lenart <lu...@googlemail.com>.
>  > Im not sure where to place my properties file.

I almost always put my messages to file named package.properties in
the same folder as my action's classes.


Regards
-- 
Lukasz

http://www.linkedin.com/in/lukaszlenart

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


Re: Struts2 Annotation based Validation

Posted by Dave Newton <ne...@yahoo.com>.
--- Tauri Valor <su...@gmail.com> wrote:
> I tried as you advised. I get the default error message 'Please enter a
> value for first name ' which is in the "message" .
> 
> Im expecting the error messaage from the properties file.
> 
> I think Im placing my properties file in the wrong location. And therefore
> my validation returns me the default error message.
> 
> Im not sure where to place my properties file.

Depends. it can be in the same ackage as the action class and named
${actionClassName}.properties, for example. The localization docs [1] gives a
set of rules for locating resource files.

Dave

[1] http://struts.apache.org/2.0.11.1/docs/localization.html



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


Re: Struts2 Annotation based Validation

Posted by Tauri Valor <su...@gmail.com>.
Lukasz, 

I tried as you advised. I get the default error message 'Please enter a
value for first name ' which is in the "message" .

Im expecting the error messaage from the properties file.

I think Im placing my properties file in the wrong location. And therefore
my validation returns me the default error message.

Im not sure where to place my properties file.

Thanks,
Tauri.


Lukasz Lenart wrote:
> 
> You mess too many thing, please try like this:
> 
>  @RequiredStringValidator(key = "error.required", message = "Please 
> enter a value for First name")
> 
> error.required = ${fieldName} is required!
> 
> 

-- 
View this message in context: http://www.nabble.com/Struts2-Annotation-based-Validation-tp16089678p16320151.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


Re: Struts2 Annotation based Validation

Posted by Lukasz Lenart <lu...@googlemail.com>.
Hi,

You mess too many thing, please try like this:

 @RequiredStringValidator(key = "error.required", message = "Please
enter a value for First name")

error.required = ${fieldName} is required!



Regards
-- 
Lukasz

http://www.linkedin.com/in/lukaszlenart

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


Re: Struts2 Annotation based Validation

Posted by Tauri Valor <su...@gmail.com>.
Thanks again.

I tried the following:

In my action:
@RequiredStringValidator(key = "error.message", message = "")
	public String getFirstName() {
		return firstName;
	}


I created the properties file just under my action with the name
"MyActionName.properties" with the following contents:

error.message = ${getText(firstName)} is required.
fieldFormat = ${getText(fieldName)} is not formatted properly.

But I do not the error message as: "error.message" instead of "firstName is
required"

Where am I erring ?

Thanks,
Tauri






Lukasz Lenart wrote:
> 
>>  Is it possible for me to have multilingual Validations using Annotation
>>  Based Validations in Struts2 ?
> 
> Yes, you can, just add key = some.error.message to your annotation, like
> below
> 
>     @RequiredStringValidator(key = "some.key")
>     public void setFirstName(String firstName) {
>         this.firstName = firstName;
>     }
> 
> 
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Struts2-Annotation-based-Validation-tp16089678p16297696.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


Re: Struts2 Annotation based Validation

Posted by Lukasz Lenart <lu...@googlemail.com>.
>  Is it possible for me to have multilingual Validations using Annotation
>  Based Validations in Struts2 ?

Yes, you can, just add key = some.error.message to your annotation, like below

    @RequiredStringValidator(key = "some.key")
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }


Regards
-- 
Lukasz

http://www.linkedin.com/in/lukaszlenart

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


Re: Struts2 Annotation based Validation

Posted by Tauri Valor <su...@gmail.com>.
Hi

Is it possible for me to have multilingual Validations using Annotation
Based Validations in Struts2 ?

Please let me know how I can achieve this .

Thanks,
Tauri.





Tauri Valor wrote:
> 
> Thanks Lukasz
> 
> 
> 
> Lukasz Lenart wrote:
>> 
>>> <td><s:password name="pwd" label="Your Password"/></td>
>> 
>> By default s:password tag don't shows value of your password, you can
>> change such behaviour by adding attribute showPassword=true, but you
>> should consider security issue.
>> 
>> 
>> Regards
>> -- 
>> Lukasz
>> 
>> http://www.linkedin.com/in/lukaszlenart
>> 
>> ---------------------------------------------------------------------
>> 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/Struts2-Annotation-based-Validation-tp16089678p16296680.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


Re: Struts2 Annotation based Validation

Posted by Tauri Valor <su...@gmail.com>.
Thanks Lukasz



Lukasz Lenart wrote:
> 
>> <td><s:password name="pwd" label="Your Password"/></td>
> 
> By default s:password tag don't shows value of your password, you can
> change such behaviour by adding attribute showPassword=true, but you
> should consider security issue.
> 
> 
> Regards
> -- 
> Lukasz
> 
> http://www.linkedin.com/in/lukaszlenart
> 
> ---------------------------------------------------------------------
> 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/Struts2-Annotation-based-Validation-tp16089678p16296676.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


Re: Struts2 Annotation based Validation

Posted by Lukasz Lenart <lu...@googlemail.com>.
> <td><s:password name="pwd" label="Your Password"/></td>

By default s:password tag don't shows value of your password, you can
change such behaviour by adding attribute showPassword=true, but you
should consider security issue.


Regards
-- 
Lukasz

http://www.linkedin.com/in/lukaszlenart

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


Re: Struts2 Annotation based Validation

Posted by Tauri Valor <su...@gmail.com>.
Thanks Lukasz it worked for textfields using <s:textfield>

But I have the same problem when I use it for <s:password>

this is my listbox:


		
		<tr>
				<td> <s:select label="Country" name="country" headerKey="1"
headerValue="Please Select" 
				list="#{'AU':'Australia',				
				'CN':'China',
				'HK':'Hong Kong',
				'IN':'India',
				'ID':'Indonesia',
				'JP':'Japan',
				'MY':'Malaysia',
				'NZ':'New Zealand',
				'PL':'Philippines',
				'SG':'Singapore',
				'KR':'South Korea',
				'TW':'Taiwan',
				'TH':'Thailand'}"/></td>
			</tr>


And my password fields:

	<tr>
				<td><s:password name="pwd" label="Your Password"/></td>
			</tr>

			<tr>
				<td><s:password name="confirmPassword" label="Confirm your Password"
/></td>
			</tr>





Please suggest.

Thanks,
Tauri


I used

Lukasz Lenart wrote:
> 
>>  <form name="myForm" method="post" action="MyAction.do" validate="true"/>
>>  <tr>
>>                                                                        
>> <td width="115">First Name</td>
>>                                                                        
>> <td width="142"><input type="text" name="firstName" id="firstName"
>>  class="form_field"></td>
>>
>>                                                                        
>> <td>
>>
>>                                                                        
>> <s:fielderror>
>>                                                                          
>> <s:param>firstName</s:param>
>>                                                                        
>> </s:fielderror>
>>                                                                        
>> </td>
> 
> But as I see, you don't use <s:textfield> for your form and that's way
> you don't see values in the form. If you want to add html tags as
> above, use theme=simple and s:textfield
> 
> 
> Regards
> -- 
> Lukasz
> 
> http://www.linkedin.com/in/lukaszlenart
> 
> ---------------------------------------------------------------------
> 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/Struts2-Annotation-based-Validation-tp16089678p16118342.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


Re: Struts2 Annotation based Validation

Posted by Lukasz Lenart <lu...@googlemail.com>.
>  <form name="myForm" method="post" action="MyAction.do" validate="true"/>
>  <tr>
>                                                                         <td width="115">First Name</td>
>                                                                         <td width="142"><input type="text" name="firstName" id="firstName"
>  class="form_field"></td>
>
>                                                                         <td>
>
>                                                                         <s:fielderror>
>                                                                           <s:param>firstName</s:param>
>                                                                         </s:fielderror>
>                                                                         </td>

But as I see, you don't use <s:textfield> for your form and that's way
you don't see values in the form. If you want to add html tags as
above, use theme=simple and s:textfield


Regards
-- 
Lukasz

http://www.linkedin.com/in/lukaszlenart

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


Re: Struts2 Annotation based Validation

Posted by Tauri Valor <su...@gmail.com>.
Hi Lukasz,

Thanks for responding..

Snippet of Jsp:


<form name="myForm" method="post" action="MyAction.do" validate="true"/> 


<tr>
									<td width="115">First Name</td>
									<td width="142"><input type="text" name="firstName" id="firstName"
class="form_field"></td>
									
									<td>
									
									<s:fielderror>
									  <s:param>firstName</s:param>
									</s:fielderror>
									</td>
	


My Struts-config:

<action name="MyAction" class="activationPendingAction">
			<result name="input" type="tiles">MyForm</result>
			<result name="failure" type="tiles">MyForm</result>
			<result name="success" type="tiles">SuccessPage</result>
		</action>





Thats the snippet of my code.


Thanks,
Tauri.


Lukasz Lenart wrote:
> 
>>  But when I enter the value in the textbox and click submit again, the
>>  validation error for next is displayed correctly but entered value
>>  disappears in the textbox which is abnormal .
> 
> Maybe you are redirecting to the next page? Could you paste snippet
> for your struts.xml and jsp?
> 
> 
> Regards
> -- 
> Lukasz
> 
> ---------------------------------------------------------------------
> 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/Struts2-Annotation-based-Validation-tp16089678p16116146.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


Re: Struts2 Annotation based Validation

Posted by Lukasz Lenart <lu...@googlemail.com>.
>  But when I enter the value in the textbox and click submit again, the
>  validation error for next is displayed correctly but entered value
>  disappears in the textbox which is abnormal .

Maybe you are redirecting to the next page? Could you paste snippet
for your struts.xml and jsp?


Regards
-- 
Lukasz

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


Re: Struts2 Annotation based Validation

Posted by Tauri Valor <su...@gmail.com>.
Thanks Jeromy and Lukasz!

I could manage to get the field specific error messages using :

<s:fielderror>
									  <s:param>firstName</s:param>

</s:fielderror>


I have a new problem, when validating the page I get error messages as
desired. 

Eg: Mandatory field validation gives out message saying "Field is
mandatory".

But when I enter the value in the textbox and click submit again, the
validation error for next is displayed correctly but entered value
disappears in the textbox which is abnormal .

Why is this happening ? Please help me understand.

Thanks,
Tauri






Lukasz Lenart wrote:
> 
>> In Struts2 error messages are stored in the ValueStack in an object
> 
> Yes, I made mistake, I've mean in request scope, not in request object ;-)
> 
> 
> Regards
> -- 
> Lukasz
> 
> ---------------------------------------------------------------------
> 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/Struts2-Annotation-based-Validation-tp16089678p16115634.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


Re: Struts2 Annotation based Validation

Posted by Lukasz Lenart <lu...@googlemail.com>.
> In Struts2 error messages are stored in the ValueStack in an object

Yes, I made mistake, I've mean in request scope, not in request object ;-)


Regards
-- 
Lukasz

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


Re: Struts2 Annotation based Validation

Posted by Jeromy Evans <je...@blueskyminds.com.au>.
Lukasz Lenart wrote:
> Hi,
>
> Error messages are stored in request, you have to subclass
> ActionSupport and add some mechanism to store messages in session.
>
>
> Regards
>   

In Struts2 error messages are stored in the ValueStack in an object 
implementing ValidationAware.
If your action extends ActionSupport, then means they're available as a 
property of your action.

You can read them from any object in the ValueStack using OGNL.  Have a 
look at ValidationAware.

eg. to iterate through action errors:
<s:iterator value="actionErrors"></s:iterator>

They're not available in the request object. They are available in the 
session object only if something puts them there for you (which is not 
the default case).

The actionError tag applies a template.  You may be better off creating 
a custom template.

regards,
 Jeromy Evans



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


Re: Struts2 Annotation based Validation

Posted by Lukasz Lenart <lu...@googlemail.com>.
Hi,

Error messages are stored in request, you have to subclass
ActionSupport and add some mechanism to store messages in session.


Regards
-- 
Lukasz

http://www.linkedin.com/in/lukaszlenart

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