使用注释验证Struts2 Login程序.
本章我们将使用Action类中的注释来验证我们的login程序.我们当前的login程序并未通过数据库来验证用户.相反在Action类中用户名和密码是通过硬编码来验证的(User: Admin and Password: Admin).
程序如何工作 :
1. 显示login页面获取用户输入
2. 用户输入用户名和密码后点击"Login"按钮
3. 在Action类中完成用户验证.如果用户在user name/password字段中输入了Admin/Admin,那么显示成功页面,否则在屏幕上显示错误信息.
创建该程序的步骤
这是使用struts2创建Login页面的简易步骤 :
1.创建Login表单
程序的GUI(图形用户界面)包括login表单(log-in.jsp)和显示成功信息的页面(loginsuccess.jsp).
log-in.jsp用来向用户显示登录页,在我们的程序中它存放"webapps\struts2tutorial\pages\",这是log-in.jsp的代码
- <%@ taglib prefix="s" uri="/struts-tags" %>
- <html>
- <head>
- <title>Struts 2 Login Application!</title>
-
- <link href="<s:url value="/css/main.css"/>" rel="stylesheet" type="text/css"/>
-
- </head>
- <body>
-
- <s:form action="AnnotationAction" method="POST" validate="true">
- <tr>
- <td colspan="2">
- Login
- </td>
-
- </tr>
-
- <tr>
- <td colspan="2">
- <s:actionerror />
- <s:fielderror />
- </td>
- </tr>
-
- <s:textfield name="username" label="Login name"/>
- <s:password name="password" label="Password"/>
- <s:submit value="Login" align="center"/>
-
- </s:form>
-
- </body>
-
- </html>
<%@ taglib prefix="s" uri="/struts-tags" %><html><head><title>Struts 2 Login Application!</title><link href="<s:url value="/css/main.css"/>" rel="stylesheet" type="text/css"/></head><body><s:form action="AnnotationAction" method="POST" validate="true"><tr><td colspan="2">Login</td></tr><tr><td colspan="2"><s:actionerror /><s:fielderror /></td></tr><s:textfield name="username" label="Login name"/><s:password name="password" label="Password"/><s:submit value="Login" align="center"/></s:form></body></html>
代码 <s:actionerror />
<s:fielderror />
显示了Action错误段验证错误
代码<s:form action="AnnotationAction" method="POST" validate="true">为程序生成了HTML表单
代码 <s:textfield name="username" label="Login name"/>
<s:password name="password" label="Password"/>
生成了Login Name和Password字段
submit按钮是由代码<s:submit value="Login" align="center"/>生成的
loginsuccess.jsp显示了当用户验证成功后的登录成功信息.这是loginsuccess.jsp的代码
- <html>
- <head>
- <title>Login Success</title>
- </head>
- <body>
- <p align="center"><font color="#000080" size="5">Login Successful</font></p>
- </body>
- </html>
<html><head><title>Login Success</title></head><body><p align="center"><font color="#000080" size="5">Login Successful</font></p></body></html>
2.创建Action类(使用注释来验证表单)
现在让我们创建Action类来处理login请求.Struts2框架提供了一个基础的ActionSupport类来实现常用的接口.在我们的Action类中 (AnnotationAction.java)我们继承了ActionSupport类并引入了com.opensymphony.xwork2.validator.annotations包
为了验证login程序我们可以向jsp页面或Action类中添加java脚本.但是struts2提供了另一种非常简单的方法来验证你的表单域,那就是使用Action类中的注释.
有两个注释是必须的 :
1. @Validation注释告诉Struts这个类的Action可能需要验证
2. @RequiredStringValidator注释用于文本输入以便保持单一值
剩下的则交由框架来处理
我们的"AnnotationAction"保存在"webapps\struts2tutorial\WEB-INF\src\java\net\roseindia"目录,这就是AnnotationAction.java的代码
Action
类:这里我们会编写代码来验证login页面
AnnotationAction.java
- package net.roseindia;
-
- import com.opensymphony.xwork2.ActionSupport;
- import com.opensymphony.xwork2.validator.annotations.*;
-
-
- @Validation
-
- public class AnnotationAction extends ActionSupport {
-
-
- private String username = null;
-
- private String password = null;
-
-
- @RequiredStringValidator(message="Supply name")
-
- public String getUsername() {
-
- return username;
- }
-
-
-
- public void setUsername(String value) {
-
- username = value;
- }
-
-
- @RequiredStringValidator(message="Supply password")
-
-
- public String getPassword() {
-
- return password;
- }
-
-
-
- public void setPassword(String value) {
-
- password = value;
- }
-
-
-
-
- public String execute() throws Exception {
-
- System.out.println("Validating login");
- if(!getUsername().equals("Admin") || !getPassword().equals("Admin")){
- addActionError("Invalid user name or password! Please try again!");
-
- return ERROR;
-
- }
- else{
-
- return SUCCESS;
- }
-
- }
- }
package net.roseindia;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.validator.annotations.*;@Validationpublic class AnnotationAction extends ActionSupport {private String username = null;private String password = null;@RequiredStringValidator(message="Supply name")public String getUsername() {return username;}public void setUsername(String value) {username = value;}@RequiredStringValidator(message="Supply password")public String getPassword() {return password;}public void setPassword(String value) {password = value;}public String execute() throws Exception {System.out.println("Validating login");if(!getUsername().equals("Admin") || !getPassword().equals("Admin")){addActionError("Invalid user name or password! Please try again!");return ERROR;}else{return SUCCESS;}}}
1.配置Action映射(在struts.xmlz中)
现在我们会在struts.xml文件中创建Action映射,这是要添加到struts.xml文件中的代码 :
- <action name="LoginAnnotation">
- <result>/pages/log-in.jsp</result>
- </action>
-
- <action name="AnnotationAction" class="net.roseindia.AnnotationAction">
- <result name="input">/pages/log-in.jsp</result>
- <result name="error">/pages/log-in.jsp</result>
- <result>/pages/loginsuccess.jsp</result>
- </action>
<action name="LoginAnnotation"><result>/pages/log-in.jsp</result></action><action name="AnnotationAction" class="net.roseindia.AnnotationAction"><result name="input">/pages/log-in.jsp</result><result name="error">/pages/log-in.jsp</result><result>/pages/loginsuccess.jsp</result></action>
2.在上面的映射中, "LoginAnnotation"用来显示登录页面, "AnnotationAction"用Action类(AnnotationAction.java)检验了用户输入信息.
3. CSS文件(main.css)
Css文件用来增强login表单的显示效果, main.css存放在"\webapps\struts2tutorial\css"
这是main.css的代码 :
- @CHARSET "UTF-8";
-
- body {
- font: 12px verdana, arial, helvetica, sans-serif;
- background-color:#FFFFFF;
- }
-
- table.wwFormTable {
- font: 12px verdana, arial, helvetica, sans-serif;
- border-width: 1px;
- border-color: #030;
- border-style: solid;
- color: #242;
- background-color: #ada;
- width: 30%;
- margin-left:35%;
- margin-right:35%;
- margin-top:15%;
- }
-
- table.wwFormTable th {
- }
-
- table.wwFormTable tr td {
- background-color: #dfd;
- margin: 5px;
- padding: 5px;
- }
-
- .tdLabel {
-
-
-
-
-
- font-weight: bold;
- align: top;
- }
-
- .label {
- }
-
- .errorMessage {
- color: red;
- font-size: 0.8em;
- }
-
- #headerDiv {
- border-style: solid;
- border-width: 1px 1px 0px;
- border-color: black;
- padding: 5px;
- background-color: #7a7;
-
- height: 1.8em;
-
- }
-
- #buttonBar {
- border-width: 0px 1px 1px;
- border-style: solid;
- border-color: black;
- color: white;
- margin-bottom: 12px;
- background-color: #7a7;
- height: 1.6em;
- padding: 5px;
- }
-
- #appName {
- color: white;
- font-size: 1.8em;
- }
-
- #pageTitle {
- font-size: 1.4em;
- color: #dfd;
- clear: none;
- }
-
- #appName, #pageTitle {
- float: right;
- }
-
- #menuContainer {
- float: left;
- }
-
- #brandingContainer {
- float: right:
- text-align: right;
- }
@CHARSET "UTF-8";body {font: 12px verdana, arial, helvetica, sans-serif;background-color:#FFFFFF;}table.wwFormTable {font: 12px verdana, arial, helvetica, sans-serif;border-width: 1px;border-color: #030;border-style: solid;color: #242;background-color: #ada;width: 30%;margin-left:35%;margin-right:35%;margin-top:15%;}table.wwFormTable th {}table.wwFormTable tr td {background-color: #dfd;margin: 5px;padding: 5px;}.tdLabel {/*border-width: 1px;border-color: #afa;border-style: solid;*/font-weight: bold;align: top;}.label {}.errorMessage {color: red;font-size: 0.8em;}#headerDiv {border-style: solid;border-width: 1px 1px 0px;border-color: black;padding: 5px;background-color: #7a7;/* height: 22px; */height: 1.8em;/* margin-bottom: 12px; */}#buttonBar {border-width: 0px 1px 1px;border-style: solid;border-color: black;color: white;margin-bottom: 12px;background-color: #7a7;height: 1.6em;padding: 5px;}#appName {color: white;font-size: 1.8em;}#pageTitle {font-size: 1.4em;color: #dfd;clear: none;}#appName, #pageTitle {float: right;}#menuContainer {float: left;}#brandingContainer {float: right:text-align: right;}
编译程序
我们进入"\webapps\struts2tutorial\WEB-INF\src"目录并键入ant命令来编译我们的项目,ant工具会为你编译项目的.
在index.html中添加链接
最后我们在index.html中添加链接来访问login表单.
<ul>
<li><a href="roseindia/LoginAnnotation.action">Action Annotation Example</a></li>
</ul>
输出 :
如果你在字段中不输入任何值就点击了Login按钮,你将会得到如下的输出页面 :
如果你只填写"Login name"不填写下面的字段就点击了Login按钮,你会得到如下输出页面 :
如果你填写了错误信息并点击Login按钮,你会得到如下输出 :
如果你输入正确信息并点击Login按钮,你会得到如下输出: