Using Image Button Beans in Struts

The following example modifies the Struts EJB example to demonstrate the use of Image Button Beans.refer post – “EJB with STRUTS”

ImageButtonBeans are specific to struts.
Normally when you have a submit button like

<input type = “submit” name=”buttonName”/>

you do a request.getParameter(“buttonName”);
The thing is that when an image is submitted, the image name is not available in the request. You map the image button name to the name of an element in your form bean:
<html:image property = “blah”…
Form bean has an attribute called “blah” :
private ImageButtonBean blah = new ImageButtonBean();
If you have two submit image buttons on the page, the ImageButtonBean blah will have x and y values populated, so you will know which image button was clicked to submit the form.
If blah.getX() is not null then blah was clicked.
i.e. In the action class if you have

formBean.getReset().getX()!= null

then for instance, reset button was clicked.

The work directory for this example looks as follows:


All files have been reused except for minor modifications in and; and an extra class has been introduced.
The following files remain unchanged:
application.xml, ejb-jar.xml, jboss.xml, jbosscmp-jdbc.xml, mysql-ds.xml, struts-config.xml, web.xml
build.xml has been slightly modified and it is reproduced below:
Changes have been highlighted in bold.

<project name="Authors" default="clean" basedir=".">
	<property environment="env" />
	<property file="./" />

	<!-- the build path -->
	<path id="build.path">
		<pathelement location="${jboss.dist}/server/default/lib" />
		<pathelement location="${jboss.dist}/server/default/lib/jboss-j2ee.jar" />
		<pathelement location="${jboss.dist}/client/javax.servlet.jar" />
		<pathelement location="${build.classes.dir}" />
		<pathelement location="${struts.dir}" />
		<pathelement location="${struts.dir}/struts.jar" />

	<target name="war" depends="compile">
		<war warfile="${war}" webxml="web.xml">
			<fileset dir="${basedir}/pages">
				<include name="*.jsp" />
				<fileset dir="${basedir}/images">
					<include name="*.jpg" />
				<webinf dir="${struts.dir}">
					<include name="*.tld" />
				<webinf dir="${basedir}">
					<include name="struts-config.xml" />
				<classes dir="${build.classes.dir}">
					<include name="shoppingcart/*.class" />
					<exclude name="shoppingcart/${appname}Bean.class" />
				<classes dir="${basedir}">
					<include name="" />
				<lib dir="${jboss.client.dir}">
					<include name="jboss-client.jar" />
					<include name="jnp-client.jar" />
				<lib dir="${struts.dir}">
					<include name="struts.jar" />

	<target name="jar" depends="compile">
		<jar jarfile="${jar}">
			<fileset dir="${build.classes.dir}">
				<include name="shoppingcart/${appname}.class" />
				<include name="shoppingcart/${appname}Home.class" />
				<include name="shoppingcart/${appname}Bean.class" />
				<include name="shoppingcart/${appname}Key.class" />
			<metainf dir="${basedir}" includes="jboss.xml,ejb-jar.xml,jbosscmp-jdbc.xml" />

	<!-- build all, and copy to the jboss/deploy directory -->

	<target name="ear" depends="jar,war">
		<ear earfile="${ear}" appxml="application.xml">
			<fileset dir="${basedir}" includes="${jar},${war}" />

	<!-- compilation options -->

	<target name="compile">
		<mkdir dir="${build.classes.dir}" />
		<javac srcdir="${src.dir}" destdir="${build.classes.dir}" debug="on"
			deprecation="on" classpathref="build.path" optimize="off" />
	<target name="build-all" depends="ear">
		<copy file="${ear}" todir="${jboss.deploy.dir}" />
		<copy file="mysql-ds.xml" todir="${jboss.deploy.dir}" />
	<target name="clean">
		<delete file="${jar}" />
		<delete file="${ear}" />
		<delete file="${war}" />
		<delete file="${jboss.deploy.dir}/${ear}" />
		<delete file="${jboss.deploy.dir}/mysql-ds.xml" />
		<delete dir="${build.classes.dir}" />

