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:

mysql-ds.xml
jbosscmp-jdbc.xml
ejb-jar.xml
jboss.xml
application.xml
ApplicationResources.properties
build.xml
build.properties
build.bat
struts-config.xml
web.xml
<build>
<images>
|find.jpg
|add.jpg
<pages>
|authors.jsp
|success.jsp
<src>
|shoppingcart
|AuthorsBean.java
|Authors.java
|AuthorsHome.java
|AuthorsKey.java
|authorsForm.java
|authorsAction.java
|ImageButtonBean.java

All files have been reused except for minor modifications in authorsAction.java and authorsForm.java; 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
and ApplicationResources.properties.
build.xml has been slightly modified and it is reproduced below:
Changes have been highlighted in bold.
build.xml

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

	<!-- 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" />
	</path>

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

	<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" />
			</fileset>
			<metainf dir="${basedir}" includes="jboss.xml,ejb-jar.xml,jbosscmp-jdbc.xml" />
		</jar>
	</target>

	<!-- 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}" />
		</ear>
	</target>

	<!-- 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>
	<target name="build-all" depends="ear">
		<copy file="${ear}" todir="${jboss.deploy.dir}" />
		<copy file="mysql-ds.xml" todir="${jboss.deploy.dir}" />
	</target>
	<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}" />
	</target>
</project>

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

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) {
		this.author = author;
	}

	public String getAuthor() {
		return this.author;
	}

	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 authorsAction.java. Changes have been highlighted.
authorsAction.java

package shoppingcart;

import shoppingcart.*;
import javax.naming.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
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
	// java.io.IOException,javax.servlet.ServletException
	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.factory.initial",
					"org.jnp.interfaces.NamingContextFactory");
			p.setProperty("java.naming.provider.url", "localhost:1099");
			p.setProperty("java.naming,factory.url.pkgs",
					"org.jboss.naming:org.jnp.interfaces");
			InitialContext jndiContext = new InitialContext(p);
			Object ref = jndiContext.lookup("shoppingcart/Authors");
			authorsHome = (AuthorsHome) PortableRemoteObject.narrow(ref,
					AuthorsHome.class);
		} catch (Exception e) {
			throw new ServletException("failed to lookup shoppingcart/Authors",
					e);
		}
		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) {
					bean.setAuthorName(auth);
				}
				req.setAttribute("author", auth);
				req.setAttribute("isbnCode", isbn);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if (action.equals("find")) {
			try {
				if (isbn != null) {
					AuthorsKey key = new AuthorsKey(isbn);
					bean = authorsHome.findByPrimaryKey(key);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			req.setAttribute("author", bean.getAuthorName());
			req.setAttribute("isbnCode", isbn);
		}
		return mapping.findForward("successful");
	}
};

An extra class called ImageButtonBean.java has been added:
ImageButtonBean.java

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:
authors.jsp

<html>
<%@ 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"%>
<head>
</head>
<body>
	<html:form action="/testout.matty">
		<p>
			<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>
	</html:form>
</body>
</html>

The ear file has the following structure:

authors.ear
    |<meta-INF>
        |application.xml
    |authors.jar
        |<meta-INF>
             |ejb-jar.xml
             |jboss.xml
             |jbosscmp-jdbc.xml
        |<shoppingcart>
             |AuthorsBean.class
             |Authors.class
             |AuthorsHome.class
             |AuthorsKey.class
    |authors.war
        |add.jpg
        |find.jpg
        |authors.jsp
        |success.jsp
        |<meta-INF>
        |<web-INF>
             |struts-config.xml
             |web.xml
             |struts-bean.tld
             |struts-html.tld
             |struts-logic.tld
             |struts-nested.tld
             |struts-logic.tld
             |<classes>
                     |ApplicationResources.properties
                     |<shoppingcart>
                          |Authors.class
                          |AuthorsKey.class
                          |AuthorsHome.class
                          |authorsForm.class
                          |authorsAction.class
                          |ImageButtonBean.class
              |<lib>
                     |jnp-client.java
                     |jboss-client.jar
                     |struts.jar

The application be started by using the following url on Jboss.
http://localhost:8080/authors.jsp

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: