Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

how do I save boolean variable in JiraWebActionSupport class and velocity template?

Arthur
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 30, 2014

I have developed a gadget plugin and want to make several things configurable via a backend servlet. Everything runs fine except for a checkbox setting.

I use JiraWebActionSupport for the backend configuration page to manage saving and retrieving config variables:

public class Configuration extends JiraWebActionSupport {

and there is a method in it to save the config, that is called properly:

// called from velocity engine
    public String doSaveConfig() throws Exception {
        synchronized (lock) {
            // many config params are save here...
             
            System.out.println("enable Otrs = " + enable_otrs);
            PSUtil.put("enable_otrs", enable_otrs?"on":"off", settings);
            return SUCCESS;
        }
    }

the variable "enable_otrs" is the one from a checkbox in my velocity template for rendering the config page:

Enable OTRS :
                        
                        
                            #set($checked = '')
                            #if($enable_otrs)
                            #set ($checked = 'checked="checked"')
                            #end
                            
                                   $enable_otrs

... ok unfortunately the html markup is not shown here. but the important part is: the velocity variable $enable_otrs corresponds to the boolean variable enable_otrs in my Configuration.java class.

I also have getters and setters for the variable:

public Boolean getEnable_otrs() {
        return PSUtil.get("enable_otrs", settings).equals("on");
    }
    public void setEnable_otrs(Boolean enable_otrs) {
        System.out.println("SET ENABLE OTRS = "+ enable_otrs);
        this.enable_otrs = enable_otrs;
    }

 

So what is the problem? The problem is in the save-method the variable is always false! All my other config variables are transferred correctly.

What is the correct way to save and retrieve boolean values with velocity + JiraWebActionSupport?

 

thank you very much!

 

3 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Answer accepted
Arthur
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 30, 2014

Yeah I figured out how to save it:

If you have the checkbox unchecked, it doesn't get submitted at all! So the value in java of the variable is NULL. When you have checked the checkbox it gets submitted with the default value.

So in the save method you just have to check if the variable is NULL or not, like this:

String enabled = enable_otrs!=null ? "on":"off";
settings.put(key+"enable_otrs", enabled); // save it to PluginSettings
1 vote
Marcin Nikliborc September 30, 2014

Hi Arthur,

 

I am not 100% sure, but probably you should name 'enable_otrs' variable using camel case, i.e. enableOtrs. If it does not work, please paste velocity template.

 

Cheers,

Marcin

Arthur
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 30, 2014

thanks for the hint. I figured it out now. see my solution ;)

0 votes
Arthur
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 30, 2014

Hi Marcin, 

here is the part of the velocity template. I have other variables like $otrs_user, which I use to save a username. This works fine. the username is saved. $otrs_user corresponds in java to String otrs_user with getter and setter. 

So I thought I could do the same with a boolean and input type checkbox.  In fact I tried two things: First, with $enable_otrs beeing a String, second, with a boolean. In the save method, the $enable_otrs is always "off". 

Screen Shot 2014-10-01 at 14.39.32.png

Arthur
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 30, 2014

If I declare $enable_otrs as Boolean, then its always false. If I declare it as String it is always empty or has a default value, which I set to "off". The setter-method of $enable_otrs gets called if the variable is a Boolean. If it is a string it does not get called.

TAGS
AUG Leaders

Atlassian Community Events