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
<%@ taglib prefix="logic" uri="http://struts.apache.org/tags-logic" %>
< logic:lessEqual > and < logic:lessThan>
< logic:lessEqual > -This tag compares between the selector attributes and the specified constant variable.If the variable is less than or equal the content the nested content of this tag is evaluated.
< logic:lessThan > – This tag compares between the selector attributes and the specified constant variable.If the variable is less than the content the nested content of this tag is evaluated.
Example Code for < logic:lessEqual > and < logic:lessThan>
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
< %@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 Less Example< /title> < /head> < body bgcolor="DDDDDD"> < h1> Struts logic:lessEqual and logic:lessThan tag example< /h1> < html:form action="/logicless"> < h3> Enter any number:< /h3> < html:text property="number"/> < html:submit/>< html:cancel/> < /html:form> < /body> < /html>
2.Create an Jsp page and name it as LogicTagLessExample.jsp.It is the output page for user which contains the logic tags to evaluate the number and display whether lessthan or equal to a given specified value.
LogicTagLessExample.jsp
<%@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" %> < html> < head> < meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> < title> Struts Logic Less Equal and Than< /title> < /head> < body bgcolor="DDDDDD"> < h1> Logic LessEqual and LessThan Example< /h1> < logic:lessEqual name="LogicLessForm" property ="number" value="100"> < h4> Using <; logic:lessEqual>; tag < /h4> < h3> Given Number is LessEqual to 100< /h3> < /logic:lessEqual> < logic:lessThan name="LogicLessForm" property ="number" value="1000"> < h4> Using <; logic:lessThan>; tag < /h4> < h3> Given Number is LessThan 1000< /h3> < /logic:lessThan> < /body> < /html>
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.
LogicLessForm.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 LogicLessForm extends org.apache.struts.action.ActionForm { private int number; public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } public LogicLessForm() { super(); } }
4.Simple Action class LogicLessAction.java which is a sub class of Action class used to process the user’s request.
LogicLessAction.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 LogicLessAction 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); } }
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.
<?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="LogicLessForm" type="com.myapp.struts.LogicLessForm"/> </form-beans> < action-mappings> < action name="LogicLessForm" path="/logicless" scope="request" type="com.myapp.struts.LogicLessAction" validate="false"> < forward name="success" path="/LogicTagLessExample.jsp"/> </action> </action-mappings> </struts-config>
6.Building and running the application
Output
Access page:http://localhost:8080/logicless/
After evaluation the result is displayed
also read: