Search code examples
javajspstruts-1

Retrieved value from database. How to set to Action Form and JSP from Action class?


I have Checkbox in jsp. I want to set its value based on the value retrieved from database.

I have retrieved the value in action class but i am not able to set this value from action class to jsp page.

I am newbie. Could anyone please tell how to do this.

JSP:

    <html:form action="faxDownloadSettings">
    <html:hidden property="checkMe"/>
    <input type=checkbox name="pdf" property="checkMe" checked="checked"> <bean:message key="com.console.asPDF"/>
    <console:button name="save" script="save();">
    <console:label><bean:message key="com.console.save"/></console:label>
    </console:button>

Action form:

    public class FaxDownloadSettingsForm extends ActionForm {


    private boolean checkMe;

    public void setCheckMe(boolean checkMe){
                    this.checkMe = checkMe;
            }

            public boolean getCheckMe(){
                    return checkMe;
            }
    }

Action class:

    public class FaxDownloadSettingsAction extends Action {

    public ActionForward execute(ActionMapping mapping,
                            ActionForm actionForm, HttpServletRequest request,
                            HttpServletResponse response)
                                            throws Exception {

     FaxDownloadSettingsForm form = (FaxDownloadSettingsForm) actionForm;

      boolean isFaxtopdf = enumResponse.getFaxtopdf();

      request.setAttribute("checkFax", isFaxtopdf);

      form.setCheckMe(true); //It also not works    

    }

return mapping.findForward("success");


    }

Solution

  • In your action's execute method, set the attribute at request level like:

    request.setAttribute("vehicleSelected", getVehicleFromDB());
    

    In your jsp, you could do something like:

    <input type="checkbox" name="vehicle" value="${vehicleSelected}"/>