Logic Equal Tag (<logic:equal>...</logic:equal>)
equal tag - if the requested variable is equal to the specified value then this tag is used to evaluate the contents contained in the nested body parts of this tag.
Compares the variable passed against the specified constant value. The nested body content of this tag is evaluated if the variable and value are equal.
Attribute Name | Description |
---|---|
cookie |
The variable contains the cookie value needed to compare .Here the name is specified by this attribute. |
header |
The variable contains the header value needed to compare whose name is specified by this attribute. The name match is performed in a case insensitive manner. |
name |
The variable to be compared is the JSP bean
specified by this attribute, if |
parameter |
The variable to be compared is the first, or only, value of the request parameter specified by this attribute. |
property |
The variable contains the bean property value needed to compare ,it is specified by this attribute. The property reference can be simple, nested, and/or indexed. |
scope |
This specifies the bean scope within which
the bean is searched by the |
value |
The constant value is specified by other attribute(s) of this tag, needed to compare |
Logic notEqual Tag (<logic:notEqual >...</logic:notEqual>)
notEmpty
- if the requested variable is not equal to the specified value then this tag
is used to evaluate the contents contained in the nested
body parts of this
tag.
Compares the variable passed against the
specified constant value. The nested body content of this tag is evaluated if
the variable and value are not equal.
Attributes of notEmpty Tag
Attribute Name | Description |
---|---|
cookie |
The variable contains the cookie value needed to compare .Here the name is specified by this attribute. |
header |
The variable contains the header value needed to compare whose name is specified by this attribute. The name match is performed in a case insensitive manner. |
name |
The variable to be compared is the JSP bean
specified by this attribute, if |
parameter |
The variable to be compared is the first, or only, value of the request parameter specified by this attribute. |
property |
The variable contains the bean property value needed to compare ,it is specified by this attribute. The property reference can be simple, nested, and/or indexed. |
scope |
This specifies the bean scope within which
the bean is searched by the |
value |
The constant value is specified by other attribute(s) of this tag, needed to compare |
Example Illustrating the use of the Equal<logic:equal> and the notEqual <logic:notEqual> logic tags.
Here you will learn to use the Struts Logic Equal and notEqual tags. We will cover an example that will show a comparison between the two logic tags (ie..<logic:equal > and the <logic:notEqual >).
Example code
Creating an Action Class
Develop a simple action class LogicAction.java.
|
Creating Form Bean
Our form bean class contains only one property number.
Here is the code of FormBean (Logic
Form
.java)
|
Defining form Bean in struts-config.xml file
Add the following entry in the struts-config.xml file for defining the form bean
<form-bean name="LogicForm "
type="roseindia.net.LogicForm " /> |
Developing the Action Mapping in the struts-config.xml
Here, Action mapping helps to select the method from the Action class for specific
requests.
<action path="/LogicAction" type="roseindia.net.LogicAction" name="LogicForm" scope="request" input="/pages/InputLogic.jsp"> <forward name="success" path="/pages/output.jsp"/> </action> |
Developing the InputLogic.jsp page
<%@ taglib uri="/tags/struts-html"
prefix="html" %> <%@ taglib uri="/tags/struts-logic" prefix="logic" %> <html:html> <head> <title>Using <logic> Tags</title> </head> <body> <h1>Using <logic> Tags</h1> <html:form action="/LogicAction" method ="post"> <h2>Enter a number:</h2> <html:text property="number"/> <br> <h3>The <logic:Equal>tag works if you enter 1</h3> <h3>else The <logic:notEqual> is processed </h3> <br> <html:submit value="Submit"/> <html:cancel/> </html:form> </body> </html:html> |
Developing the output.jsp page 0
<%@ taglib uri="/tags/struts-bean"
prefix="bean" %> <%@ taglib uri="/tags/struts-logic" prefix="logic" %> <HTML> <HEAD> <TITLE>Here's Your Data...</TITLE> </HEAD> <BODY> <H3>Here's Your Data...</H3> <h4>The number entered is: <bean:write name="LogicForm" property="number"/></h4> <logic:equal name="LogicForm" property="number" value="1"> <h4>Using the tag <logic:equal ></h4> Result : Equal </logic:equal> <logic:notEqual name="LogicForm" property="number" value="1"> <h4>Using the tag <logic:notEqual > </h4> Result : notEqual </logic:notEqual> </BODY> </HTML> |
Add the following line in the index.jsp to call the form.
<li>
<html:link page="/pages/InputLogic.jsp">Struts File Upload</html:link>
<br>
Example demonstrates how LogicAction Class works.
</li>
Building and Testing the Example 1
To build and deploy the application go to Struts\Strutstutorial directory and type ant on the command prompt. This will deploy the application. Open the browser and navigate to the InputLogic.jsp page. Your browser displays the following page.
Writing value equal to1 to the InputLogic.jsp page displays the working of the Equal Logic tag <logic:equal >
It displays the following out.jsp page 2
Now write any data other than 1 to the InputLogic.jsp to see the working of the notEqual logic tag <logic:notEqual>
The <logic:notEqual > evaluated here displays the output.jsp as