The entity bean classes remain unchanged.
There is a slight modification to Changes have been highlighted.

package shoppingcart;

import javax.servlet.http.*;

public class authorsForm extends org.apache.struts.action.ActionForm {
	private ImageButtonBean findButton = new ImageButtonBean();
	private ImageButtonBean addButton = new ImageButtonBean();
	String author;
	String isbnCode;

	public void setAuthor(String author) { = author;

	public String getAuthor() {

	public void setIsbnCode(String isbnCode) {
		this.isbnCode = isbnCode;

	public String getIsbnCode() {
		return this.isbnCode;

	public void setFindButton(ImageButtonBean button) {
		this.findButton = button;

	public ImageButtonBean getFindButton() {
		return this.findButton;

	public void setAddButton(ImageButtonBean button) {
		this.addButton = button;

	public ImageButtonBean getAddButton() {
		return this.addButton;

There is a slight modification to Changes have been highlighted.

package shoppingcart;

import shoppingcart.*;
import javax.naming.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Hashtable;
import java.util.Properties;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import org.apache.struts.action.*;
import java.util.*;

public class authorsAction extends Action {
	// public ActionForward perform(ActionMapping mapping, ActionForm
	// form,HttpServletRequest req, HttpServletResponse res) throws
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse res)
			throws java.lang.Exception {
		String action = null;
		String auth = ((authorsForm) form).getAuthor();
		String isbn = ((authorsForm) form).getIsbnCode();
		if ((((authorsForm) form).getAddButton().getX() != null)
				|| (((authorsForm) form).getAddButton().getY() != null)) {
			action = "add";
		if ((((authorsForm) form).getFindButton().getX() != null)
				|| (((authorsForm) form).getFindButton().getY() != null)) {
			action = "find";
		AuthorsHome authorsHome = null;
		Authors bean = null;
		try {
			Properties p = new Properties();
			p.setProperty("java.naming.provider.url", "localhost:1099");
			InitialContext jndiContext = new InitialContext(p);
			Object ref = jndiContext.lookup("shoppingcart/Authors");
			authorsHome = (AuthorsHome) PortableRemoteObject.narrow(ref,
		} catch (Exception e) {
			throw new ServletException("failed to lookup shoppingcart/Authors",
		req.setAttribute("author", auth);
		req.setAttribute("isbnCode", isbn);
		if (action.equals("add")) {
			try {
				AuthorsKey key = new AuthorsKey(isbn);
				bean = authorsHome.create(key);
				if (auth != null) {
				req.setAttribute("author", auth);
				req.setAttribute("isbnCode", isbn);
			} catch (Exception e) {
		if (action.equals("find")) {
			try {
				if (isbn != null) {
					AuthorsKey key = new AuthorsKey(isbn);
					bean = authorsHome.findByPrimaryKey(key);
			} catch (Exception e) {
			req.setAttribute("author", bean.getAuthorName());
			req.setAttribute("isbnCode", isbn);
		return mapping.findForward("successful");

An extra class called has been added:

package shoppingcart;

public final class ImageButtonBean extends Object {
	private String x = null;
	private String y = null;

	public String getX() {
		return (this.x);

	public void setX(String x) {
		this.x = x;

	public String getY() {
		return this.y;

	public void setY(String y) {
		this.y = y;
}// end imagebuttonbean

success.jsp remains unchanged. authors.jsp has been changed as follows:

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
	<html:form action="/testout.matty">
			<html:errors />
			<br />
			<bean:message key="isbnCode.label" />
			<html:text property="isbnCode" size='10' />
			<br />
			<bean:message key="author.label" />
			<html:text property="author" size='10' />
			<br /> <b> <html:image property="addButton" src="./add.jpg" />
				<html:image property="findButton" src="./find.jpg" /> </b>

The ear file has the following structure:


The application be started by using the following url on Jboss.

About cuppajavamattiz
Matty Jacob - Avid technical blogger with interests in J2EE, Web Application Servers, Web frameworks, Open source libraries, Relational Databases, Web Services, Source control repositories, ETL, IDE Tools and related technologies.

Comments are closed.

%d bloggers like this: