在这一章节中,我们创建一个web工程为:struts2passwd,演示<s:password>的使用。完整的工程目录结构如下:
在struts2中,可以使用<s:password>来创建html密码字段。例如,可以声明“s:password”键属性或者标签和名称属性。
<s:password key="password" /> //or <s:textfield label="password" name="password" />
<td class="tdlabel"> <label for="registeruser_password" class="label">password:</label> </td> <td> <input type="password" name="password" id="registeruser_password"/> </td>
global.properties
#global messages username = 用名名 password = 密码 confirmpassword = 确认密码 submit = 提交
registeraction.properties
#error message username.required = username is required password.required = password is required cpassword.required = confirm password is required cpassword.notmatch = confirm password is not match
registeraction.java
package com.h3.user.action; import com.opensymphony.xwork2.actionsupport; public class registeraction extends actionsupport{ private string username; private string password; private string confirmpassword; public string getpassword() { return password; } public void setpassword(string password) { this.password = password; } public string getconfirmpassword() { return confirmpassword; } public void setconfirmpassword(string confirmpassword) { this.confirmpassword = confirmpassword; } public string getusername() { return username; } public void setusername(string username) { this.username = username; } //business logic public string execute() { return "success"; } //simple validation public void validate(){ if("".equals(getusername())){ addfielderror("username", gettext("username.required")); } if("".equals(getpassword())){ addfielderror("password", gettext("password.required")); } if("".equals(getconfirmpassword())){ addfielderror("confirmpassword", gettext("cpassword.required")); } if(!(getpassword().equals(getconfirmpassword()))){ addfielderror("confirmpassword", gettext("cpassword.notmatch")); } } }
register.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> </head> <body> <h1>struts 2 - password 示例</h1> <s:form action="registeruser" namespace="/user"> <s:textfield key="username" /> <s:password key="password" /> <s:password key="confirmpassword" /> <s:submit key="submit" name="submit" /> </s:form> </body> </html>
welcome.jsp
<%@ page contenttype="text/html;charset=utf-8" %> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <body> <h1>struts 2 - password 示例</h1> <h2>password : <s:property value="password"/></h2> <h2>confirm password : <s:property value="%{confirmpassword}"/></h2> </body> </html>
<?xml version="1.0" encoding="utf-8" ?> <!doctype struts public "-//apache software foundation//dtd struts configuration 2.0//en" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.custom.i18n.resources" value="global" /> <constant name="struts.devmode" value="true" /> <package name="user" namespace="/user" extends="struts-default"> <action name="register"> <result>/pages/register.jsp</result> </action> <action name="registeruser" class="com.h3.user.action.registeraction"> <result name="success">/pages/welcome.jsp</result> <result name="input">/pages/register.jsp</result> </action> </package> </struts>
http://localhost:8080/struts2passwd/user/register.action