Question

I'm writing a Struts action class from which I generate some HTML content and add it to a field in ActionForm. How can I display it in the JSP file.

Was it helpful?

Solution

I would consider to use the request object instead of modifying the ActionForm. You may use the logic and bean taglibs in the JSP and try to separate the HTML content and put it all in the JSP.

As example, you can use in your action:

request.setAttribute("message", "<STRONG>html message</STRONG>");

And in your JSP something like this:

<logic:present name="message">
  <bean:write name="message" filter="false" />
</logic:present>

But I think it would be a better aproach to leave of the html code to the JSP who is the responsible of that. An action has to do other things and is a good design to maintain these tasks separate.

Consider this for your action:

request.setAttribute("message", "html message");

And this for your JSP:

<logic:present name="message">
  <STRONG><bean:write name="message" /></STRONG>
</logic:present>

This way you centralize all the html code generation on your JSP files and free the action of that burden.

OTHER TIPS

Using the JSP EL.

In the action:

actionForm.setMyHtmlString(someHtml);

In the JSP:

Here is the HTML : ${fooBarActionForm.myHtmlString}

The action form is stored as a request or session attribute by Struts. The name of this attribute is the name given to the action form in the struts-config.xml file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top