【问题标题】:How to set property to struts bean using jsp on click event of button如何在按钮的单击事件上使用jsp将属性设置为struts bean
【发布时间】:2013-12-06 18:41:14
【问题描述】:

我是 struts 甚至 web 应用程序的新手...我想在按钮的单击事件上使用 jsp 设置 struts 表单 bean 的属性。但我不能这样做....

以下是代码:

jsp代码

function importDistList(){
    alert("Importing List function called...");
    var val = document.forms['myForm'].importButtonClicked.value;

    var val = document.forms['myForm'].elements['importButtonClicked'].value =    "true";

    alert("val :: "+val); 
}

 <console:button name="importMembers" script="importDistList();">
              <console:label><bean:message key="com.demo.web.console.importList"/></console:label>
            </console:button>

我没有得到值 true..并且警报没有显示...相反它给出了错误消息:'document.forms.myForm.elements.importButtonClicked' is null or not an object

表单 bean 代码:

public class MyForm extends ActionForm {
   private String importButtonClicked;

   public void setImportButtonClicked(String importButtonClicked){
        this.importButtonClicked = importButtonClicked;
   }

   public String getImportButtonClicked(){
    return importButtonClicked;
   }

}

struts-config.xml

<form-beans>
     <form-bean name="myCSVForm" type="com.demo.web.console.MyCSVForm"/>

</form-beans>
<action-mappings>
   <action path="/importCSV"
            type="com.demo.web.console.MyCSVAction"
            scope="session"
            name="myForm"
            input="/myCSV.jsp">
        <forward name="success" path="/myDialog.jsp" />
   </action>
</action-mappings>

在上面的jsp代码中,使用了自定义的taglib,代码为:

public class ButtonTag extends BodyTagSupport {
    private static Log log = LogFactory.getLog(ButtonTag.class);

    /**
     * The name (id) of this button. This way you can
     * retrieve it from some javascript and disable
     * this button.
     */
    private String name;
    /**
     * The "onClick" javascript.
     */
    private String script;

    /**
     * The "href" value of the link.
     */
    private String href;

    /**
     * If the button is enabled or not.
     */
    private String enabled = "enabled";

    /**
     * cell attributes, allows for button placement
     */
    private String cellattr;

    /**
     * the button has a popup attached to it.
     */
    private String hasPopup = "false";

    /**
     * the button's only purpose is to display a pop up menu
     */
    private String isPopupOnly = "false";

    private String buttonLabel;

    private String popupId = null;


    public int doAfterBody() throws JspException {
        if (href == null && script == null && !isButtonAPopupOnly()) {
            throw new IllegalStateException("Either href or script parameter must be provided.");
        }

        try {
            BodyContent bc = getBodyContent();
            String content = bc.getString();
            String buttonClass = null;

            if (isEnabled()) {
                buttonClass = "button";
            }
            else {
                buttonClass = "buttonROLL_disabled";
            }

            /** cause sometimes the button is by itself or in a button bar */
            if ( cellattr != null )
            {
                getPreviousOut().print("<td ");
                getPreviousOut().print( cellattr );
                getPreviousOut().print(">");
            }
            else
            {
                getPreviousOut().print("<td>");
            }
/*          getPreviousOut().print("<table cellSpacing=\"0\" cellPadding=\"0\" border=\"0\" id=\"" + name + "_table\"><tbody><tr><td>");
            getPreviousOut().print("<table class=\"blackBorder\" cellSpacing=\"0\" cellPadding=\"0\" border=\"0\"><tbody><tr>");
            getPreviousOut().print("<td class=\"");
*/
            getPreviousOut().print("<table cellSpacing=\"0\" cellPadding=\"0\" border=\"0\" id=\"" + name + "_table\" class=\"blackBorder\">\n<tr>\n");
            getPreviousOut().print("<td id=\"" + name + "\" nowrap");
            getPreviousOut().print(" class=\"");

            if (isEnabled()) {
                getPreviousOut().print("button");
            }
            else {
                getPreviousOut().print("button_disabled");
            }
            getPreviousOut().print("\"><a href=\"");

            if ( isButtonAPopupOnly() )
            {
                getPreviousOut().print( "#" + popupId );
            }
            else if (script != null && href != null) {
                getPreviousOut().print(href);
                getPreviousOut().print("\" onclick=\"");
                getPreviousOut().print(script);
            }
            else if (script != null) {
                getPreviousOut().print("javascript:");
                getPreviousOut().print(script);
            }
            else if ( href != null ){
                getPreviousOut().print(href);
            }
            else {
                getPreviousOut().print("#");
            }
            getPreviousOut().print("\" class=\"");
            if (isEnabled()) {
                getPreviousOut().print("buttonRoll");
            }
            else {
                getPreviousOut().print("buttonRoll_disabled");
            }


            if ( isButtonAPopupOnly() )
            {
                getPreviousOut().print("\" id=\"");
                getPreviousOut().print(name);
                getPreviousOut().print("_label\">");
                getPreviousOut().print( buttonLabel );
                getPreviousOut().print( "&nbsp<img src=\"images/i_sort_des.gif\" border=\"0\"/>" );
                getPreviousOut().print("</a>");
                getPreviousOut().print( content );
                getPreviousOut().print("</td>\n");
            }
            else if ( buttonHasAPopup() )
            {
                getPreviousOut().print("\" target=\"_self\" id=\"");
                getPreviousOut().print(name);
                getPreviousOut().print("_label\">");
                getPreviousOut().print( buttonLabel );
                getPreviousOut().print("</a></td>\n");
                getPreviousOut().print( "<td id=\" "); 
                getPreviousOut().print( name ); 
                getPreviousOut().print("PopupButton nowrap class=\"button\"><a href=\"#" );
                getPreviousOut().print( popupId );
                getPreviousOut().print("'); class=\"buttonRoll\"><img src=\"images/i_sort_des.gif\" border=\"0\"></a>" );
                getPreviousOut().print( content );
                getPreviousOut().print( "</td>\n" );
            }
            else // a basic button
            {
                getPreviousOut().print("\" target=\"_self\" id=\"");
                getPreviousOut().print(name);
                getPreviousOut().print("_label\">");
                getPreviousOut().print( buttonLabel );
                getPreviousOut().print("</a></td>\n");
            }

            getPreviousOut().print("</tr></table></td>\n");
        } catch (IOException e) {
            e.printStackTrace();
            throw new IllegalStateException("Error creating the page's HTML code.");
        }

        return SKIP_BODY;
    }

    public int doEndTag() throws JspException {
        return EVAL_PAGE;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public String getPopupButtonName() {
        if ( isButtonAPopupOnly() )
            return name;
        else
            return name + "PopupButton";
    }

    public void setScript(String script) {
        this.script = script;
    }

    public String getScript() {
        return script;
    }

    public void setHref(String href) {
        this.href = href;
    }

    public String getHref() {
        return href;
    }

    public void setCellattr(String cellattr) {
        this.cellattr = cellattr;
    }

    public String getCellattr() {
        return cellattr;
    }

    public void setButtonEnabled(String enabled) {
        this.enabled = enabled;
    }

    public String getButtonEnabled() {
        return enabled;
    }

    public boolean isEnabled() {
        return enabled.equals("enabled");
    }

    public void setIsPopupOnly(String isPopupOnly) {
        this.isPopupOnly = isPopupOnly;
    }

    public String getIsPopupOnly() {
        return isPopupOnly;
    }

    public boolean isButtonAPopupOnly() {
        return isPopupOnly.equals("true");
    }

    public void setHasPopup(String hasPopup) {
        this.hasPopup = hasPopup;
    }

    public String getHasPopup() {
        return hasPopup;
    }

    public boolean buttonHasAPopup() {
        return hasPopup.equals("true");
    }

    public void setButtonLabel(String buttonLabel) {
        this.buttonLabel = buttonLabel;
    }

    public String getButtonLabel() {
        return buttonLabel;
    }

    public void setPopupId(String popupId) {
        if ( this.popupId == null)
            this.popupId = popupId;
    }

    public String getPopupId() {
        return popupId;
    }
}

【问题讨论】:

    标签: java javascript jsp struts-1


    【解决方案1】:
    1. 在动作映射中,name="myForm" 应该与form-bean 中的name="myCSVForm" 相同
    2. 属性名称应与表单元素实体的名称相同
    3. 提交表单时自动设置属性值或使用(JSTL)&lt;c:set&gt;(不能使用JS)
    4. 使用&lt;bean:write&gt;获取值

    【讨论】:

    • 1.) myForm 写不正确.. 我在原始代码中看到没问题.. 2.) 属性名称已经相同。 3.) 你能否更详细地解释一下我如何使用 JSTL。 4.) 没关系
    • 错误提示 document.forms.myForm.elements.importButtonClicked' is null 你确定是页面中名为 importButtonClicked 的元素位于另一个名为 elements 的元素中?
    • 我已经使用了这两种方案,但在这两种情况下......我得到了同样的错误......
    猜你喜欢
    • 2013-03-30
    • 2015-08-30
    • 2014-02-20
    • 1970-01-01
    • 1970-01-01
    • 2021-10-11
    • 1970-01-01
    • 2014-06-08
    • 1970-01-01
    相关资源
    最近更新 更多