Struts 2 <s:password> password example

In Struts 2 , you can use the <s:password> to create a HTML password field. For example, you can declared the “s:password” with a key attribute or label and name attribute.

<s:password key="password" />
<s:textfield label="Password" name="password" />

Both are generate the same HTML output (in default xhtml theme).

<td class="tdLabel">
  <label for="registerUser_password" class="label">Password:</label>
  <input type="password" name="password" id="registerUser_password"/>

Struts 2 <s:password> example

A page with ‘password’ and ‘confirm password’ fields, and do the validation to make sure the ‘confirm password’ is match with the ‘password’.

1. Properties file

#Global messages
username = Username
password = Password
confirmPassword = Confirm Password
submit = Submit

#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

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(){
			addFieldError("username", getText("username.required"));
			addFieldError("password", getText("password.required"));
			addFieldError("confirmPassword", getText("cpassword.required"));
			addFieldError("confirmPassword", getText("cpassword.notmatch"));


3. View page

Result page with Struts 2 “s:password” tag to create a HTML password field.


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

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



<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>

<h1>Struts 2 - password example</h1>

<h2>Password : <s:property value="password"/></h2>
<h2>Confirm Password : <s:property value="%{confirmPassword}"/></h2> 


4. struts.xml

Link all together ~

<?xml version="1.0" encoding="UTF-8" ?>
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

   <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">
	<action name="registerUser" 
		<result name="SUCCESS">pages/welcome.jsp</result>
		<result name="input">pages/register.jsp</result>

5. Demo


Struts 2 password example


  1. Struts 2 password documentation

About the Author

author image
Founder of, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.


8 Comment threads
0 Thread replies
Most reacted comment
Hottest comment thread
8 Comment authors
get fb photo likesRoslyn bookwormBhaskar Recent comment authors
newest oldest most voted

hi Mkyong, I want to ask you a question, why the submit tag require a name too ?
why isn’t enough with the key like the tag textfield or password.


hi Mkyong, i want to ask you a question, exist available or we are creative it.thanks very much

Wendy Harper
Wendy Harper

Hi, whatsup? I came across your site searching Google. I think your
information extremely interesting. I will be sure to add it to
my bookmarks as well as come back to find out more.
Thank you for the posts!


Hi mkyong,

Thanks for posting the useful info. When i tried in my machine i couldn’t able to get the error messages as given in “” file. getText(“cpassword.required”) simply returns the cpassword.required string in the error message, not its equivalent. could you please help to solve this problem

google bookworm
google bookworm

I quite like reading an article that will make people think.
Also, thank you for allowing me to comment!

hello there and thank you for your info – I_ve definitely picked up something new from right here. I did however expertise some technical issues using this web site, since I experienced to reload the site a lot of times previous to I could get it to load properly. I had been wondering if your web host is OK? Not that I am complaining, but sluggish loading instances times will sometimes affect your placement in google and can damage your high quality score if ads and marketing with Adwords. Well I am adding this RSS to my email and could… Read more »


I_d need to verify with you here. Which is not one

thing I normally do! I get pleasure from reading a submit that may make people think.
Also, thanks for

permitting me to comment!

get fb photo likes
get fb photo likes

My brother recommended I would possibly like this web site.

He used to be entirely right. This submit actually made my day.
You can not imagine simply how so much time I had spent for
this info! Thanks!