14.Adding Validation to Struts 2 Login Application

In this section we will write the code to validate the login application. After completing this section you will be able to write validations for your Struts 2 projects. The concepts defined in this section are so illustrative that a learner quickly develops his/her skills in Struts 2 framework.

Struts 2 is very elegant framework that provides a lot of functionality to develop web based applications quickly. Here you will learn to write the form validation code in Struts 2 very easily. We will add the form validation code in our login application.

For validation the login application java script can be added to the jsp page or in action class, but Struts 2 provides another very easy method to validate your fields automatically. You can even use the same configuration file to generate client side script ( in next section we will see how to generate client side validation code).

The Struts 2 validation framework uses xml based configuration file. The file name should be <Your action class> -validation.xml. In our case our action class name is Login.Java, so our validation configuration file will be Login-validation.xml. The Login-validation.xml will be saved into "webapps/struts2tutorial/WEB-INF/src/java/net/roseindia" directory. Here is the content of Login-validation.xml file:

 

<?xml version="1.0" encoding="UTF-8"?>

 

        <!DOCTYPE validators PUBLIC 

 

               "-//OpenSymphony Group//XWork Validator 1.0.2//EN" 

 

               "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">

 

       

 

        <validators>

 

       <field name="username">

 

              <field-validator type="requiredstring">

 

                    <param name="trim">true</param>

 

                    <message>Login name is required</message>

 

              </field-validator>

 

       </field>

 

       <field name="password">

 

              <field-validator type="requiredstring">

 

                    <param name="trim">true</param>

 

                    <message>Password is required</message>

 

              </field-validator>

 

       </field>

 

        </validators>

     

In the above configuration file the field name corresponds to the ActionForm properties. For the username and password elements the requiredstring validation is applied and the message in the <message>....</message> tag is used to display the message if validation fails.

Compiling the application

To compile the application go to "/webapps/struts2tutorial/WEB-INF/src" directory and type ant command. The ant tool will compile the application for you.

Adding the link into index.html

Finally we add the link in the index.html to access the login form. 

<ul>
<li><a href="roseindia/showLogin.action">Login Application</a></li>
</ul>

In the next section we will run and test the application.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值