Question

I am newbee to struts and even web application... I want to set the property of struts form bean using jsp on click event of button . But i am not able to do the same ....

The following is the code :

jsp code

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>

I am not getting the value true..and alert is not shown... instead it gives error Message: 'document.forms.myForm.elements.importButtonClicked' is null or not an object

form bean code :

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>

In the above jsp code, the custom taglib is used and the code for that is :

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;
    }
}
Was it helpful?

Solution

  1. in the action mapping, the name="myForm" should be the same as name="myCSVForm" in the form-bean
  2. The property names should be the same as the name of the form element entities
  3. the property values are automatically set when the form is submitted or use (JSTL)<c:set> (you can't use JS)
  4. fetch the values using <bean:write>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top