Struts - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Struts 2 Data Tags

Struts 2 Data Tags

shape Description

Struts 2 Data Tags, While developing on the projects, developer needs to to write the code, then some tags will help to reduce the code where each and every tag will have its own functionality but all the tags functionality is same but their usage may be different in technologies. Struts 2 have some set of tags. Using these tags, user can easily execute the flow of page execution. Struts 2 has the great feature that can integrate with other frameworks such as hibernate, tiles, spring, tiles, Ajax. Following are the different tags available in Struts 2 will help to reduce the programming length

Data Tags

shape Description

The Struts 2 data tags are mostly used to display the manipulate data on a page. Struts 2 contain multiple data tags.
The action tag is used to call the JSP to Action class directly, using action name and action class. The action tag contain result tag and is written on the struts.xml file. [c] <action name =” action name” class= ” Action class name”> <result name=” success”>index.jsp</result> </action> [/c] Example of action tag 1. In this example, let's create one view page and get the input values from the user and those values will be displayed into the browser. 2. Create the directory structure. 3. Create the view pages and get the input values. index.jsp [java] <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title> Hello World </title> </head> <body> <form action="action"> <s:textfield name="name" label="Enter your Name" /> <input type="submit" value="submit"/> </form> </body> </html>[/java] 4. Create the action class and get the request from browser. Then, the response is forwarded to result page. MyActionClass.java [java] package com.splessons; public class MyActionClass { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public String execute() { return "success"; } } [/java] 5. Create the struts 2 configuration file and configure the action class in XML file by using <action > tag. struts.xnl [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> <package name="default" extends="struts-default"> <action name="action" class="com.splessons.MyActionClass"> <result name="success"> /success.jsp </result> </action> </package> </struts> [/xml] 6. Print the result into browser through success page and include the index.jsp page using the tag. success.jsp [java]<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!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"> </head> <body> <font color="green"> <h1> Welcome to SPLessons </h1> </font> <h2> Hai <s:property value="name"> This is include tag example </h2> </br> <script type="text/javascript"> alert("Enter another person name"); </script> <s:include value="index.jsp"/> </body> </html> [/java] Here code will be compiled from the index.jsp page. web.xml [xml] <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <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> [/xml] Output 7. Output Enter the required field and click on submit button.
These include tag is used to include a JSP file in another page.For example, let's take two JSP pages, but both JSP pages output will be displayed on one browser.These include tag can be written in different types. [c] <!– First syntax –> <s: include value=” Another jsp file name”/> <!– Second syntax –> <s:include value=” Another jsp file name”> <param name=” key1″ value=”value1″/> <param name=”key2″ value=”value2″/> </s:include> <!– Third syntax –> <s:include value=” Another jsp file name”> <param name=” key1″ >value1</param> <param name=”key2″ >value2</param> </s:include> [/c] Example of include tag: 1. In this example, let's create one Register form, so that if user submits the details, the second page should be displayed on the first page. 2. Create the directory structure. 3. Create the view pages and get the input values. index.jsp [java] <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title> Hello World </title> </head> <body> <form action="include"> <s:textfield name="name" label="Enter Name" > <input type="submit" value="submit"> </form> </body> </html> [/java] 4. Create the action class and get the request from browser. Forward the response to result page. MyActionClass.java [java] package com.splessons; public class MyActionClass { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public String execute() { return "success"; } } [/java] 5. Create the struts 2 configuration file and configure the action class in XML file by using tag. struts.xnl [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> <package name="default" extends="struts-default"> <action name="include" class="com.splessons.MyActionClass"> <result name="success"> /success.jsp </result> </action> </package> </struts> [/xml] 6. Print the result into browser through success page and include the index.jsp page using the tag. success.jsp [java] <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!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"> </head> <body> <font color="green"> <h1> Welcome to SPLessons </h1> </font> <h2> Hai <s:property value="name"> This is include tag example </h2> </br> <script type="text/javascript"> alert("Enter another person name"); </script> <s:include value="index.jsp"> </body> </html> [/java] web.xml [xml] <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <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> [/xml] Output 7. Deploy the application into the server and run the application. 8. Enter the required field and click on submit button. 9. An alert message will be displayed, proceed by clicking OK button, then the output will be as shown in the below image.
These property tag is used to get the property of a value, which is by default defined in stack. [c] <s:property value=”name”/> [/c] Following is an example which describes more about the property tag and how it works functionally. Person.java [java]public class Person { private String name = "Name from Person.java"; public String getName() { return name; } }[/java] Here just declared the text by using String and Get method has been performed to get the data. PropertyTagAction.java [java]import com.opensymphony.xwork2.ActionSupport; public class PropertyTagAction extends ActionSupport{ private String name = "Name from PropertyTagAction.java"; public String getName() { return name; } public String execute() throws Exception { return SUCCESS; } }[/java] execute() will execute the String and returns the success. property.jsp [html]<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> </head> <body> <h1>Struts 2 property tag example</h1> <h2>1. Call getName() from propertyTagAction.java</h2> <s:property value="name" /> <h2>2. Call getName() from Person.java</h2> <s:bean name="com.splessons.Person" var="personBean" /> <s:property value="#personBean.name" /> </body> </html>[/html] getName() is used to get the name from propertyTagAction.java and Person.java. struts.xml [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" /> <package name="default" namespace="/" extends="struts-default"> <action name="propertyTagAction" class="com.splessons.PropertyTagAction" > <result name="success">pages/property.jsp</result> </action> </package> </struts>[/xml] By default, the development mode is disabled, because it has a significant impact on performance, since the entire configuration will be reloaded on every request. The development mode is only suitable in a development or debugging environment. In a production environment, you have to disable it. It will cause significant impact on performance, because the entire application configuration and properties files will be reloaded on every request and extra logging and debugging information will also be provided. web.xml [xml]<!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>Struts 2 Web Application</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>[/xml] Output When compile the program output will be as follows.
This push tag is used to push the value in the property. If one defined the push tag in an application, then need to write the property tag inside push tag. [c] <s:push value =”student”> <s:property value =” Current address “/> <s:property value =”Permanent address”/> <s:push> [/c] Following is an example which describes more about push tag. Person.java [java]public class Person{ private String firstName = "This is firstName"; private String lastName = "This is lastName"; public String getFirstName() { return firstName; } public String getLastName() { return lastName; } }[/java] Here only declared the data members and performed encapsulation theory. PushTagAction.java [java]import com.opensymphony.xwork2.ActionSupport; public class PushTagAction extends ActionSupport{ public String execute() throws Exception { return SUCCESS; } }[/java] Struts 2 comes with an optional action interface (com.opensymphony.xwork2.Action). By implements this interface, it bring some convenient benefits. push.jsp [java]<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> </head> <body> <h1>Struts 2 push tag example</h1> <h2>1. Normal way</h2> <s:bean name="com.splessons.Person" var="personBean" /> First name : <s:property value="#personBean.firstName" /><br/> Last name: <s:property value="#personBean.lastName" /><br/> <h2>2. Push way</h2> <s:push value="#personBean" > First name : <s:property value="firstName" /><br/> Last name: <s:property value="lastName" /><br/> </s:push> </body> </html>[/java] web.xml [xml]<!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>Struts 2 Web Application</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>[/xml] struts.xml [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" /> <package name="default" namespace="/" extends="struts-default"> <action name="pushTagAction" class="com.splessons.PushTagAction" > <result name="success">pages/push.jsp</result> </action> </package> </struts>[/xml] Output When compile the program following is the output will be displayed.
This set tag assigns a value to a variable in a specified scope. It is useful to assign a variable to a complex expression and then simply refer that variable each and every time. Struts 2 as different type of scopes. [c] <s:set name= “myaddress” value =”environment.address”/> <s:property value =”myaddress”/> [/c] Following is an example which describes more about the set tag. SetTagAction.java [java]import com.opensymphony.xwork2.ActionSupport; public class SetTagAction extends ActionSupport{ private String msg = "Struts 2 is a funny framework"; public String getMsg() { return msg; } public String execute() throws Exception { return SUCCESS; } } [/java] set.jsp [java]<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> </head> <body> <h1>Struts 2 set tag example</h1> <h2>1. <s:set var="varMsg" value="msg" /></h2> <s:set var="varMsg" value="msg" /> <s:property value="varMsg" /> <h2>2. <s:set var="varUrl" value="%{'http://www.splesson.com'}" /></h2> <s:set var="varUrl" value="%{'http://www.splesson.com'}" /> <s:property value="varUrl" /> </body> </html>[/java] struts.xml [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="true" /> <package name="default" namespace="/" extends="struts-default"> <action name="setTagAction" class="com.splessons.SetTagAction" > <result name="success">pages/set.jsp</result> </action> </package> </struts>[/xml] web.xml Where success is a predefined result type. action element is the sub component of package. It speaks to an activity to be conjured for the approaching request. It has name, class and method attributes. In the event that you don’t determine name property as a matter of course execute() technique will be summoned for the predetermined actiion class. [xml]<!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>Struts 2 Web Application</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>[/xml] Output When compile the program following is the output will be displayed.
This text tag is used to display the text messages in a browser. [c] <!– syntax 1 –> <s:i18n name = ” action class name ” > <s:text name = “student.currentAddress”/> </s:i18n> <!– syntax 2 –> <s:text name = “student.companyAddress”> value </s:text> <!– syntax 3 –> <s:text name=”student.permanentAddress”> <param >value</param> </s:text> [/c] Following is an example which describes more about the text tag. TextTagAction.java [java]import com.opensymphony.xwork2.ActionSupport; public class TextTagAction extends ActionSupport{ public String execute() throws Exception { return SUCCESS; } }[/java] Struts 2 comes with an optional action interface (com.opensymphony.xwork2.Action). By implements this interface, it bring some convenient benefits. TextTagAction.properties [java]name.msg = "This is a message from properties file" name.msg.param = "This is a message from properties file - param : {0}"[/java] struts.xml [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="true" /> <package name="default" namespace="/" extends="struts-default"> <action name="textTagAction" class="com.splessons.TextTagAction" > <result name="success">pages/text.jsp</result> </action> </package> </struts>[/xml] text.jsp [java]<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> </head> <body> <h1>Struts 2 text tag example</h1> <h2>1.<s:text name="name.msg" /></h2> Output : <s:text name="name.msg" /> <h2>2. <s:text name="name.msg.unknow">message doesn't exists</s:text></h2> Output : <s:text name="name.msg.unknow">message doesn't exists</s:text> <h2>3. <s:text name="name.msg.unknow" /></h2> Output : <s:text name="name.msg.unknow" /> <h2>4. <s:text name="name.msg.param" ><s:param >splesson</s:param> </s:text></h2> Output : <s:text name="name.msg.param" > <s:param >splesson</s:param> </s:text> </body> </html>[/java] web.xml [xml]<!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>Struts 2 Web Application</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>[/xml] Output When compile the program following output will be displayed.
Struts 2 URL tag is used to create an URL and output it as a text format. It’s never work by itself, but it can provides URL to other tags like to create a hyperlink or to render an image. In this tutorials, it shows 5 common use cases of the Struts 2 URL tag. [c] <!– syntax 1 –> <s:url value=”loginname.action”> <s:param name=”id” value=”%{selected}”/> </s:url> <!– syntax 2 — > <s:url action=”login”> <s:param name=”id” value=”%{selected}”/> </s:url> [/c] Following is an example which describes more about URL tag. URLTagAction.java [java]import com.opensymphony.xwork2.ActionSupport; public class URLTagAction extends ActionSupport{ public String execute() { return SUCCESS; } }[/java] Struts 2 comes with an optional action interface (com.opensymphony.xwork2.Action). By implements this interface, it bring some convenient benefits. url.jsp [java]<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> </head> <body> <h1>Struts 2 URL tag example</h1> <ol> <li> <a href="<s:url value="http://www.itoolsinfo.com/" />" target="_blank">iTools info</a> </li> <li> <s:url action="urlTagAction.action" > <s:param name="id">123</s:param> </s:url> </li> <li> <s:url action="urlTagAction.action" var="urlTag" > <s:param name="name">mkyong</s:param> </s:url> <a href="<s:property value="#urlTag" />" >URL Tag Action (via property)</a> </li> <li> <s:url action="urlTagAction.action" var="urlTag" > <s:param name="age">99</s:param> </s:url> <s:a href="%{urlTag}">URL Tag Action (via %)</s:a> </li> </ol> </body> </html>[/java] struts.xml [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="true" /> <package name="default" namespace="/" extends="struts-default"> <action name="urlTagAction" class="com.splessons.URLTagAction" > <result name="success">pages/url.jsp</result> </action> </package> </struts>[/xml] The development mode is only suitable in a development or debugging environment. In a production environment, you have to disable it. It will cause significant impact on performance, because the entire application configuration and properties files will be reloaded on every request and extra logging and debugging information will also be provided. web.xml [xml]<!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>Struts 2 Web Application</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>[/xml] output When compile the program following is the result will be displayed.

Summary

shape Key Points

  • Property tag is used to get the property of a value, which is by default defined in stack.
  • Struts 2 Data Tags - These include tag is used to include a JSP file in another page
  • Struts 2 Data Tags - This text tag is used to display the text messages in a browser.
  • Struts 2 Data Tags - The action tag is used to call the JSP to Action class directly