PseudoNinja
PseudoNinja

Reputation: 2856

Access value of struts bean from Java class

I am in the process of updating some rather old and unsavory code when I hit a snag. My predecessor is populating default values for variables inside the JSP file (via scriptlets and Struts beans).

Is there any way for me to access the value of the struts bean from the java class (model), or possible use his bean definitions to reference the value and pass it to my Java bean ?

(I am new to beans, so be gentle)

Here is how he is currently retrieving the default values

<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>

<bean:define id="SummaryNumbers" name="SummaryNumbers" type="QueryResults"/>

Here is how I am accessing the model class

<jsp:useBean id="content" class="com.xxx.xxx.ProductionContent">
    <jsp:setProperty name="content" property="summaryNumbers"  /> 
</jsp:useBean>

Upvotes: 0

Views: 5900

Answers (2)

zawhtut
zawhtut

Reputation: 8541

I believe you are using Struts 1 and there is a better approach to do that. In your Struts action class

public class ProductionAction extends DispatchAction {
        public ActionForward showProductionConent(ActionMapping mapping, ActionForm form,
                HttpServletRequest  request,HttpServletResponse response) throws STException  
        {           
            //.. get productionContent here
            request.setAttribute("productionContent",productionContent);    
            return mapping.findForward("showProductionContent");
        }   
    }

In your JSP You can use EL to retrieve data like this

<td>Production Content Name: ${productionContent.name}</td>

Or you can use struts html tag to retrive data like this (You need to bind the form bean correctly in struts-config.xml)

   <td>Production Content Name: <html:text property="name"></td>

Upvotes: 2

weltraumpirat
weltraumpirat

Reputation: 22604

You should be able to use simple getters and setters ( getSummaryNumbers(), setSummaryNumbers()) for each property in the Bean Class, if it's bound correctly.

Upvotes: 0

Related Questions