• 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 HTML Options Tag ( < html:options >)

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

Struts HTML Tag Library

Struts HTML tag library provides tags which are used to create input forms and HTML user interfaces. The tags in the Struts HTML library form a bridge between a JSP view and the other components of a Web application. Since a dynamic Web application often depends on gathering data from a user, input forms play an important role in the Struts framework. Consequently, the majority of the HTML tags involve HTML forms.

Syntax to use Struts HTML tag library

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

< html:options >

< html:options > -renders a HTML option field.This Tag is valid only if nested inside < select> Tag.The element is used to display data of lists (arrays, collections) inside a select element.

Example Code for < html:options>

1.Create an Jsp page and name it as options.jsp.It is the Welcome page for a user.

options.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> HTML File example </title>
< head>
< body bgcolor="#DDDDDD">
< h1 > Struts html:file example </h1>

< html:form action="/optn" >

< h3> Select Color:
< html:select name="optform" property="colors">
< html:option value="">–Select Color–< /html:option>
< html:options name="optform" property="colorlist" />
< /html:select>< br>
< html:submit value="Submit"/>< html:reset value="Reset"/>
< /h3>
< /html:form>

</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.Here we have three property to hold in tha form bean “colorlist”,”message” and “colors” of type ArrayList(Collection),Strings respectively.
optform.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 optform extends org.apache.struts.action.ActionForm {
private String message;
private String colors;
ArrayList colorlist=new ArrayList();

public String getColors() {
return colors;
}

public void setColors(String colors) {
this.colors = colors;
}

public ArrayList getColorlist() {

ArrayList list=new ArrayList();
list.add(0,"Red");
list.add(1,"Blue");
list.add(2,"Green");
return list;
}

public void setColorlist(ArrayList colorlist) {
this.colorlist = colorlist;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public optform() {
super();

}
}

[/code]

3.Simple Action class optaction.java which is a sub class of Action class used to process the user’s request.In this class we check for null values entered in the file input field and set their respective error message. The Message font color is set to the selected color from the drop down list from user.
optaction.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 optaction extends org.apache.struts.action.Action {

/* forward name="success" path="" */
private final static String SUCCESS = "success";
private final static String FAILURE = "failure";

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

if(formBean.getColors().equals(""))
{
formBean.setMessage("No Color is Selected from list");
return mapping.findForward(SUCCESS);
}

formBean.setMessage("&lt; span style=’color:"+formBean.getColors()+"’&gt; U selected "+formBean.getColors()+" Color &lt;/span&gt;");
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="optform" type="com.myapp.struts.optform"/>
</form-beans>

< action-mappings>
< action name="optform" path="/optn" scope="request" type="com.myapp.struts.optaction" validate="false">
< forward name="success" path="/output.jsp"/>
</action>
</action-mappings>

</struts-config>
[/code]

5.Create another simple Jsp page output.jspwhich is for displaying the output Message.If nothing is selected from the drop down list sets the respective error message and get displayed .If The color is selected from the drop down the message is set to display the message in selected font color in this page.
[code lang=”html”] <%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>

< html>
< head>
< meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
< title> Struts HTML Output Page </title>
</head>
< body bgcolor="#DDDDDD">
< h1> < bean:write name="optform" property="message" filter="false"/> </h1>
</body>
</html>
[/code]

6.Building and running the application
Output
Access page:http://localhost:8084/options/

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 File Tag ( < html:file > )
Next Post: Struts HTML Img Tag ( < html:img >) »

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