JavaBeat

  • Home
  • 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)
  • Privacy

Struts LOGIC messagesPresent and messagesNotPresent Tag ( < logic:messagesPresent > and < logic:messagesNotPresent >)

October 19, 2010 by Krishna Srinivasan Leave a Comment

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.

also read:

  • Struts 2 Tutorials
  • Struts Interview Questions
  • Struts and Spring Integration

Syntax to use Struts LOGIC tag library

[code lang=”html”]
&lt;%@ taglib prefix=&quot;logic&quot; uri=&quot;http://struts.apache.org/tags-logic&quot; %&gt;
[/code]

&lt logic:messagesPresent &gt and &lt logic:messagesNotPresent&gt

&lt logic:messagesPresent &gt -This tag is used if an ActionMessages object, ActionErrors object, a String, or a String array is present in any scope the nested body content of this tag is evaluated.
&lt logic:messagesNotPresent &gt – This tag is used if an ActionMessages object, ActionErrors object, a String, or a String array is not present in any scope the nested body content of this tag is evaluated.
By default both the tags will retrieve the bean it will iterate over from the Globals.ERROR_KEY constant string, but if message attribute is set to ‘true’ the bean will be retrieved from the Globals.MESSAGE_KEY constant string.If message attribute is set to ‘true’, any value assigned to the name attribute will be ignored.

Example Code for &lt logic:messagesPresent &gt and &lt logic:messagesNotPresent&gt

1.Create of Modify the index.jsp page which is the welcome page for the users.It forwards to the action class.
index.jsp

[code lang=”html”]
&lt;%@page contentType=&quot;text/html&quot;%&gt;
&lt;%@page pageEncoding=&quot;UTF-8&quot;%&gt;

&lt;jsp:forward page=&quot;logicmessage.do&quot;/&gt;
[/code]

2.Create an Jsp page and name it as LogicMessageTag.jsp.It is the output page for user which contains the logic tags to to check whether the page contains any error messages or not.

LogicMessageTag.jsp

[code lang=”html”]
&lt;%@page contentType=&quot;text/html&quot; pageEncoding=&quot;UTF-8&quot;%&gt;
&lt;%@taglib uri=&quot;http://struts.apache.org/tags-logic&quot; prefix=&quot;logic&quot; %&gt;
&lt;%@taglib uri=&quot;http://struts.apache.org/tags-bean&quot; prefix=&quot;bean&quot; %&gt;

&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;
&lt;title&gt;Struts Logic Message Present not Present Tag&lt;/title&gt;
&lt;/head&gt;
&lt;body bgcolor=&quot;DDDDDD&quot;&gt;
&lt;h1&gt;struts logic:messagesPresent and logic:messagesNotPresent tag&lt;/h1&gt;
&lt;logic:messagesPresent &gt;
&lt;h4&gt;There are errors on this page!
The error message is
&lt;bean:message key=&quot;common.errors.text&quot;/&gt;
&lt;/h4&gt;
&lt;/logic:messagesPresent&gt;
&lt;logic:messagesNotPresent message=&quot;true&quot;&gt;
&lt;h4&gt;There are no errors messages on this page!&lt;/h4&gt;
&lt;/logic:messagesNotPresent&gt;

&lt;/body&gt;
&lt;/html&gt;
[/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 String to hold the text.This form contains the validate method to add error messages to the request page.

LogicMessageForm.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 LogicMessageForm extends org.apache.struts.action.ActionForm {

private String text;

public String getText() {
return text;
}

public void setText(String text) {
this.text = text;
}

public LogicMessageForm() {
super();

}

public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {

ActionErrors errors = new ActionErrors();

errors.add(&quot;common.errors.text&quot;,
new ActionMessage(&quot;common.errors.text&quot;));
return errors;
}
}

[/code]

4.ApplicationResource.properties file contains the messages and their corresponding keys.

[code]
common.errors.text=Text Required
[/code]

5.Simple Action class LogicMessageAction.java which is a sub class of Action class used to process the user’s request.
LogicMessageAction.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 LogicMessageAction extends org.apache.struts.action.Action {

private final static String SUCCESS = &quot;success&quot;;

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {

return mapping.findForward(SUCCESS);
}
}

[/code]

6.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”]
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;

&lt;!DOCTYPE struts-config PUBLIC
&quot;-//Apache Software Foundation//DTD Struts Configuration 1.2//EN&quot;
&quot;http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd&quot;&gt;

