Java 如何在按钮的单击事件中使用jsp将属性设置为StrutsBean

Java 如何在按钮的单击事件中使用jsp将属性设置为StrutsBean,java,javascript,jsp,struts-1,Java,Javascript,Jsp,Struts 1,我是struts甚至web应用程序的新手。。。我想在按钮的单击事件中使用jsp设置struts表单bean的属性。但我不能做同样的事 代码如下: jsp代码 function importDistList(){ alert("Importing List function called..."); var val = document.forms['myForm'].importButtonClicked.value; var val = document.forms[

我是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>
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>

");
getPreviousOut()。打印(内容);
getPreviousOut().print(“\n”);
}
else if(buttonhaspopup())
{
getPreviousOut().print(“\”target=\”\u self\”id=\”);
getPreviousOut().print(名称);
getPreviousOut()。打印(“\u label\”>”);
getPreviousOut().print(按钮标签);
getPreviousOut().print(“\n”);
getPreviousOut()。打印(“”);
getPreviousOut().print(按钮标签);
getPreviousOut().print(“\n”);
}
getPreviousOut().print(“\n”);
}捕获(IOE异常){
e、 printStackTrace();
抛出新的IllegalStateException(“创建页面HTML代码时出错”);
}
返回跳过单元体;
}
public int doEndTag()抛出JspException{
返回评估页面;
}
公共void集合名(字符串名){
this.name=名称;
}
公共字符串getName(){
返回名称;
}
公共字符串getPopuButtonName(){
如果(IsButtonOpopoponly())
返回名称;
其他的
返回名称+“弹出按钮”;
}
公共void setScript(字符串脚本){
this.script=脚本;
}
公共字符串getScript(){
返回脚本;
}
公共void setHref(字符串href){
this.href=href;
}
公共字符串getHref(){
返回href;
}
公共void setCellattr(字符串cellattr){
this.cellattr=cellattr;
}
公共字符串getCellattr(){
返回cellattr;
}
public void setButtonEnabled(字符串已启用){
this.enabled=已启用;
}
公共字符串getButtonEnabled(){
返回启用;
}
公共布尔值isEnabled(){
返回启用。等于(“启用”);
}
public void setispopuploy(字符串ispopuploy){
this.ispopuply=ispopuply;
}
公共字符串getispopuploy(){
回报率高;
}
公共布尔值IsButtonOpupoponly(){
返回ispopuply.equals(“true”);
}
public void sethaspoop(字符串haspoop){
this.hasPopup=hasPopup;
}
公共字符串getHasPopup(){
返回窗口;
}
公共布尔按钮hapopup(){
返回haspoop.equals(“true”);
}
公共无效设置按钮标签(字符串按钮标签){
this.buttonLabel=按钮标签;
}
公共字符串getButtonLabel(){
返回按钮标签;
}
公共void setPopupId(字符串popupId){
if(this.popupId==null)
this.popupId=popupId;
}
公共字符串getPopupId(){
返回popupId;
}
}
  • 在动作映射中,
    name=“myForm”
    应该与
    formbean中的
    name=“myCSVForm”
    相同
  • 属性名称应与表单元素实体的名称相同
  • 提交表单或使用(JSTL)
    (您不能使用JS)时会自动设置属性值
  • 使用

  • 1.)我的表格写得不正确。。我在原始代码中看到它是可以的。。2.)属性名称已相同。3.)你能更详细地解释一下我如何使用JSTL来实现同样的目的吗。4.)确定错误为
    document.forms.myForm.elements.importButtonClicked'为空
    您确定页面中名为importButtonClicked的元素在另一个名为elements的元素中吗?我使用了这两种情况,但在这两种情况下。。。我得到了同样的错误。。。
    <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>
    
    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;
        }
    }