Newsletter |
Struts 2 Iterator Tag Example
Struts » on Oct 29, 2011 { 1 Comment } By Sivateja
Let us see how to use iterator tag in struts 2, actually iterator is the best tag to use rather writing while loop to print collection of objects in jsp’s. if we use while loop we may need to write 4 – 5 lines but we can get the same result with iterator with 1 line hah.
Example
required files….
- index.jsp
- success.jsp
- web.xml
- struts.xml
- LogingEx.java
Directory Structure
index.jsp
<META HTTP-EQUIV="Refresh" CONTENT="0;URL=iteAction.action">
success.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <h1>Iterator_Values</h1> <s:iterator value="countries"> <s:property /><br> </s:iterator> </body> </html>
LogingEx.java
package java4s; import java.util.ArrayList; import java.util.List; import com.opensymphony.xwork2.ActionSupport; public class LogingEx extends ActionSupport{ private static final long serialVersionUID = 1L; private List countries; public List getCountries() { return countries; } public String execute() { countries = new ArrayList(); countries.add("Uganda"); countries.add("Ukraine"); countries.add("United States"); countries.add("United Arab Emirates"); countries.add("United Kingdom"); countries.add("Uruguay"); countries.add("Uzbekistan"); return SUCCESS; } }
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <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> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
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> <include file="struts-default.xml"/> <package name="a" extends="struts-default"> <action name="iteAction" class="java4s.LogingEx"> <result name="success">/success.jsp</result> </action> </package> </struts>
Output
You Might Also Like
::. About the Author .:: | ||
Comments
One Response to “Struts 2 Iterator Tag Example”
Hi,
Is it possible to use only struts 2 taglibs for my JSP without using action class and filter dispatcher.