&lt;struts-config&gt;
&lt; form-beans&gt;
&lt; form-bean name=&quot;LogicMessageForm&quot; type=&quot;com.myapp.struts.LogicMessageForm&quot;/&gt;
&lt;/form-beans&gt;

&lt; action-mappings&gt;
&lt; action input=&quot;/LogicMessageTag.jsp&quot; name=&quot;LogicMessageForm&quot; path=&quot;/logicmessage&quot; scope=&quot;request&quot; validate=&quot;true&quot; type=&quot;com.myapp.struts.LogicMessageAction&quot;&gt;
&lt; forward name=&quot;success&quot; path=&quot;/LogicMessageTag.jsp&quot;/&gt;

&lt;/action&gt;
&lt;/action-mappings&gt;
&lt;;message-resources parameter=&quot;com/myapp/struts/ApplicationResource&quot;/&gt;;
&lt;/struts-config&gt;
[/code]

7.Building and running the application
Output

Access page:http://localhost:8080/logicmessage/

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

Filed Under: Struts Tagged With: Struts Tags

Struts LOGIC match and notMatch Tag ( < logic:match > and < logic:notMatch >)

October 18, 2010 by Krishna Srinivasan Leave a Comment

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:match > and < logic:notMatch>

< logic:match >-This tag matches the selector attributes(String) by specified constant variable.If the value is a substring (appropriately limited by the location attribute), the nested body content of this tag is evaluated
< logic:notMatch >– This tag macthes between the selector attributes(String) by specified constant variable. If the value is not a substring (appropriately limited by the location attribute), the nested body content of this tag is evaluated.

Example Code for < logic:match > and < logic:notMatch>

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 text 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 Match Tag Example< /title>
< /head>
< body bgcolor="DDDDDD">

< html:form action="/logicmatch">
< h3> Enter Some Text:< /h3>
< html:text name="LogicMatchForm" property="text"/>
< html:submit/>

< /html:form>
< /body>
< /html>
[/code]

2.Create an Jsp page and name it as LogicMatchTag.jsp.It is the output page for user which contains the logic tags to Match the text and display whether the specified constant value is a substring or not.

LogicMatchTag.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 Logic Match Tag< /title>
< /head>
< body bgcolor="DDDDDD">
< h1&gt;Struts logic:match and logic:notMatch tag< /h1>
< logic:match name="LogicMatchForm" property="text" value="java>
< h4> Using &lt;logic:match&gt; tag the given text contains "java"< /h4>

< /logic:match>
<logic:notMatch name="LogicMatchForm" property="text" value="struts">
< h4> Using &lt;logic:notMatch&gt; tag the given text does not contains "struts"< /h4>

< /logic:notMatch>
< /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 String to hold the text.

LogicMatchForm.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 LogicMatchForm extends org.apache.struts.action.ActionForm {

private String text;

public String getText() {
return text;
}

public void setText(String text) {
this.text = text;
}

public LogicMatchForm() {
super();

}

}

[/code]

4.Simple Action class LogicMatchAction.java which is a sub class of Action class used to process the user’s request.
LogicMatchAction.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 LogicMatchAction 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="LogicMatchForm" type="com.myapp.struts.LogicMatchForm"/>
</form-beans>

< action-mappings>
< action name="LogicMatchForm" path="/logicmatch" scope="request" type="com.myapp.struts.LogicMatchAction" validate="false">
< forward name="success" path="/LogicMatchTag.jsp"/>

</action>
</action-mappings>

</struts-config>

[/code]

6.Building and running the application
Output

Access page:http://localhost:8080/logicmatch/
After evaluation the result is displayed

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

Filed Under: Struts Tagged With: Struts Tags

Struts LOGIC Iterate Tag ( <logic:iterate> )

October 16, 2010 by Krishna Srinivasan Leave a Comment

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=”java”]
<%@ taglib prefix="logic" uri="http://struts.apache.org/tags-logic" %>
[/code]

-This tag repeats the nested body content of this tag once for every element of the specified collection, which must be an Iterator or a Collection or a Map (whose values are to be iterated over), or an array. The collection to be iterated must be specified in any of the form like a runtime expression spcified as the value of the collection attribute,as Jsp bean spcified by the name attribute or as the property, specified by the property, of the JSP bean specified by the name attribute
If the collection you are iterating over can contain null values, the loop will still be performed but no page scope attribute (named by the id attribute) will be created for that loop iteration.

Example Code for

1.Create of Modify the index.jsp page which redirects to the action class to set the values.
index.jsp
[code lang=”html”]
< %@page contentType="text/html"%>
< %@page pageEncoding="UTF-8"%>
< jsp:forward page="logiciterate.do">
[/code]
2.Create an Jsp page and name it as LogicIterateTag.jsp.It is the output page for user which contains the logic iterate tag to iterate through the collection and display the result as specified.

LogicIterateTag.jsp

[code lang=”html”]
<%@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" %>
<%@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 Logic Iterate Tag< /title>
</head>
<body bgcolor="DDDDDD">
<h1> Struts logic:iterate tag< /h1>
<table style="font-weight:bold">
<tr><td>Employee ID</td><td>Employee Name</td></tr>
<logic:iterate id="employee" name="LogicIterateForm" property="emp">
<tr><td><bean:write name="employee" property="empid"/></td>
<td> <bean:write name="employee" property="empname"/></td>
</tr>
</logic:iterate></table>
</body>
</html>
[/code]

3.Create a Employee class Employee.java which contains the details of the employee(employee id and employee name).
[code lang=”java”]

package com.myapp.struts;

public class Employee {
String empid;
String empname;

public String getEmpid() {
return empid;
}

public void setEmpid(String empid) {
this.empid = empid;
}

public String getEmpname() {
return empname;
}

public void setEmpname(String empname) {
this.empname = empname;
}

public Employee(String empid, String empname) {
this.empid = empid;
this.empname = empname;
}

}
[/code]

4.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 a List of Employee objects which are set from the action class.

LogicIterateForm.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 LogicIterateForm extends org.apache.struts.action.ActionForm {

private List<employee> emp=new ArrayList<employee>();

public List<employee> getEmp() {
return emp;
}

public void setEmp(List<employee> emp) {
this.emp = emp;
}

public LogicIterateForm() {
super();

}

}
[/code]

5.Simple Action class LogicIterateAction.java which is a sub class of Action class used to process the user’s request.Here we create and set the employee objects to the form bean.

LogicIterateAction.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 LogicIterateAction 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 {
LogicIterateForm formBean=(LogicIterateForm)form;
ArrayList<employee> list=new ArrayList<employee>();
list.add(new Employee("11A0","Jack"));
list.add(new Employee("11A1","Sam"));
list.add(new Employee("11A2","Joe"));
list.add(new Employee("11A3","John"));
formBean.setEmp(list);
return mapping.findForward(SUCCESS);
}
}
[/code]

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

< action-mappings>
<action name="LogicIterateForm" path="/logiciterate" scope="session" type="com.myapp.struts.LogicIterateAction" validate="false">
< forward name="success" path="/LogicIterateTag.jsp"/>

</action>
</action-mappings>

</struts-config>

[/code]

6.Building and running the application
Output

Access page:http://localhost:8080/logiciterate/

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

Filed Under: Struts Tagged With: Struts Tags

Struts LOGIC present and notPresent Tag ( <logic:present> and <logic:notPresent>)

October 12, 2010 by Krishna Srinivasan Leave a Comment

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:present and logic:notPresent

< logic:present &> -This tag checks in the current request depending upon the attribuite specified if the specified value is present the nested content of this tag is evaluated.
< logic:notPresent&> – This tag checks in the current request depending upon the attribuite specified if the specified value is not present the nested content of this tag is evaluated.

Example Code for < logic:present &> and < logic:notPresent

1.Create of Modify the index.jsp page which redirects to the action class to set the values.
index.jsp

[code lang=”html”]
< %@page contentType="text/html"%>
< %@page pageEncoding="UTF-8"%>
< jsp:forward page="logicpresent.do">
[/code]

2.Create an Jsp page and name it as LogicPresentTag.jsp.It is the output page for user which contains the logic tags to check the specified attribute in this current request and display the result.

LogicPresentTag.jsp

[code lang=”html”]
<%@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 Present Tag< /title>
</head>
<body bgcolor="DDDDDD">
<h1> Struts logic:present and logic:notPresent tag< /h1>
<logic:present property="name" name="LogicPresentForm">
<h4> Using &amplt;logic:present &amp>; tag < i> name</i> is
< bean:write name="LogicPresentForm" property="name"/>
< /h4>
</logic:present>
<logic:notPresent name="LogicPresentForm" property ="number" >
<h4> Using &amplt; logic:notPresent&amp>; tag <i>number</i> is not set < /h4>

