Manoj
Manoj

Reputation: 5867

How to access values from dynaaction form in jsp

I can set the attributes in the servlet and I can get those values in jsp by accessing the get attribute. do we have anything to access values in jsp like that.

For Example:

DynaActionForm home = (DynaActionForm) form;
String age = (String)home.get("age");

I want to access this age in jsp.

Please help me solve this. Thanks

Upvotes: 3

Views: 4389

Answers (3)

Spiiff77
Spiiff77

Reputation: 55

You can add the map of your formbean in the request scope like that:

        Map m = dynaform.getMap();
    request.setAttribute("mapForm", m);

And then access propertys into your jsp with:

${mapForm['nameOfYourFormProperty'] }

This is using JSTL. Otherwise you can use that:

<%= ((Map)request.getAttribute("mapForm")).get("nameOfYourFormProperty") %>

Upvotes: 1

Daniel
Daniel

Reputation: 855

If your struts-config.xml file is configured correctly, all you need to do is use the bean:write tag.

Upvotes: 1

Art Peterson
Art Peterson

Reputation: 589

Are you asking if you can access DynaActionForm values directly within the Struts View (jsp) component?

You could try setting the DynaActionForm as a request attribute in your Struts Action:

DynaActionForm myForm = (DynaActionForm) form;
request.setAttribute("myForm", myForm);

Then in your JSP page import DynaActionForm and do something like:

DynaActionForm myForm = (DynaActionForm) request.getAttribute("myForm");
String age = (String) myForm.get("var");

But it would be much better to just access the value you needed within the Struts Action and just set that value onto the request or session.

Upvotes: 0

Related Questions