You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Vivek Bansal <vi...@irislogic.com> on 2011/03/09 23:54:06 UTC

Struts2 XML Validation App

Hi,

I am creating my first struts2 application with xml based validation.
However, I am not able to get the validation part working i.e. its not
returning the field error message to UI. Please help.

My code looks like this.


*login.jsp*

<s:form action="LoginAction" method="post">
  <s:textfield name="username" label="Login Name"/>
  <s:password name="password" label="Password"/>
  <s:submit value="Login"/>
  <s:fielderror></s:fielderror>
</s:form>

*struts.xml*

<action name="LoginAction" class="com.helloworld.action.LoginAction"
method="execute">
   <result name="input">/login.jsp</result>
        <result name="error">/login.jsp</result>
        <result name="success">/HelloWorld.jsp</result>
</action>

*LoginAction.java*

 public class LoginAction extends ActionSupport{

private String username;
private String password;

public String execute() throws Exception {

    if(this.username.equals("admin") && this.password.equals("admin"))
        return SUCCESS;

    return ERROR;
}

public void setUsername(String username) {
    this.username = username;
}
public String getUsername(){
    return this.username;
}
public void setPassword(String password) {
    this.password = password;
}
public String getPassword(){
    return this.password;
}
}

*LoginAction-Validation.xml*

<validators>
    <field name="username">
        <field-validator type="requiredstring">
            <message key="user.required"/>
        </field-validator>
    </field>
</validators>

*LoginAction.properties*

user.required=UserName is required.

LoginAction, LoginAction-Validation.xml and LoginAction.properties all are
in same package. Also I am building my application via ant.

Any help will be appreciated.

Thanks,
Vivek

Re: Struts2 XML Validation App

Posted by Okan Özeren <ok...@gmail.com>.
Hi,

Name of LoginAction-Validation.xml file name must be
LoginAction-validation.xml; so Validation's first character must be
lowercase.

And you should consider to struts form which theme definition used for
it. Look at the
http://struts.apache.org/2.2.1/docs/struts-2-themes.html

Maybe you could try and use annotation based valiton for struts2
rather than using xml definitons.

Okan Ozeren.

On Thu, Mar 10, 2011 at 4:56 AM, Chris Mawata
<ch...@mathcove.net> wrote:
> Your struts.xml is missing a <package> tag -- or did you just not
> include it when you pasted?
>
> On Wed, 2011-03-09 at 14:54 -0800, Vivek Bansal wrote:
>> Hi,
>>
>> I am creating my first struts2 application with xml based validation.
>> However, I am not able to get the validation part working i.e. its not
>> returning the field error message to UI. Please help.
>>
>> My code looks like this.
>>
>>
>> *login.jsp*
>>
>> <s:form action="LoginAction" method="post">
>>   <s:textfield name="username" label="Login Name"/>
>>   <s:password name="password" label="Password"/>
>>   <s:submit value="Login"/>
>>   <s:fielderror></s:fielderror>
>> </s:form>
>>
>> *struts.xml*
>>
>> <action name="LoginAction" class="com.helloworld.action.LoginAction"
>> method="execute">
>>    <result name="input">/login.jsp</result>
>>         <result name="error">/login.jsp</result>
>>         <result name="success">/HelloWorld.jsp</result>
>> </action>
>>
>> *LoginAction.java*
>>
>>  public class LoginAction extends ActionSupport{
>>
>> private String username;
>> private String password;
>>
>> public String execute() throws Exception {
>>
>>     if(this.username.equals("admin") && this.password.equals("admin"))
>>         return SUCCESS;
>>
>>     return ERROR;
>> }
>>
>> public void setUsername(String username) {
>>     this.username = username;
>> }
>> public String getUsername(){
>>     return this.username;
>> }
>> public void setPassword(String password) {
>>     this.password = password;
>> }
>> public String getPassword(){
>>     return this.password;
>> }
>> }
>>
>> *LoginAction-Validation.xml*
>>
>> <validators>
>>     <field name="username">
>>         <field-validator type="requiredstring">
>>             <message key="user.required"/>
>>         </field-validator>
>>     </field>
>> </validators>
>>
>> *LoginAction.properties*
>>
>> user.required=UserName is required.
>>
>> LoginAction, LoginAction-Validation.xml and LoginAction.properties all are
>> in same package. Also I am building my application via ant.
>>
>> Any help will be appreciated.
>>
>> Thanks,
>> Vivek
>
>
> ---------------------------------------------------------------------
> 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: Struts2 XML Validation App

Posted by Chris Mawata <ch...@mathcove.net>.
Your struts.xml is missing a <package> tag -- or did you just not
include it when you pasted?

On Wed, 2011-03-09 at 14:54 -0800, Vivek Bansal wrote:
> Hi,
> 
> I am creating my first struts2 application with xml based validation.
> However, I am not able to get the validation part working i.e. its not
> returning the field error message to UI. Please help.
> 
> My code looks like this.
> 
> 
> *login.jsp*
> 
> <s:form action="LoginAction" method="post">
>   <s:textfield name="username" label="Login Name"/>
>   <s:password name="password" label="Password"/>
>   <s:submit value="Login"/>
>   <s:fielderror></s:fielderror>
> </s:form>
> 
> *struts.xml*
> 
> <action name="LoginAction" class="com.helloworld.action.LoginAction"
> method="execute">
>    <result name="input">/login.jsp</result>
>         <result name="error">/login.jsp</result>
>         <result name="success">/HelloWorld.jsp</result>
> </action>
> 
> *LoginAction.java*
> 
>  public class LoginAction extends ActionSupport{
> 
> private String username;
> private String password;
> 
> public String execute() throws Exception {
> 
>     if(this.username.equals("admin") && this.password.equals("admin"))
>         return SUCCESS;
> 
>     return ERROR;
> }
> 
> public void setUsername(String username) {
>     this.username = username;
> }
> public String getUsername(){
>     return this.username;
> }
> public void setPassword(String password) {
>     this.password = password;
> }
> public String getPassword(){
>     return this.password;
> }
> }
> 
> *LoginAction-Validation.xml*
> 
> <validators>
>     <field name="username">
>         <field-validator type="requiredstring">
>             <message key="user.required"/>
>         </field-validator>
>     </field>
> </validators>
> 
> *LoginAction.properties*
> 
> user.required=UserName is required.
> 
> LoginAction, LoginAction-Validation.xml and LoginAction.properties all are
> in same package. Also I am building my application via ant.
> 
> Any help will be appreciated.
> 
> Thanks,
> Vivek


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