</logic:notPresent>
</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 two properties of type int and String to hold the integer number and name which are set from the action class.

LogicPresentForm.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 LogicPresentForm extends org.apache.struts.action.ActionForm {

private String name;

public String getName() {
return name;
}

public void setName(String string) {
name = string;
}

public LogicPresentForm() {
super();

}

}

[/code]

4.Simple Action class LogicPresentAction.java which is a sub class of Action class used to process the user’s request.
LogicPresentAction.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 LogicPresentAction 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 {
LogicPresentForm formBean=(LogicPresentForm)form;
formBean.setName("Java Struts");

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

< action-mappings>
< action name="LogicPresentForm" path="/logicpresent" scope="session" type="com.myapp.struts.LogicPresentAction" validate="false">
< forward name="success" path="/LogicPresentTag.jsp"/>

</action>
</action-mappings>

</struts-config>

[/code]

6.Building and running the application
Output

Access page:http://localhost:8080/logicpresent/

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

Filed Under: Struts Tagged With: Struts Tags

Struts LOGIC lessEqual and lessThan Tag ( < logic:lessEqual > and < logic:lessThan >)

October 12, 2010 by Krishna Srinivasan Leave a Comment

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: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
[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 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>
[/code]
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

[code lang=”html”]
<%@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 &lt;; logic:lessEqual&gt;; tag < /h4>
< h3> Given Number is LessEqual to 100< /h3>
< /logic:lessEqual>
< logic:lessThan name="LogicLessForm" property ="number" value="1000">
< h4> Using &lt;; logic:lessThan&gt;; tag < /h4>
< h3> Given Number is LessThan 1000< /h3>
< /logic:lessThan>
< /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.

LogicLessForm.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 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();

}

}

[/code]

4.Simple Action class LogicLessAction.java which is a sub class of Action class used to process the user’s request.
LogicLessAction.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 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);
}
}

[/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="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>

[/code]

6.Building and running the application
Output

Access page:http://localhost:8080/logicless/

After evaluation the result is displayed

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

Filed Under: Struts Tagged With: Struts Tags

Struts LOGIC greaterEqual and greaterThan Tag ( <logic:greaterEqual> and <logic:greaterThan>)

October 12, 2010 by Krishna Srinivasan Leave a Comment

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" 2="%>[/code]

< logic:greaterEqual > and < logic:greaterThan>

< logic:greaterEqual > -This tag compares between the selector attributes and the specified constant variable.If the variable is greater 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 greater than the content the nested content of this tag is evaluated.

Example Code for < logic:greaterEqual > and < logic:greaterThan>

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 Greater Than and Equal Tag</title>
</head>
< body bgcolor="DDDDDD">
<h1> Struts logic:greaterEqual and logic:greaterThan tags </h1>
<html:form action="/logicgreater">
<h3> Enter any number:</h3>
<html:text property="number"/>
<html:submit/>< html:cancel/>
</html:form>
</body>
</html>
[/code]

2.Create an Jsp page and name it as LogicGreaterTag.jsp.It is the output page for user which contains the logic tags to evaluate the number and display whether greater than or equal to a given specified value.

LogicGreaterTag.jsp

[code lang=”html”]
<%@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 Greater Tag Example< /title>
</head>
<body bgcolor="DDDDDD">
<h1> Struts logic:greaterThan and logic:greaterEqual tag example< /h1>
<logic:greaterThan name="LogicGreaterForm" property ="number" value="10">
<h4> Using <logic:greaterThan> tag < /h4>
<h3> Given Number is Greater Than 10< /h3>
</logic:greaterThan>
<logic:greaterEqual name="LogicGreaterForm" property ="number" value="100">
<h4> Using < logic:greaterEqual> tag </h4>
<h3> Given Number is Greater Equal to 100</h3>
</logic:greaterEqual>
</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.

LogicGreaterForm.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 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();

}

}

[/code]

4.Simple Action class LogicGreaterAction.java which is a sub class of Action class used to process the user’s request.

LogicGreaterAction.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 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);
}
}

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

< action-mappings>
< action name="LogicGreaterForm" path="/logicgreater" scope="request" type="com.myapp.struts.LogicGreaterAction" validate="false">
< forward name="success" path="/LogicGreaterTag.jsp"/>

