0

here is the situation.

I have a page called param.jsp, it only has a form and a submit button. There is a single record in the database, when the form renders I want to populate the form with that record. When the form is submitted, I want to update that single record and return to the very same page. What would be the best way to do this in struts?

So far, I've come up with this; here is the Action:

class MyAction extends DispatchAction{
    public ActionForward savePlatinumJLParam(......){
         //<insert the form to the database>
         return mapping.findForward("<return to the same page>");
    }
    public ActionForward initPlatinumJLParam(......){
         //<load the form from the database>
         //form.setXX(...);
         return mapping.findForward("<return to the same page>");
    }
}

Saving works just fine, but I'm having trouble with populating the form. Any help is appreciated.

Erkan Haspulat
  • 12,032
  • 6
  • 39
  • 45

2 Answers2

0

When you render the JSP, you need access a variable from your bean to set the value of your form elements.

Might look something like this,

<input type="text" name="username" value="<%= someBean.getSomeField() >"/>

Read more here, http://struts.apache.org/1.x/userGuide/building_view.html

I know it's not your question, but I wouldn't reccomend using the same action to save and display. I would have one action to save the data, and a different action to display. Then when you submit the data to the save action, redirect to the display action. This a question about redirecting.

Community
  • 1
  • 1
Andy
  • 8,841
  • 8
  • 45
  • 68
0

If you have declared an action like this in struts-config.xml (assume that name="submitForm" is already declared):

<form-beans>
    <form-bean name="submitForm" type="hansen.playground.SubmitForm"/>
</form-beans>

<action   path="/submit"
              type="hansen.playground.SubmitAction"
              name="submitForm"
              input="/submit.jsp"
              scope="request">
</action>

And your form is like this:

package hansen.playground;
public class SubmitForm extends ActionForm {
    private String name;
    private String contactEmail;

    //Getters and setters are here....

}

Then you can do this on your Struts DispatchAction (in my case, SubmitAction):

package hansen.playground;
public class SubmitAction extends DispatchAction{
    public ActionForward request(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){
        //<insert the form to the database>
        ((SubmitForm)form).setName("The Elite Gentleman");
        ((SubmitForm)form).setContactEmail("someone@somewhere.com");

        return mapping.findForward("<return to the same page>");
    }
}

Because your ActionForm is mapped to your Struts Action, Struts sends SubmitForm to the ActionForm form when the request method is called. Changing the name on the <action> tag to another ActionForm, Struts will send that form on request.

Hope this helps...


EDIT On output, you would have to display your result from the submitForm like this:

<html:text name="submitForm" property="name" />

(See that the name attribute matches the Struts Form name).

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228