Struts 2.x : [A Framework from ASF] :: Struts 2.x UI Tags
Struts 2.x UI Tags |
Struts 2.x UI Tags Example.
Example for Struts 2.x UI Tags |
Project Structure
EmployeeAction.java
package com.javaskool.struts;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
public class EmployeeAction extends ActionSupport
{
private String name;
private int age;
private String gender;
private String email;
private String location;
private List tech;
private String agreement;
public String execute() throws Exception
{
return SUCCESS;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public List getTech() {
return tech;
}
public void setTech(List tech) {
this.tech = tech;
}
public String getAgreement() {
return agreement;
}
public void setAgreement(String agreement) {
this.agreement = agreement;
}
public void validate() {
// TODO Auto-generated method stub
// super.validate();
if(tech.isEmpty())
{
addFieldError("tech", "You must select atlease one value.");
}
}
}
index.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<h1>Welcome to Struts UI Tags</h1>
<form action="myemp" method="post">
<s:textfield name="name" label="Enter Your Name"></s:textfield><br>
<s:textfield name="age" label="Enter Your Age"></s:textfield><br>
<s:radio name="gender" label="Gender" list=#" {'M':'Male', 'F':'Female'}"></s:radio><br>
<s:select name="location" label="Select Location"
list=#"{'Ind':'India','USA':'United States','Poland':'Poland'}"></s:select><br>
<s:textfield name="email" label="Email ID"></s:textfield><br>
<s:checkboxlist name="tech" label="Select Technology you know"
list=#"{'Java':'Java','J2EE':'J2EE','Struts':'Struts'}"></s:checkboxlist><br>
<s:checkbox name="agreement" label="Accept Agreement"
value="true"></s:checkbox><br>
<s:submit></s:submit>
</form>
empDetails.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<h1>Welcome !!</h1>
Name : <s:property value="name"/><br>
Age : <s:property value="age"/><br>
Gender : <s:property value="gender"/><br>
Email ID : <s:property value="email"/><br>
Location : <s:property value="location"/><br>
Technology List : <s:iterator value="tech"><s:property/> ;
</s:iterator><br>
Agreement : <s:property value="agreement"/><br>
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.devMode" value="false"></constant>
<package name="p1" extends="struts-default">
<action name="myemp" class="com.javaskool.struts.EmployeeAction"
method="execute">
<result name="success">/empDetails.jsp</result>
<result name="input">/index.jsp</result>
</action>
</package>
</struts>
jar files required in lib folder
Note: List of jar files differ as per project
commons-fileupload-1.2.2
commons-io-2.0.1
commons-lang-2.4
commons-lang3-3.1
commons-logging-1.1.1
commons-logging-api-1.1
freemarker-2.3.19
javassist-3.11.0.GA
ognl-3.0.5
struts2-core-2.3.4
xwork-core-2.3.4
Click Here to download all jar files required for basic Struts 2.x Application
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>StrutsUITags</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>s2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>s2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Output
Click here to download complete code with jar files [3.5 MB]
Recent Comments