</action>
</action-mappings>

</struts-config>

[/code]

6.Building and running the application
Output

Access page:http://localhost:8080/logicgreater/
After evaluation the result is displayed

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

Filed Under: Struts Tagged With: Struts Tags

Struts BEAN Resource Tag ( < bean:resource >)

October 3, 2010 by Krishna Srinivasan Leave a Comment

Struts BEAN Tag Library

Struts BEAN tag library provides tags which are usefull in accessing beans and their properties as well as defining new beans (based on these accesses) that are accessible to the remainder of the page via scripting variables and page scope attributes. Convenient mechanisms to create new beans based on the value of request cookies, headers, and parameters are also provided.

Syntax to use Struts BEAN tag library

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

< bean:resource >

< bean:resource > – It is used to load a web application resource and make it available as a bean. Retrieves the value of the specified web application resource and make it available as Input Stream or String depending on the input attribute value.

Example Code for < bean:resource>

In this example we do not need the ActionForm bean.
In this example we do not need the Action class.
No need to configure struts-config.xml file

1.Create a simple jsp page BeanResourceTag.jsp.In this page we specify the particular web application resource by name attribute of this tag and id to make available it as a bean.In this example we load a Sample jsp page and display its content.

[code lang=”html”]
<%@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>

< title> Struts Bean Resource Tag </title>
< head>
< body bgcolor="#DDDDDD">
< h1 > bean:resource tag example </h1>

< bean:resource id="Samplejsp" name="/Sample.jsp"/>

< h3> Content of SampleJsp Page which is loaded as web application resource</h3>
< bean:write name="Samplejsp" /> < br>

</body>
</html>
[/code]

2.Create any Sample jsp page which will be displayed here we used Sample.jsp page to be loaded.

3.Building and running the application

Output

Access page:http://localhost:8084/beanresource/BeanResourceTag.jsp

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

Filed Under: Struts Tagged With: Struts Tags

Struts BEAN Size and Header Tag ( < bean:size > and < bean:header >)

October 3, 2010 by Krishna Srinivasan Leave a Comment

Struts BEAN Tag Library

Struts BEAN tag library provides tags which are usefull in accessing beans and their properties as well as defining new beans (based on these accesses) that are accessible to the remainder of the page via scripting variables and page scope attributes. Convenient mechanisms to create new beans based on the value of request cookies, headers, and parameters are also provided.

Syntax to use Struts BEAN tag library

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

< bean:size > and < bean:header>

< bean:size > -This tag defines a new bean containing the number of elements in the specified Collection or Map.Collection size can be counted in any way as a runtime expression specified as the value of the collection attribute or by JSP bean specified by the name attribute or as specified by property of the bean by the property attribute with respect to given name attribute.
< bean:header > – It is used to define a scripting variable based on the values of the specified request header. It retrieves the specified request header and define the result as the page scope attribute of type String or String[]

Example Code for < bean:size > and < bean:header>

1.Create an Jsp page and name it as BeanSizeTag.jsp.It displays the size of the collection specified in the name and property attributes of the < bean:size > tag. Displays the header information of the page using < bean:header> tag in this example we define two request header values (user-agent,host) and make available as the page context attribute and display.

BeanSizeTag.jsp

[code lang=”html”]
<%@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>

< title> Bean Size and Header Tag</title>
< head>
< body bgcolor="#DDDDDD">
< h1 > bean:size and bean:header tag example </h1>

< bean:size id="beansize" name="BeanSizeForm" property="list"/>

< h3> Size of the Collection in the Form Bean is:</h3>
< bean:write name="beansize" /> < br>

< h2> Some of Header Object’s values</h2>
< h3> user agent:</h3>
< bean:header id="header_agent" name="user-agent"/>
< bean:write name="header_agent"/>
< h3> localhost:</h3>
< bean:header id="header_host" name="host"/>
< bean:write name="header_host"/>
</body>
</html>

[/code]

2.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 a list of type ArrayList which contains the elements of Integer objects.In this example list is initialized with numbers from 1 to 10 of Integer objects.

BeanSizeForm.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 BeanSizeForm extends org.apache.struts.action.ActionForm {

private ArrayList&lt; Integer&gt; list=new ArrayList&lt; Integer&gt;();

public ArrayList&lt; Integer&gt; getList() {
return list;
}

public void setList() {
for(int i=0;i&lt; 10;i++)
this.list.add(i) ;
}

public BeanSizeForm() {
super();

}

}

