Search code examples
javahtmljspstruts2struts-tags

make a button disable/enable base on java flg in the struts2


Action:

public class TuAction() extends ActionSupport{

    private boolean loseItemFlg=false;

    private String doFuilureOrder(){
        if(...){
           loseItemFlg=true;
        }
        return SUCCESS;
    }

    public boolean isLoseItemFlg() {
        return loseItemFlg;
    }

    public void setLoseItemFlg(boolean loseItemFlg) {
        this.loseItemFlg = loseItemFlg;
    }
}

And my Jsp:

function dialogOpen(formName,actionName){
    if(comfirm("do you want do this?")){
        ....
        document.forms[formName].action=actionName;
        document.forms[formName].submit();
    }else{
        //i want do not reload the page.
    }
}
<input type="button" disable="%{loseItemFlg}" value="lose" 
    onclick="dialogOpen('tuAction', '<%request.getContextPath()%>/tuAction_doFuilureOrder.action')" 
/>

But this code the button's disable property is not by my control!! Then i change the jsp to:

<s:submit type="button" disable="%{loseItemFlg}" value="lose"
       onclick="dialogOpen('tuAction', '<%request.getContextPath()%>/tuAction_doFuilureOrder.action')" 
/>

Now the button's disable property is by my control,but the "doFuilureOrder()" is not by used.

About do not reload the page should do what in my jsp.

My English is terrible,And this is my first time to use the stackoverflow. Someone know what I means.


Solution

  • You can't nest scriptlet in Struts tags (like in your second case), while you can (but you should NOT, because using scriptlets is a bad-practice) inject them in HTML tags.

    You can then use the <s:property /> tag in the HTML tag (first case)

    <input type = "button"
        disable = "<s:property value="%{loseItemFlg}"/>" 
        onclick = "dialogOpen('tuAction'), '<%request.getContextPath()%>/tuAction_doFuilureOrder.action')" 
    />
    

    , or substitute the scriptlet in your Struts tag (second case), better using the <s:url /> tag to mount the URL:

    <s:url action = "tuAction_doFuilureOrder.action" 
        namespace = "/"
              var = "myUrl" 
    />        
    <s:submit type = "button" 
           disable = "%{loseItemFlg}" 
           onclick = "dialogOpen('tuAction'), '%{myUrl}')" 
    />
    

    they both work.

    The <s:url /> usage can (and should) be applied to the first case too:

    <s:url action = "tuAction_doFuilureOrder.action" 
        namespace = "/"
              var = "myUrl" 
    />        
    <input type = "button" 
        disable = "<s:property value="%{loseItemFlg}"/>" 
        onclick = "dialogOpen('tuAction', '<s:property value="%{#myUrl}"/>')" 
    />