使用注释验证struts2登录程序 (来自: 饕尽天下 )

 

在本章我们将会在Action类中使用Annotation来验证login程序。我们这个login程序还没有使用数据库来验证用户。相反的我们在Action类中使用硬编码的形式来验证login namepasswords(User: Admin and Password: Admin)

 

程序是如何工作的 :

1.      显示登录页面等待输入

2.      用户输入用户名和密码后点击"Login"按钮

3.      Action类中用户校验得到了执行,如果用户在name/password字段输入Admin/Admin,那么将会显示成功页面.否则页面显示错误信息.

开发本程序的步骤 :

这是些简单的创建登录页面的步骤 :

1.创建登录页面

程序的用户界面由登录表单(login.jsp)和成功消息页面(loginsuccess.jsp)组成.
login.jsp
用来向用户显示登录页面.在我们的程序中它存放在"webapps/Struts2tutorial/pages/",这就是login.jsp的代码 :

Html代码 复制代码
  1. <%@ taglib prefix="s" uri="/struts-tags" %>  
  2. <html>  
  3. <head>  
  4. <title>Struts 2 Login Application!</title>  
  5.   
  6. <link href="<s:url value="/css/main.css"/>rel="stylesheet" type="text/css"/>  
  7.   
  8. </head>  
  9. <body>  
  10.   
  11. <s:form action="AnnotationAction" method="POST" validate="true">  
  12. <tr>  
  13. <td colspan="2">  
  14. Login   
  15. </td>  
  16.   
  17. </tr>  
  18.   
  19.   <tr>  
  20.    <td colspan="2">  
  21.          <s:actionerror />  
  22.          <s:fielderror />  
  23.    </td>  
  24.   </tr>  
  25.   
  26. <s:textfield name="username" label="Login name"/>  
  27. <s:password name="password" label="Password"/>  
  28. <s:submit value="Login" align="center"/>  
  29.   
  30. </s:form>  
  31.   
  32. </body>  
  33.   
  34. </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="doLogin" method="POST">为程序生成了HTML表单

 

代码 <s:textfield name="username" label="Login name"/>
<s:password name="password" label="Password"/>

生成了Login NamePassword字段.

 

提交按钮由代码<s:submit value="Login" align="center"/>生成

 

loginsuccess.jsp页面显示的是当用户验证成功后的登录成功信息.这就是loginsuccess.jsp文件的代码 :

 

Html代码 复制代码
  1. <html>    
  2. <head>  
  3.   
  4. <title>Login Success</title>  
  5.   
  6. </head>  
  7.   
  8. <body>  
  9.   
  10. <p align="center"><font color="#000080" size="5">Login Successful</font></p>  
  11.   
  12. </body>  
  13.   
  14. </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(使用Annotation来验证表单输入)

现在让我们创建Action类来处理登录请求。Struts2提供了一个基础类ActionSupport来实现常用的接口。在我们的Action类中(AnnotationAction.java)我们继承了ActionSupport 类并且引入了com.opensymphony.xwork2.validator.annotations.

为了验证login程序我们可以在JSP或者Action类中添加JavaScript,但是struts2提供了另一种很简单的方法来验证你的表单字段,那就是在Action类中使用annotation

有两个annotation是必须的 :

1.      @Validation注释告诉struts2该类中的Action可能需要验证。

2.      @RequiredStringValidator注释用来使文本输入保留一个奇值。

剩下的都交由框架来处理了。

 

我们的"AnnotationAction"类存放在"webapps/struts2tutorial/WEB-INF/src/java/net/roseindia" 目录。这就是AnnotationAction.java的代码 :

Java代码 复制代码
  1. package net.roseindia;   
  2.      
  3. import com.opensymphony.xwork2.ActionSupport;   
  4. import com.opensymphony.xwork2.validator.annotations.*;   
  5.     
  6.   
  7. @Validation  
  8.      
  9. public class AnnotationAction extends ActionSupport {   
  10.   
  11.          
  12. private String username = null;   
  13.        
  14. private String password = null;   
  15.   
  16.        
  17. @RequiredStringValidator(message="Supply name")   
  18.      
  19. public String getUsername() {   
  20.   
  21.        return username;   
  22.     }   
  23.   
  24.       
  25.        
  26. public void setUsername(String value) {   
  27.   
  28.           username = value;   
  29.     }   
  30.      
  31.      
  32. @RequiredStringValidator(message="Supply password")   
  33.   
  34.      
  35. public String getPassword() {   
  36.            
  37. return password;   
  38.     }   
  39.   
  40.        
  41.        
  42. public void setPassword(String value) {   
  43.            
  44. password = value;   
  45.     }   
  46.   
  47.        
  48.        
  49.       
  50. public String execute() throws Exception {   
  51.            
  52. System.out.println("Validating login");   
  53.     if(!getUsername().equals("Admin") || !getPassword().equals("Admin")){   
  54.             addActionError("Invalid user name or password! Please try again!");   
  55.               
  56.  return ERROR;   
  57.        
  58. }   
  59. else{   
  60.         
  61.  return SUCCESS;   
  62.     }   
  63.      
  64.     }   
  65. }   
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;
    }
  
    }
} 

 

 

 

