简体   繁体   English

如何传递文本框值以及对spring mvc控制器的作用是什么?

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

I am working on a Spring MVC controller project. 我正在从事Spring MVC控制器项目。

  • First button is, INSERT . 第一个按钮是INSERT As soon as I click INSERT button, I am showing three text box just below the INSERT button. 单击INSERT按钮后,我将在INSERT按钮下面显示三个文本框。 First textbox is datacenter , second textbox is node and third textbox is data . 第一个文本框是datacenter ,第二个文本框是node ,第三个文本框是data
  • Second button is, UPDATE . 第二个按钮是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 . 第三个按钮是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 . 在这个文本框中将是node
  • Fourth button is, PROCESS . 第四个按钮是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 在此第一个文本框将是datacenter ,第二个文本框将是node ,第三个文本框将是data ,第四个文本框将是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. 这是我的jsfiddle ,其中包含我上面讨论的所有内容。

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 . 现在,我需要做的是,假设我先单击“ Insert按钮,它将在“插入”按钮下面提供三个文本框,然后在这三个文本框中键入一些值,然后单击“ Submit按钮,该按钮将传递所有这三个文本框值以及我对控制器方法testOperations操作(意味着是INSERT或UPDATE或DELETE或PROCESS)。 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. 因此,在这种情况下,如果我在数据中心中键入dc1 ,在节点中输入hello ,在数据中输入world ,那么它应该将所有这三个值传递给我的控制器方法,但还应该将conf值作为null bcoz传递给该文本框(对于“ Insert按钮不存在)因此,当我们按下Submit按钮时,它将conf值作为null传递给控制器​​方法。

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. 同样,如果单击“删除”按钮,则它将仅在“删除”按钮下方显示一个文本框,一旦单击“提交”按钮,则它应将“删除”值作为操作传递,数据中心值应为null,节点值作为我们键入的实际值在框中,数据值也应该为null,而conf值也应该为null。对于其他情况也是如此。

Below is my method in Controller code - 以下是我在Controller代码中的方法-

   @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? 但是无论如何,只要我在输入一些值后按下提交按钮,就会给我一个异常,例如Bad Request Exception。而且我知道为什么吗? 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.. 因为假定在这三个文本框中键入值后为Insert情况,所以我按了Submit按钮,则未设置conf值,并且我的控制器方法使用conf参数,因此这是它引发异常的原因。为了解决此问题,我可以删除conf参数,但是对于Process案例,我需要它。

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: 选项1)不需要所有对所有三个动作都必需的参数,即:

@RequestParam(required=false, defaultValue=null) String conf @RequestParam(required = false,defaultValue = null)字符串配置

If you add "required=false" to all the non-mandatory params, you will not get the exception 如果将“ required = false”添加到所有非强制性参数,则不会获得异常

option 2) make different controller methods for each operation, and make each method take only the required params for the specified operation: 选项2)为每个操作设置不同的控制器方法,并使每个方法仅采用指定操作所需的参数:

 @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! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将具有数据的文本框值传递给spring MVC控制器? - How to pass textbox value which has the data to spring MVC controller? java - 如何将动态添加的文本框传递给spring MVC控制器java - How to pass dynamically added textbox to spring MVC controller java 将值传递给Spring MVC中的控制器,有什么区别? - Pass value to controller in Spring MVC, what is the difference? 如何将javascript变量传递给Spring mvc控制器 - how to pass javascript variable to Spring mvc controller Spring MVC:如果我在控制器操作中启动一个线程会发生什么? - Spring MVC: What happens if I start a thread in a controller action? Spring MVC将一个值列表从JSP页面传递给控制器 - Spring MVC pass a list of values into controller from JSP page 在spring mvc中重定向后,从控制器传递参数的方法是什么? - What are ways for pass parameters from controller after redirect in spring mvc? 如何在Spring MVC中将LIST从一个控制器传递到另一个控制器 - How to pass LIST from one controller to another controller in spring mvc 如何将单击的单选按钮的值传递给Spring MVC控制器? - How to pass value of radio button which is clicked to Spring MVC controller? 如何在Spring MVC中使用jQuery Ajax serialize()将文件传递给控制器 - how to pass file to controller using jquery ajax serialize() in spring mvc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM