• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Struts LOGIC Equal and notEqual Tag ( < logic:equal > and < logic:notEqual >)

September 16, 2010 //  by Krishna Srinivasan//  Leave a Comment

Struts LOGIC Equal and notEqual Tag ( < logic:equal > and < logic:notEqual >)

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

[code lang=”html”]<%@ taglib prefix="logic" uri="http://struts.apache.org/tags-logic" %>[/code]

< logic:equal > and < logic:notEqual>

< logic:equal > -This tag compares between the selector attributes and the specified constant variable.If the variable and specified value are equal then the nested content of this tag is evaluated.
< logic:notEqual > – This tag compares between the selector attributes and the specified constant variable.If the variable and the spcified value are not equal then the nested content of this tag is evaluated.

Example Code for < logic:equal > and < logic:notEqual>

1.Create of Modify the index.jsp page whic is the welcome page for the users.It displays a Textbox nested inside the form where the number is to be entered.
index.jsp
[code lang=”html”] < %@page contentType="text/html" pageEncoding="UTF-8"%>
< %@taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
< !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=UTF-8">
< title> Struts Logic Equal Tag< /title>
< /head>
< body bgcolor="DDDDDD">
< h1> Struts logic:equal and logic:notEqual tags example< /h1>
< html:form action="/logicequal">
< h3> Enter any number:< /h3>
< html:text property="number"/>
< html:submit/>< html:cancel/>
&lt;h4&gt;If value is equal to 100 the nested content of &lt;logic:equal&gt;
tag will be executed else &lt;logic:notEqual&gt; tag is executed
&lt;/h4&gt;
< /html:form>
< /body>
< /html>
[/code] 2.Create an Jsp page and name it as LogicEqualTag.jsp.It is the output page for user which contains the logic tags to compare the number and display whether it is equal to a given specified value or not.

LogicEqualTag.jsp

[code lang=”html”] <%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="html" uri="http://jakarta.apache.org/struts/tags-html" %>
<%@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 Equal Tag< /title>
< /head>
< body bgcolor="DDDDDD">
< h1>Struts logic:equal and logic:notEqual tags example< /h1>
< logic:equal name="LogicEqualForm" property="number" value="100">
< h4> Using < logic:equal> tag value is equal to 100< /h4>

< /logic:equal>
< logic:notEqual name="LogicEqualForm" property="number" value="100">
< h4> Using < logic:notEqual> tag value is not equal to 100< /h4>

< /logic:notEqual>
< /body>
< /html>

[/code]

3.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 only one property of type int to hold the integer number to compare.

LogicEqualForm.java
[code lang=”java”] 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 LogicEqualForm extends org.apache.struts.action.ActionForm {

private int number;

public int getNumber() {
return number;
}

public void setNumber(int i) {
number = i;
}

public LogicEqualForm() {
super();

}

}

[/code]

4.Simple Action class LogicEqualAction.java which is a sub class of Action class used to process the user’s request.
LogicEqualAction.java
[code lang=”java”] 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 LogicEqualAction 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 {

return mapping.findForward(SUCCESS);
}
}

[/code]

5.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.
[code lang=”xml”]

<?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="LogicEqualForm" type="com.myapp.struts.LogicEqualForm"/>
</form-beans>

< action-mappings>
< action name="LogicEqualForm" path="/logicequal" scope="request" type="com.myapp.struts.LogicEqualAction" validate="false">
< forward name="success" path="/LogicEqualTag.jsp"/>

</action>
</action-mappings>

</struts-config>

[/code] 6.Building and running the application
Output

Access page:http://localhost:8080/logicequal/

Struts Framework Articles

also read:

  • Struts 2.0 Introduction and Validations using Annotations
  • Introduction to Struts Actions
  • What’s new in Struts 2.0?
  • Internationalization and Taglibs in Struts 1.2

Category: StrutsTag: Struts Tags

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « Struts HTML Select Tag ( )
Next Post: Struts HTML Text AreaTag ( < html:textarea > ) »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

np.zeros

A Complete Guide To NumPy Functions in Python For Beginners

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact