Struts 2 s:password is the equivalent of input password input box in the HTML. This tutorial shows how to take the input and password from a JSP file and pass on to the next page using Struts 2 Action class. Lets see the example.
1. Action Class
[code lang=”java”]
package javabeat.net.struts2;
public class Struts2UITagsAction{
private String employeeName;
private String password;
public String getEmployeeName() {
return employeeName;
}
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String execute(){
return "success";
}
}
[/code]
2. Password Tag Example
Input.jsp
[code lang=”xml”]
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
</head>
<body>
<h1>JavaBeat – Struts 2 Password Field Demo</h1>
<s:form action="Result">
<s:textfield name="employeeName" label="Employee Name" />
<s:textfield name="password" label="Password" />
<s:submit label="Submit" />
</s:form>
</body>
</html>
[/code]
Result.jsp
[code lang=”xml”]
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head></head>
<body>
<h1>JavaBeat – Struts 2 Password Field Demo</h1>
<h4>
Hello <s:property value="employeeName" /><br>
Entered Secret Code <s:property value="password" />
</h4>
</body>
</html>
[/code]
3. Struts.xml
[code lang=”xml”]
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts
Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="textfielddemo" extends="struts-default">
<action name="Result" class="javabeat.net.struts2.Struts2UITagsAction" >
<result name="success">Result.jsp</result>
</action>
</package>
</struts>
[/code]
4. Password Field Demo
If you access the application using URL http://localhost:8080/Struts2App/Input.jsp, you would see the following output.
Leave a Reply