views:

87

answers:

1

How can I expose a Java bean to a JSP page by using struts? I know how to configure a StrutsAction to include a form-bean, but I wonder if there are other ways to interact with Java code from a JSP page? I ask this question because I don't understand fully a likely answer to a problem that I have asked here:

http://stackoverflow.com/questions/1444225/clean-way-for-conditionally-rendering-html-in-view/

EDIT:

I understand that a JavaBean is defined as a class that contains mainly getters and setters for its properties. My problem was that I did not see how I can access parameters from Java classes in my JSP. Currently, I use a DynaForm to communicate parameters to the view. E.g. in the ActionClass I set the parameter, and in the JSP I can access it with

bean:write name="MyFormBean" property="myParameter"

My question was basically if there are other classes than a DynaForm class that can easily be accessed from inside the JSP with tags, and if so, if someone could provide an example.

+1  A: 

In your action class:

MyBean myBean = new MyBean();
myBean.setSomeProperty("someValue");
request.setAttribute("myBean", myBean);

In your JSP:

<bean:write name="myBean" property="someProperty" scope="request"/>

You can do the same with session as well. Note that you don't have to explicitly specify the scope in <bean:write> tag - if you don't, Struts will look in all scopes from page to application.

More information on scopes is available in Java EE tutorial.

ChssPly76