How To Use Logic Tags in Struts

Here I am explaining how to use Logic Tags in Struts .

CdTest.java

package com.LogicTags;

import java.util.List;

public class CdTest {
String title;
String id;

public void setId(String id) {
this.id = id;
}

public String getTitle() {
return title;
}

public CdTest() {
this.title="pokiri";
this.id="1";

}
}

Cd.java

package com.LogicTags;

import java.util.List;

public class Cd {
String title;
int id;
List actors;

public Cd(String title, int id, List actors) {
this.title = title;
this.id = id;
this.actors = actors;
}

public String getTitle() {return title;}

public int getId() {return id;}

public List getActors() {
return actors;
}

}

CdForm.java

package com.LogicTags;

import java.util.*;

public class CdForm {
List CDList = new LinkedList();

public CdForm() {
List list1 = new LinkedList();
list1.add("Ameer Khan");
list1.add("Kajol");
Cd cd1 = new Cd("Fana", 1, list1);
CDList.add(cd1);

List list2 = new LinkedList();
list2.add("Mahesh Babu");
list2.add("Iliana");
Cd cd2 = new Cd("Pokiri", 2, list2);
CDList.add(cd2);
}

public List getCDList() {
return CDList;
}

}

web.xml

<web-app>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>

</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<welcome-file-list>
<welcome-file>LogicTest.jsp</welcome-file>
</welcome-file-list>

<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

<taglib>
<taglib-uri>/tags/struts-html</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-bean</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-logic</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>

</web-app>

LogicTest.jsp

<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>

<jsp:useBean id="cds" class="com.LogicTags.CdForm" scope="session" />
<jsp:useBean id="cdt" class="com.LogicTags.CdTest" scope="request"/>

<body>
<center>

<br><br>
<h2><u>Logic Tags Examples</u></h2><br>

<b><u>logic:notEqual Example</u> </b>
<logic:notEqual name="cdt" property="title" value="The Matrix">
This CD The Matrix is not available
</logic:notEqual> <br><br>

<b><u>logic:equal Example</u> </b>
<logic:equal name="cdt" property="title" value="pokiri">
This CD Pokiri available
</logic:equal><br><br>

<b><u>logic:present Example</u> </b>
<logic:present name="cdt" property="title">
title property is available
</logic:present><br><br>

<b><u>logic:notPresent Example</u> </b>
<logic:notPresent name="cdt" property="name">
name property is not available
</logic:notPresent><br><br>

<b><u>logic:iterate Example</u> </b><br>
<b>CdList</b>
<table border=1 >
<tr>
<th>Title</th>
<th>Id</th>
<th>Actors</th>
</tr>

<logic:iterate id="cd" name="cds" property="CDList">

<tr>
<td><bean:write name="cd" property="title" /></td>
<td><bean:write name="cd" property="id" /></td>
<td><bean:write name="cd" property="actors" /></td>
</tr>
</logic:iterate>
</table>

</body>

No comments: