您的位置:首页 > 编程语言 > Java开发

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

2009-08-16 13:04 447 查看
 

在本章我们将会在Action类中使用Annotation来验证login程序。我们这个login程序还没有使用数据库来验证用户。相反的我们在Action类中使用硬编码的形式来验证login name和passwords(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代码



<%@ 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"
4000
 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="doLogin" method="POST">为程序生成了HTML表单
 
代码 <s:textfield name="username" label="Login name"/>
<s:password name="password" label="Password"/>

生成了Login Name和Password字段.
 
提交按钮由代码<s:submit value="Login" align="center"/>生成
 
loginsuccess.jsp页面显示的是当用户验证成功后的登录成功信息.这就是loginsuccess.jsp文件的代码 :
 

Html代码



<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类(使用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代码



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.*;

@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代码



<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>

 

在上面的映射中,Action "LoginAnnotation"用来显示登录页面,而"AnnotationAction"校验了用户使用的Action类(AnnotationAction.java)。
 
2). CSS文件(main.css)
该css文件用来增强login表单的显示效果。main.css存放在"/webapps/struts2tutorial/css"目录。
这就是main.css的代码 :

Html代码



@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;   

}  

@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代码



<ul>  

<li><a href="roseindia/LoginAnnotation.action">Action Annotation Example</a></li>  

</ul>  

<ul>
<li><a href="roseindia/LoginAnnotation.action">Action Annotation Example</a></li>
</ul>

 
 

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





 





 





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





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

如果你只输入"Login name"字段而不输入剩下的字段就点击了Login按钮,你会得到这样的页面 :
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