[/code]

3.Simple Action class BeanSizeAction.java which is a sub class of Action class used to process the user’s request.In this class we set the list in the ActionForm bean.
BeanSizeAction.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 BeanSizeAction 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 {
BeanSizeForm formBean=(BeanSizeForm)form;
formBean.setList();
return mapping.findForward(SUCCESS);
}
}

[/code]

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

< action-mappings>
< action name="BeanSizeForm" path="/size" scope="request" type="com.myapp.struts.BeanSizeAction" validate="false">
< forward name="success" path="/BeanSizeTag.jsp"/>

</action>
</action-mappings>

</struts-config>
[/code]

5.Modify index.jsp which conatins the jsp forward to the action class .

[code lang=”html”]
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

< jsp:forward page="size.do"/>
[/code]

6.Building and running the application
Output
Access page:]http://localhost:8080/beansize/

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

Filed Under: Struts Tagged With: Struts Tags

Struts BEAN Paramter Tag ( < bean:parameter >)

October 1, 2010 by Krishna Srinivasan Leave a Comment

Struts BEAN Tag Library

Struts BEAN tag library provides tags which are usefull in accessing beans and their properties as well as defining new beans (based on these accesses) that are accessible to the remainder of the page via scripting variables and page scope attributes. Convenient mechanisms to create new beans based on the value of request cookies, headers, and parameters are also provided.

Syntax to use Struts BEAN tag library

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

– It is used to retrieve the value of the specified request parameter and define the result as a page scope attribute of type String (if multiple is not specified).

Example Code for bean:parameter

In this example we do not need the ActionForm bean.
In this example we do not need the Action class.
No need to configure struts-config.xml file

1.Create a simple jsp page BeanParamTag.jsp which illustrates the bean:parameter tag by retrieving the request parameter value and displaying as a string to the current page.

[code lang=”html”]
<%@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>

< title> Struts Bean Param Tag </title>
< head>
< body bgcolor="#DDDDDD">
< h1 > bean:parameter tag example </h1>
< bean:parameter name="parameter1" id="param1"/>
< bean:parameter name="parameter2" id="param2" value="Java Struts"/>
< h3> Parameter 1:</h3> < %=param1%> < br>
< h3> Parameter 2:</h3> < %=param2% />
</body>
</html>

[/code]

2.Modify index.jsp page which redirects to BeanParamTag.jsp page which retreives the parameter values and displays.

[code lang=”lhtm”]
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

< jsp:forward page="BeanParamTag.jsp?parameter1=parameter_value"/>

[/code]

3.Building and running the application

Output

Access page:http://localhost:8084/beanparam/index.jsp

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

Filed Under: Struts Tagged With: Struts Tags

Struts BEAN Include Tag ( < bean:include >)

October 1, 2010 by Krishna Srinivasan Leave a Comment

Struts BEAN Tag Library

Struts BEAN tag library provides tags which are usefull in accessing beans and their properties as well as defining new beans (based on these accesses) that are accessible to the remainder of the page via scripting variables and page scope attributes. Convenient mechanisms to create new beans based on the value of request cookies, headers, and parameters are also provided.

Syntax to use Struts BEAN tag library

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

bean:include

– It is used to load the response data from the dynamic application request and make it as available as a bean of type string.It performs internal dispatch to specified application component.this tag works similar to tag except the response date is stored in page context instead of writing into output stream.

Example Code for bean:include

In this example we do not need the ActionForm bean.
In this example we do not need the Action class.
No need to configure struts-config.xml file

1.Create a simple jsp page BeanIncludeTag.jsp which illustrates the bean:include tag by id and page attributes which specifies the jsp page that to be included.

[code lang=”html”]
<%@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>

< title> Bean Include Tag </title>
< head>
< body bgcolor="#DDDDDD">
< h1 > bean:include tag example </h1>

< bean:include id="SampleJsp" page="/Sample.jsp"/>

< h3> Displaying the Included Page Content</h3>
< bean:write name="SampleJsp" /> < br>

</body>
</html>

[/code]

2.Create any Sample jsp page which will be displayed here we used Sample.jsp page to be included.

3.Building and running the application
Output

Access page:http://localhost:8084/beaninclude/BeanIncludeTag.jsp

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

Filed Under: Struts Tagged With: Struts Tags

  • 1
  • 2
  • 3
  • Next Page »

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved