12:35 am - Monday March 27, 2017

Struts Html image tag example program

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

This Struts tutorial explains how to use img tag and image attribute in struts-html.tld file. Image attribute is used to create submit button with image displayed on it, but img attribute can be used to make an image as an link.

Project Structure:

Jar Files: 

TLD Files: 

Following is the list of required JAR files to be added in Java Class Path of your project. Download displaytag JAR files from 

JAR FILES + TLD FILES

MyActn.java:

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class MyActn extends Action {
	public ActionForward execute(ActionMapping map, ActionForm form,
			HttpServletRequest req, HttpServletResponse res) {
		HttpSession ses = req.getSession(true);

		MyImMod m = (MyImMod) form;

		String s = m.getName();
		ses.setAttribute("s", s);
		if (s.equals("")) {
			return map.findForward("error");
		}
		return map.findForward("success");

	}
}

MyImMod.java:

import org.apache.struts.action.ActionForm;

public class MyImMod extends ActionForm {

	private static final long serialVersionUID = 1L;
	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}

web.xml:


<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>

	<display-name>Struts Blank Application</display-name>

	<servlet>
		<servlet-name>action</servlet-name>
		<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
		<init-param>
			<param-name>config</param-name>
			<param-value>/WEB-INF/struts-config.xml</param-value>
		</init-param>
		<init-param>
			<param-name>debug</param-name>
			<param-value>2</param-value>
		</init-param>
		<init-param>
			<param-name>detail</param-name>
			<param-value>2</param-value>
		</init-param>
		<load-on-startup>2</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>action</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>

	<welcome-file-list>
		<welcome-file>image.jsp</welcome-file>
	</welcome-file-list>
	<taglib>
		<taglib-uri>/struts-html</taglib-uri>
		<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
	</taglib>
	<taglib>
		<taglib-uri>/struts-bean</taglib-uri>
		<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
	</taglib>

</web-app>

struts-config.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="myForm" type="MyImMod" />
	</form-beans>

	<action-mappings>

		<action path="/myAct" type="MyActn" name="myForm" input="image.jsp">
			<forward name="success" path="/success.jsp" />
			<forward name="error" path="/error.jsp" />
		</action>

	</action-mappings>

</struts-config>

error.jsp:


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Error_page</title>
</head>
<body bgcolor="pink">
<p>Please enter your Name</p>

</body>
</html>

image.jsp:


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" 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=ISO-8859-1">
<title>Welcome</title>
</head>
<body bgcolor="pink">
<h3>Example Of Image tag:</h3>
<html:form action="/myAct" method="get">
	<p>Enter your Name: <html:text property="name" /></p>
	<p>click this image</p>
	<p><html:image value="submit" src="image/african-daisy.jpg"
		alt="ygg" /></p>
</html:form>
<h3>Example Of Img tag:</h3>
<p>click this image</p>
<a href="/image/index.jsp"> <html:img src="image/flower.jpg"
	width="150" alt="ygg" /> </a>

</body>
</html>

index.jsp:


Index

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Index</title>
</head>
<body bgcolor="pink">
<h3>This is example of Img tag links to a page.</h3>
</body>
</html>

success.jsp:


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Success</title>
</head>
<body bgcolor="pink">
<h3>Welcome</h3>
<h4><%=session.getAttribute("s")%></h4>
<p>This is example of Image tag its like submit button.</p>
</body>
</html>

OUTPUT:

DOWNLOAD+SOURCE CODE

Filed in: Z-AStruts Codes

No comments yet.

Leave a Reply