【问题标题】:Retrieved value from database. How to set to Action Form and JSP from Action class?从数据库中检索值。如何从 Action 类设置为 Action Form 和 JSP?
【发布时间】:2014-12-09 13:07:12
【问题描述】:

我在 jsp 中有复选框。我想根据从数据库中检索到的值来设置它的值。

我已检索到动作类中的值,但我无法将此值从动作类设置到 jsp 页面。

我是新手。谁能告诉我如何做到这一点。

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>

动作形式:

    public class FaxDownloadSettingsForm extends ActionForm {


    private boolean checkMe;

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

            public boolean getCheckMe(){
                    return checkMe;
            }
    }

动作类:

    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");


    }

【问题讨论】:

  • 为什么不能正确初始化ActionForm

标签: java jsp struts-1


【解决方案1】:

在您的操作的执行方法中,在请求级别设置属性,例如:

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

在您的 jsp 中,您可以执行以下操作:

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

【讨论】:

  • 既然是Struts 1,最好使用现有的框架机制,例如ActionForm
  • @DaveNewton 我想这样做。但是表单会在页面加载时重置。发送相同的小代码快照。
  • @k00989 这就是ActionForm.reset 的用途。
  • @almas shaikh 我已按照您的步骤操作并找到了解决方案。我使用表达式语言并从表单本身设置值。
猜你喜欢
  • 1970-01-01
  • 2011-02-13
  • 2013-06-13
  • 2012-04-18
  • 1970-01-01
  • 1970-01-01
  • 2018-03-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多