DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report

Struts Logic Tag Tutorial

Meyyappan Muthuraman user avatar by
Meyyappan Muthuraman
·
Jun. 14, 12 · Tutorial
Like (0)
Save
Tweet
Share
144.77K Views

Join the DZone community and get the full member experience.

Join For Free

In this example you will learn how to use Struts Logic Tags. In order to use the Struts Logic Tag you need to include the following taglib directive in the jsp page.

<%@taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>

To use the Struts Logic Tag Library you need to add the following <taglib> subelement to the web.xml file.

<jsp-config>
    <taglib>
        <taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
        <taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
    </taglib>
</jsp-config>

In this example we will create a user.jsp page wich displays the user details according to the conditions specified using the logic tags. The UserForm has the following attributes and the corresponding getter and setter methods.

private String name;
private int age;
private float height;
private float weight;
private String favouriteFood;
private ArrayList hobbies;

Initially we set default values for these attributes in the UserAction.

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception {
    ArrayList hobbiesList = new ArrayList();
    UserForm userForm = (UserForm)form;
    userForm.setName("Eswar");
    userForm.setAge(21);
    userForm.setHeight(5.11f);
    userForm.setWeight(70f);
    userForm.setFavouriteFood("Fish and Chicken");
    hobbiesList.add("Music");
    hobbiesList.add("Art");
    hobbiesList.add("Dance");
    userForm.setHobbies(hobbiesList);
    return mapping.findForward(SUCCESS);
}

Based on the conditions specified using the logic tags in the jsp page, the corresponding messages will be displayed to the user.

<logic:present /> <logic:notPresent />

The following code will dispaly "The User Name is Eswar." if the name attribute has some value else it will display "The User is not Present.
<logic:present name="UserForm" property="name">
The User Name is Eswar.
</logic:present>
<logic:notPresent name="UserForm" property="name">
The User is not Present.
</logic:notPresent>

<logic:equal /> <logic:notEqual />

The following code will dispaly "The age is equal to 18." if the value of the age attribute is 18 else it will dispaly "The age is not equal to 18."

<logic:equal name="UserForm" property="age" value="18">
The age is equal to 18.
</logic:equal>
<logic:notEqual name="UserForm" property="age" value="18">
The age is not equal to 18.
</logic:notEqual>

<logic:greaterEqual /> <logic:greaterThan />

The following code will dispaly "The height is greater than or equal to 5.11" if the value of the height attribute is equal to or greater than 5.11 else it will display "The height is greater than 5.11" if the value of the height attribute is greater than 5.11.

<logic:greaterEqual name="UserForm" property="height" value="5.11">
The height is greater than or equal to 5.11
</logic:greaterEqual>
<logic:greaterThan name="UserForm" property="height" value="5.11">
The height is greater than 5.11
</logic:greaterThan>

<logic:lessEqual /> <logic:lessThan />

The following code will dispaly "The weight is less than or equal to 70." if the value of the weight attribute is less than or equal to 70 else it will display "The weight is less than 70" if the value of the weight attribute is less than 70.

<logic:lessEqual name="UserForm" property="weight" value="70">
The weight is less than or equal to 70.
</logic:lessEqual>
<logic:lessThan name="UserForm" property="weight" value="70">
The weight is less than 70.
</logic:lessThan>

<logic:match /> <logic:notMatch />

The following code will display "The user's favourite food includes Chicken" if the value of the favouriteFood attribute contains "Chicken" else it will display "The user's favourite food does not include Chicken".

<logic:match name="UserForm" property="favouriteFood" value="Chicken">
The user's favourite food includes Chicken.
</logic:match>
<logic:notMatch name="UserForm" property="favouriteFood" value="Chicken">
The user's favourite food does not includes Chicken.
</logic:notMatch>

<logic:iterate />

The following code is used to iterate the ArrayList and display the contents.

<logic:iterate name="UserForm" property="hobbies" id="data">
<bean:write name="data" />
</logic:iterate>

On executing the example the following page will be displayed to the user.

You can download the source code of the Struts Logic Tag example by clicking on the Download link below.

Source: Download

Source + Lib: Download

Attribute (computing)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 10 Things to Know When Using SHACL With GraphDB
  • Isolating Noisy Neighbors in Distributed Systems: The Power of Shuffle-Sharding
  • Spring Boot vs Eclipse MicroProfile: Resident Set Size (RSS) and Time to First Request (TFR) Comparative
  • How Elasticsearch Works

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: