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:struts > ;
< ; bean:struts > ; -It is used to retrieve the value of the specified Struts internal configuration object.You must specify exactly one of the formBean, forward, and mapping attributes to select the configuration object to be exposed.
Example Code for <bean:struts>
2.Create a Form bean.Form bean is used to hold the properties of the submitted form which is a sub class of ActionForm.Since we should have atleast one FormBean to use this tag we specify a form bean without any properties.
BeanStrutsForm.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 BeanStrutsForm extends org.apache.struts.action.ActionForm { public BeanStrutsForm() { super(); } }
3.Simple Action class BeansStrutsAction.java which is a sub class of Action.In this example we call this action class first which loads the internal configuration object value to specified fields and are made accessible to the current page.
BeanStrutsAction.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 BeanStrutsAction 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); } }
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.
<?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="BeanStrutsForm" type="com.myapp.struts.BeanStrutsForm"/> </form-beans> < action-mappings> < action name="BeanStrutsForm" path="/BeanStrutsAction" scope="request" type="com.myapp.struts.BeanStrutsAction" validate="false"> < forward name="success" path="/BeanStrutsTag.jsp"/> </action> </action-mappings> </struts-config>
5.Create another simple Jsp page BeanStrutsTag.jspwhich is a output page which displays the loaded information of the internal configuration object value.
<%@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> Struts BeanStruts Output Tag</title> < head> < body bgcolor="#DDDDDD"> < h1 > bean:struts tag example output </h1> < bean:struts id="actionmapping" mapping="/BeanStrutsAction"/> < h3> Configuration Object Name:</h3> actionmapping < br> < h3> Connfiguration Object Value:</h3> < bean:write name="actionmapping" /> </body> </html>
6.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="BeanStrutsAction.do"/>
7.Building and running the application
Output
Access page:http://localhost:8084/beanstruts/
also read: