Www.helper3.com
热门关键字: php net javascript java
Struts2基础 专题
专题目录
您的位置:java > Struts2基础专题 > Struts2 <s:password>密码示例
Struts2 <s:password>密码示例
作者:--    发布时间:2019-11-20 10:14:55

在这一章节中,我们创建一个web工程为:struts2passwd,演示<s:password>的使用。完整的工程目录结构如下:

在struts2中,可以使用<s:password>来创建html密码字段。例如,可以声明“s:password”键属性或者标签和名称属性。

<s:password key="password" />
//or
<s:textfield label="password" name="password" />
两者都产生相同的html输出(在默认的xhtml主题)。
<td class="tdlabel">
  <label for="registeruser_password" class="label">password:</label>
</td> 
<td>
  <input type="password" name="password" id="registeruser_password"/>
</td>

struts2 <s:password> 示例

一个页面,“密码”和“确认密码”字段,并做了验证,以确保“确认密码”是与“密码”相匹配。

1. 属性文件

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

2. 动作 - action

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"));
		}
	}

}

3. 视图页面

结果页面使用 struts2 的“s:password”标签来创建一个html密码字段。

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>

4. struts.xml

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

5. 运行示例

http://localhost:8080/struts2passwd/user/register.action


参考

  1. struts2密码文档

滚动到顶部 滚动到底部
网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
QQ群:570078429
网站备案号:
粤ICP备17073857号