Struts2 Generator Tag with id attribute Example
Aim:
Struts 2 generator tag is used to generate an iterator based on the “val” attribute provided in the page. The generator tag generates an iterator with "count" attribute and tag prints it out using the tag. The separator attribute separates the val into entries of the iterator.
In this section, we are going to describe the generator tag using the id attributes.
Procedure:
Step 1:
Create project GeneratorPro
Step 2:
Create Action GeneratorAction.java
Include following coding into GeneratorAction.java
package example;
import com.opensymphony.xwork2.ActionSupport;
public class GeneratorAction extends ActionSupport
{
public GeneratorAction()
{
}
public String execute() throws Exception
{
return "success";
}
}
Step 3:
Create jsp files
home.jsp
< %@taglib uri="/struts-tags" prefix="s" % >
< html >
< head >
< meta content="keyword" name="Struts2 Generator Tag Example" >
< title > Generator Tag Example < /title >
< /head >
< body >
< s:form action="generateAction" method="post" >
< s:submit value="OK"/ >
< /s:form >
< /body >
< /html >
GeneratorTag.jsp
< %@taglib uri="/struts-tags" prefix="s" % >
< html >
< head >
< meta content="keyword" name="Struts2 Generator Tag Example" >
< title > Generator Tag Example < /title >
< /head >
< body >
< s:generator val="%{'www.suresh.com,www.suresh.net,www.suresh.org'}" separator="," count=”3” id=”mylist” / >
< %
Iterator i=(Iterator)pageContext.getAttribute("mylist");
while(i.hasNext())
{
out.println(i.next());
}
% >
< /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" namespace="/example" extends="struts-default" >
< action name="generateAction" class="example.GeneratorAction" >
< result name="input" > /example/home.jsp < /result >
< result name="success" > /example/GeneratorTag.jsp < /result >
< /action >
< /package >
< /struts >
Step 5:
Run the home.jsp
Only three values displayed because we specify count is 3.
Directory structure of this project
No comments:
Post a Comment