chenyan
chenyan

Reputation: 13

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.

Upvotes: 1

Views: 6103

Answers (2)

hKs
hKs

Reputation: 516

For the input tag, disabled property is not based on true/false. When u write disabled attribute then the input is disabled by default, Please check Fiddle

<input type ='button' disabled='true' value='Button1'/>
<input type ='button' disabled='false' value='Button2'/>

In above code, both buttons is in disabled state.

For your purpose we can rewrite your code as:

Method 1:

By using struts if tag

<s:if test="%{loseItemFlg}"> // if true - button disabled state
  <input type="button" disabled value="lose" onclick="dialogOpen('tuAction','<%request.getContextPath()%>/tuAction_doFuilureOrder.action')"/>
</s:if><s:else> //button enabled
   <input type="button" value="lose" onclick="dialogOpen('tuAction','<%request.getContextPath()%>/tuAction_doFuilureOrder.action')"/>
</s:else>

Method 2:

Rewrite your java code as,

private String loseItemFlg='';

private String doFuilureOrder(){
    if(...){
       loseItemFlg="disabled";
    }
    return SUCCESS;
}
public String getLoseItemFlg() {
    return loseItemFlg;
}

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

then in jsp :

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

Upvotes: 2

Andrea Ligios
Andrea Ligios

Reputation: 50261

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}"/>')" 
/>

Upvotes: 1

Related Questions