14.Adding Validation to Struts 2 Login Application
来源:互联网 发布:广电网络公司员工出路 编辑:程序博客网 时间:2024/06/03 20:18
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.
- 14.Adding Validation to Struts 2 Login Application
- Adding Validation to the Model
- 13.Developing Login Application in Struts 2
- Adding Windows To Your Console Application
- Adding Tracing Support to the Application
- Adding React to an Existing Application
- Adding Items to Login Items From the CLI
- Struts 2 Validation
- 添加react到已存在的项目中(Adding React to an Existing Application)-2
- Adding XP Visual Styles to Your Visual Basic Application
- Adding XP Visual Styles to Your Visual Basic Application
- Adding a Rich Text Editor to your Rails Application
- Adding a Google AdMob(6.2.1) to Your Application
- Struts Validation框架浅尝2
- Struts Validation框架浅尝2
- Struts Validation框架浅尝2
- Ajax validation with Struts 2
- How To Create A Struts 2 Web Application
- .Net中窗体间传递值的一种方法
- 14.向login程序添加校验
- c# 中combobox绑定数据
- oracle优化学习笔记
- asp.net 自定义分页
- 14.Adding Validation to Struts 2 Login Application
- Instancof详解
- 我翻译的Struts2教程.
- How to write a Makefile
- 新兴XML处理方法VTD-XML介绍
- [Java] Appfuse 源代码分析
- 一个简单的滑动门效果代码
- 测试点击“测试”后,不让这部分链接到别的地方
- 第一天正式学习jQuery