In this article, we will learn how to use a push tag in struts2. Below is the syntax for the push tag.
<s:push value="someObject"> <s:property value="obejctProperty" /> </s:push>
1. Development Environment
- Eclipse
- JDK 1.8
- Tomcat 8.5
- Maven
2. Create a Maven Web Project
Below is my dynamic web project struts2-push-tag structure in eclipse. In this example, we are using struts 2.1.8 version.
This project has the following actions,
- push-tag.action: In this action, we will see the values of the User object defined in Action class.
Code:
package com.simba.demo; import com.opensymphony.xwork2.ActionSupport; /** * @author Simba * */ public class PushTagExampleAction extends ActionSupport { private User user; public PushTagExampleAction() { } public String pushTagExample() { user=new User(); user.setName("Daenerys"); user.setAge(25); user.setGender("Female"); user.setDescription("Daenerys of the House Targaryen, the First of Her Name, Breaker of Chains and Mother of Dragons."); return SUCCESS; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } }
package com.simba.demo; /** * @author Simba * */ public class User { private String name; private String gender; private String description; private Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@taglib uri="/struts-tags" prefix="s"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Push Tag Example</title> </head> <body> <h1>Push Tag Example</h1> <s:push value="user"> <table> <tr> <td>Name :</td> <td><s:property value="name" /></td> </tr> <tr> <td>Gender :</td> <td><s:property value="gender" /></td> </tr> <tr> <td>Description :</td> <td><s:property value="description" /></td> </tr> <tr> <td>Age :</td> <td><s:property value="age" /></td> </tr> </table> </s:push> </body> </html>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "classpath://struts-2.0.dtd"> <struts> <package name="pushTag" namespace="/" extends="struts-default"> <action name="push-tag" class="com.simba.demo.PushTagExampleAction" method="pushTagExample"> <result>/WEB-INF/jsp/push-tag.jsp</result> </action> </package> </struts>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>struts2-push-tag</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>struts2-push-tag</groupId> <artifactId>struts2-push-tag</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <build> <sourceDirectory>src</sourceDirectory> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.0.0</version> <configuration> <warSourceDirectory>WebContent</warSourceDirectory> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.1.8</version> </dependency> </dependencies> </project>
3. Testing
In your eclipse configure tomcat and run your application by selecting Run on server.