Struts2 密码范例

下载它– Struts2-Password-Example.zip

在Struts 2中,可以使用<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>

Struts 2 <s:password>示例

带有“密码”和“确认密码”字段的页面,并进行验证以确保“确认密码”与“密码”匹配。

1.属性文件

global.properties

#Global messages
username = Username
password = Password
confirmPassword = Confirm Password
submit = 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.行动

RegisterAction.java

package com.mkyong.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.查看页面

结果页面带有Struts 2“ s:password ”标签,以创建HTML密码字段。

register.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
</head>

<body>
<h1>Struts 2 - password example</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 example</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.mkyong.user.action.RegisterAction">
		<result name="SUCCESS">pages/welcome.jsp</result>
		<result name="input">pages/register.jsp</result>
	</action>
   </package>
	
</struts>

5.演示

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

Struts 2 password example

参考

  1. Struts 2密码文档

翻译自: https://mkyong.com/struts2/struts-2-spassword-password-example/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值