简体   繁体   中英

How to pass textbox values and what was the action to spring mvc controller?

I am working on a Spring MVC controller project.

  • First button is, INSERT . As soon as I click INSERT button, I am showing three text box just below the INSERT button. First textbox is datacenter , second textbox is node and third textbox is data .
  • Second button is, UPDATE . As soon as I click UPDATE button, I am showing same above three text box just below the UPDATE button.
  • Third button is, DELETE . As soon as I click DELETE button, I am showing only one text box just below the DELETE button. In this one text box will be node .
  • Fourth button is, PROCESS . As soon as I click PROCESS button, I am showing four text box just below the PROCESS button. In this first textbox will be datacenter , second textbox will be node , third textbox will be data and fourth textbox will be conf

And lastly there will be submit button which I will be pressing.

And here is my jsfiddle which has everything which I talked about above.

Problem Statement:-

Now what I need to do is, suppose if I am clicking Insert button first which will provide three text box just below the Insert button and then after typing some values in those three text box, I will be clicking Submit button which should pass the all those three text box values along with what action I did (meaning it was INSERT or UPDATE or DELETE or PROCESS) to my controller method testOperations . So for this case, if I typed dc1 in datacenter, hello in node, world in data, then it should pass all these three values to my controller method but it should also pass conf value as null bcoz this textbox was not there for Insert button so when we press submit button, it should pass conf value as null to controller method.

Similarly If I click Delete button, then it will show only one text box below the Delete button and once I click Submit button, then it should pass Delete value as an action, datacenter value should be null, node value as the actual value we typed in the box, data value should be null as well and conf value should be null.. Similarly for others as well.

Below is my method in Controller code -

   @RequestMapping(value = "testOperation", method = RequestMethod.GET)
    public Map<String, String> testOperation() {
        final Map<String, String> model = new LinkedHashMap<String, String>();
        return model;
    }

    @RequestMapping(value = "testOperation", method = RequestMethod.POST)
    public Map<String, String> testOperations(@RequestParam String action,
                                              @RequestParam String datacenter, 
                                              @RequestParam String node, 
                                              @RequestParam String data,
                                              @RequestParam String conf) {
        final Map<String, String> model = new LinkedHashMap<String, String>();

        System.out.println(action);
        System.out.println(datacenter);
        System.out.println(node);
        System.out.println(data);
        System.out.println(conf);

        return model;
    }

But somehow whenever I am pressing submit button after typing some values, it is giving me exception as Bad Request Exception.. And I know the reason why? Because suppose for the Insert case after typing values in those three textbox, I pressed Submit button, then conf value is not set and my controller method takes conf parameter so that is the reason it is throwing an exception.. And to fix this, I can remove the conf parameter but then for Process case, I need it..

So is there any way, I can pass null value for those text box which I don't need for certain buttons to controller to avoid this issue?

You have two options to solve your problem.

option 1) All the params that are not mandatory for all three actions, should be non-required, ie:

@RequestParam(required=false, defaultValue=null) String conf

If you add "required=false" to all the non-mandatory params, you will not get the exception

option 2) make different controller methods for each operation, and make each method take only the required params for the specified operation:

 @RequestMapping(value = "insertOperation", method = RequestMethod.POST)

    public Map<String, String> insertOperation(
        @RequestParam String action,
        @RequestParam String datacenter, 
        @RequestParam String node, 
        @RequestParam String data
    ) {}

and:

@RequestMapping(value = "updateOperation", method = RequestMethod.POST)
        public Map<String, String> updateOperation(@RequestParam String action,
                                                  @RequestParam String datacenter, 
                                                  @RequestParam String node, 
                                                  @RequestParam String data,
                                                  @RequestParam String conf,
                                                  ) {}

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM