Struts2 Set Tag Example
Solution:
Example for set tag , property tag
Aim:
The set tag is a generic tag that is used to assign a value to a variable in a specified scope.
Procedure:
Step 1:
Create Project PropertyPro
Step 2:
Create LoginAction
Include following coding into LoginAction
package example;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport
{
private String username;
private String password;
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public LoginAction()
{
}
public String execute() throws Exception
{
if((getUsername().equals("suresh"))&&(getPassword().equals("kumar")))
{
return "success";
}
else
{
return "failure";
}
}
}
Step 3:
Create jsp files.
home.jsp
< %@taglib uri="/struts-tags" prefix="s" % >
< html >
< head >
< meta content="keyword" name="Struts2 Property Tag Example" >
< title > JSP Page < /title >
< /head >
< body >
< s:form action="LoginAction" method="post" >
< s:textfield label="UserName" name="username"/ >
< s:password label="PassWord" name="password"/ >
< s:submit value="Login"/ >
< /s:form >
< /body >
< /html >
success.jsp
< %@taglib uri="/struts-tags" prefix="s" % >
< html >
< head >
< meta content="keyword" name="Struts2 Property Tag Example" >
< title > JSP Page < /title >
< /head >
< body >
< h2 > Welcome < /h2 >
< h1 > < s:property value="username"/ > < /h1 > < h2 > You Are Welcome < /h2 >
< s:set name="username" value="'suji'"/ >
< s:property value="#username"/ >
< /body >
< /html >
failure.jsp
< %@taglib uri="/struts-tags" prefix="s" % >
< html >
< head >
< meta content="keyword" name="Struts2 Property Tag Example" >
< title > JSP Page < /title >
< /head >
< body >
< h1 > < s:property value="username"/ > < /h1 > < h3 > You Are Not Valid User. < /h3 >
< /body >
< /html >
Step 4:
Made some changes in struts.xml
< !DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
http://struts.apache.org/dtds/struts-2.0.dtd >
< struts >
< package name="example" extends="struts-default" >
< action name="LoginAction" class="example.LoginAction" >
< result name="input" > /example/home.jsp < /result >
< result name="success" > /example/success.jsp < /result >
< result name="failure" > /example/failure.jsp < /result >
< /action >
< /package >
< /struts >
Step 5:
Run home.jsp
Directory Structure of this project