Struts LOGIC Tag Library
Struts LOGIC tag library provides tags that are useful in managing conditional generation of output text, looping over object collections for repetitive generation of output text, and application flow management.
Syntax to use Struts LOGIC tag library
1 | <%@ taglib prefix="logic" uri="http://struts.apache.org/tags-logic" %> |
If the collection you are iterating over can contain null values, the loop will still be performed but no page scope attribute (named by the id attribute) will be created for that loop iteration.
Example Code for
1.Create of Modify the index.jsp page which redirects to the action class to set the values.
index.jsp
1 2 3 | < %@page contentType="text/html"%> < %@page pageEncoding="UTF-8"%> < jsp:forward page="logiciterate.do"> |
2.Create an Jsp page and name it as LogicIterateTag.jsp.It is the output page for user which contains the logic iterate tag to iterate through the collection and display the result as specified.
LogicIterateTag.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="html" uri="http://jakarta.apache.org/struts/tags-html" %>
<%@taglib prefix="bean" uri="http://jakarta.apache.org/struts/tags-bean" %>
<%@taglib prefix="logic" uri="http://jakarta.apache.org/struts/tags-logic" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title> Struts Logic Iterate Tag< /title>
</head>
<body bgcolor="DDDDDD">
<h1> Struts logic:iterate tag< /h1>
<table style="font-weight:bold">
<tr><td>Employee ID</td><td>Employee Name</td></tr>
<logic:iterate id="employee" name="LogicIterateForm" property="emp">
<tr><td><bean:write name="employee" property="empid"/></td>
<td> <bean:write name="employee" property="empname"/></td>
</tr>
</logic:iterate></table>
</body>
</html> |
3.Create a Employee class Employee.java which contains the details of the employee(employee id and employee name).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
package com.myapp.struts;
public class Employee {
String empid;
String empname;
public String getEmpid() {
return empid;
}
public void setEmpid(String empid) {
this.empid = empid;
}
public String getEmpname() {
return empname;
}
public void setEmpname(String empname) {
this.empname = empname;
}
public Employee(String empid, String empname) {
this.empid = empid;
this.empname = empname;
}
} |
4.Create a Form bean.Form bean is used to hold the properties of the submitted form which is a sub class of ActionForm.It contains a List of Employee objects which are set from the action class.
- Buy Struts Books from Java Books Store
.
LogicIterateForm.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | package com.myapp.struts;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
public class LogicIterateForm extends org.apache.struts.action.ActionForm {
private List<employee> emp=new ArrayList<employee>();
public List<employee> getEmp() {
return emp;
}
public void setEmp(List<employee> emp) {
this.emp = emp;
}
public LogicIterateForm() {
super();
}
} |
5.Simple Action class LogicIterateAction.java which is a sub class of Action class used to process the user’s request.Here we create and set the employee objects to the form bean.
LogicIterateAction.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | package com.myapp.struts;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
public class LogicIterateAction extends org.apache.struts.action.Action {
private final static String SUCCESS = "success";
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
LogicIterateForm formBean=(LogicIterateForm)form;
ArrayList<employee> list=new ArrayList<employee>();
list.add(new Employee("11A0","Jack"));
list.add(new Employee("11A1","Sam"));
list.add(new Employee("11A2","Joe"));
list.add(new Employee("11A3","John"));
formBean.setEmp(list);
return mapping.findForward(SUCCESS);
}
} |
6.Create or modify struts config file struts-config.xml with action mappings.Struts-config file contains the information about the configuration of the struts framework to the application.It contains the action mappings which helps to select Action,ActionForm and other information for specific user’s request’s.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
< struts-config>
< form-beans>
<form-bean name="LogicIterateForm" type="com.myapp.struts.LogicIterateForm"/>
</form-beans>
< action-mappings>
<action name="LogicIterateForm" path="/logiciterate" scope="session" type="com.myapp.struts.LogicIterateAction" validate="false">
< forward name="success" path="/LogicIterateTag.jsp"/>
</action>
</action-mappings>
</struts-config> |
6.Building and running the application
Output
Access page:
1 | http://localhost:8080/logiciterate/ |







October 16, 2010
Struts