webdeveloper
webdeveloper

Reputation: 1

Set default value to <s:checkbox> tag in Struts 2

I am using <s:checkbox> of Struts 2.3 the following is the line of code:

<s:checkbox
  theme="simple" 
  name ="Mychkbox"
  value="R"
  onclick="Auto_Au('rej');"
/>

Now in this I need this value in action , but I am not finding any way to set this value "R" because the fieldValue Parameter of <s:checkbox> will give true or false.

I need to set the value as 'R' that can be accessed in the action class.

Upvotes: 0

Views: 11052

Answers (4)

Roman C
Roman C

Reputation: 1

The value of checkbox input field that is submitted by default is set to true. You can change this by providing fieldValue attribute of the checkbox tag. For example

<s:checkbox  theme="simple" name ="mychkbox" fieldValue="R" onclick="Auto_Au('rej');"/>

If you want to preset the value of the input element you should provide a value attribute. For example

<s:checkbox  theme="simple" name ="mychkbox" fieldValue="R" value="%{mychkbox}" onclick="Auto_Au('rej');"/>

Note that for better readability I've changed the name of the field. To map a field name to the action bean you should provide a property for the mychkbox.

private String mychkbox;

public String getMychkbox() {
  return mychkbox;
}

public void setMychkbox(String mychkbox) {
  this.mychkbox= mychkbox;
} 

The default stack of interceptors include checkbox interceptor for which you should specify uncheckedValue parameter. For this purpose you should override the parameter of the interceptor

<interceptor-ref name="checkbox">
  <param name="uncheckedValue">undefined</param>
</interceptor-ref>

Upvotes: 0

Andrea Ligios
Andrea Ligios

Reputation: 50281

You need a property in your target Action with the following setter

setMychkbox(String mychkbox){
    this.mychkbox = mychkbox;
}

but you are using a capital letter in your name attribute, that will prevent the correct mapping by Struts2.

Change it to

<s:checkbox theme="simple" name ="mychkbox" value="R" onclick="Auto_Au('rej');"/>

with a lowercase starting m to make it work.

Upvotes: 0

afsal parangooz
afsal parangooz

Reputation: 162

<s:checkbox name="checkMe" fieldValue="true" label="Check Me for testing"/> this is the syntax for Checkbox.By using same name(here name is 'checkMe') and adding getter and setter method for it in action class we can get this value in action class. example:My Action.class

private String checkMe;

public String getCheckMe() {
        return checkMe;
    }
public void setCheckMe(String checkMe) {
        this.checkMe = checkMe;
    }

by using the getCheckMe() method we can get the value of checkbox 'checkMe'.

In your example use 'mychkbox' instead of 'checkMe'. This site have a simple example/ Struts 2 checkbox Checkbox Example

Upvotes: 0

hari
hari

Reputation: 1963

if you are using private boolean Mychkbox; it give result like true or false.make correct it boolean to String datatype.

your value must set in fieldValue="R"

action.jsp

    <s:form action="mylogin">
     <s:checkbox  theme="simple" name ="Mychkbox" fieldValue="R" value="true" onclick="Auto_Au('rej');"/>
     </s:form>

MyAction.class

    private String Mychkbox;
     public String execute() throws Exception {
          System.out.Println(" vaue : "+Mychkbox);
     }

value attributes-boolean type.(UI components value set)

fieldValue attribute--string type(value of Mychkbox)

Upvotes: 0

Related Questions