开发者

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="SummaryNumbe开发者_StackOverflowrs" 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>


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>


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