Struts BEAN Define Tag ( < bean:define >)

SHARE & 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

<%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean" %>

< bean:define >

< bean:define > -It is used to create the new atttribute in the scope specified by he toScope property and a corresponding scripting variable both of which are named by the value of the id attribute.The corresponding value to which this new attribute (and scripting variable) is set are specified via use of exactly one of the following approaches like Specifying a name attribute plus optional property and scope attributes,specifying value attribute or by specifying nested body content.

Example Code for < bean:define>

In this example we do not need the ActionForm bean.

1.Simple Action class BeansDefineAction.java which is a sub class of Action.In this example we call this action class first which defines and loads a bean with respective property name and value and make it available for current page.
BeanDefineAction.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 BeanDefineAction 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);
    }
}

2.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.


<?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>

< action-mappings>
        < action path="/define" type="com.myapp.struts.BeanDefineAction" validate="false">
        < forward name="success" path="/BeanDefineTag.jsp"/>
		</action>
</action-mappings>

	</struts-config>

3.Create another simple Jsp page BeanDefineTag.jspwhich is a output page which displays the .Property name and Property value which are defined on the same page.

<%@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 Define Tag Example</title>
    < head>
    < body bgcolor="#DDDDDD">
        < h1 > bean:define tag example output </h1>

		   < h3> Property Name: name </h3> < br>
		   < bean:define id="profile" property="name" value="Java Struts" toScope="request"/>
            < h3> Property Value:</h3>
            < bean:write name="profile" />
		  </body>
</html>

4.Change or modify the web.xml file by making the welcome file to index.jsp which contains the forward action to Action class.
index.jsp

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

< jsp:forward page="define.do"/>

5.Building and running the application
Output

Access page:>http://localhost:8084/beandefine/

also read:

Comments

comments

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.

Speak Your Mind

*

Close
Please support the site
By clicking any of these buttons you help our site to get better