1).配置Action映射(struts.xml)

现在我们在struts.xml中创建Action映射. 这就是添加到struts.xml的代码 :

Xml代码 复制代码
  1. <action name="LoginAnnotation">  
  2. <result>/pages/log-in.jsp</result>  
  3. </action>  
  4.   
  5. <action name="AnnotationAction" class="net.roseindia.AnnotationAction">  
  6. <result name="input">/pages/log-in.jsp</result>  
  7. <result name="error">/pages/log-in.jsp</result>  
  8. <result>/pages/loginsuccess.jsp</result>  
  9. </action>  

 

在上面的映射中,Action "LoginAnnotation"用来显示登录页面,而"AnnotationAction"校验了用户使用的Action(AnnotationAction.java)

 

2). CSS文件(main.css)

css文件用来增强login表单的显示效果。main.css存放在"/webapps/struts2tutorial/css"目录。

这就是main.css的代码 :

Html代码 复制代码
  1. @CHARSET "UTF-8";   
  2.   
  3. body {   
  4. font: 12px verdana, arial, helvetica, sans-serif;   
  5. background-color:#FFFFFF;   
  6. }    
  7.   
  8. table.wwFormTable {   
  9. font: 12px verdana, arial, helvetica, sans-serif;   
  10. border-width: 1px;   
  11. border-color: #030;   
  12. border-style: solid;   
  13. color: #242;   
  14. background-color: #ada;   
  15. width: 30%;   
  16. margin-left:35%;   
  17. margin-right:35%;   
  18. margin-top:15%;   
  19. }    
  20.   
  21. table.wwFormTable th {   
  22. }   
  23.   
  24. table.wwFormTable tr td {   
  25. background-color: #dfd;   
  26. margin: 5px;   
  27. padding: 5px;   
  28. }   
  29.   
  30. .tdLabel {   
  31. /*   
  32. border-width: 1px;   
  33. border-color: #afa;   
  34. border-style: solid;   
  35. */   
  36. font-weight: bold;   
  37. align: top;   
  38. }    
  39.   
  40. .label {   
  41. }    
  42.   
  43. .errorMessage {   
  44. color: red;   
  45. font-size: 0.8em;   
  46. }    
  47.   
  48. #headerDiv {   
  49. border-style: solid;   
  50. border-width: 1px 1px 0px;   
  51. border-color: black;   
  52. padding: 5px;   
  53. background-color: #7a7;   
  54. /* height: 22px; */   
  55. height: 1.8em;   
  56. /* margin-bottom: 12px; */   
  57. }   
  58.   
  59. #buttonBar {   
  60. border-width: 0px 1px 1px;   
  61. border-style: solid;   
  62. border-color: black;   
  63. color: white;   
  64. margin-bottom: 12px;   
  65. background-color: #7a7;   
  66. height: 1.6em;   
  67. padding: 5px;   
  68. }   
  69.   
  70. #appName {   
  71. color: white;   
  72. font-size: 1.8em;   
  73. }   
  74.   
  75. #pageTitle {   
  76. font-size: 1.4em;   
  77. color: #dfd;   
  78. clear: none;   
  79. }   
  80.   
  81. #appName, #pageTitle {   
  82. float: right;   
  83. }   
  84.   
  85. #menuContainer {   
  86. float: left;   
  87. }   
  88.   
  89. #brandingContainer {   
  90. float: right:   
  91. text-align: right;   
  92. }  
@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"目录(cmd)并键入ant命令。Ant工具会为你编译程序的。

index.html中添加链接

最后我们在index.html中添加链接来访问login表单

Html代码 复制代码
  1. <ul>  
  2. <li><a href="roseindia/LoginAnnotation.action">Action Annotation Example</a></li>  
  3. </ul>  
<ul>
<li><a href="roseindia/LoginAnnotation.action">Action Annotation Example</a></li>
</ul>

 

 

输出

如果不输入任何字段就点击Login按钮,你会得到这样的输出页面 :

 

 

如果你输入了正确的信息并点击Login按钮,你会收到这样的信息 :

如果你输入了错误的信息并点击Login按钮,你会得到这样的信息 :

如果你只输入"Login name"字段而不输入剩下的字段就点击了Login按钮,你会得到这样的页面 :

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值