简体   繁体   English

如何将具有数据的文本框值传递给spring MVC控制器?

[英]How to pass textbox value which has the data to spring MVC controller?

I am using Spring MVC controller in one of my project. 我在我的一个项目中使用Spring MVC控制器。

Below is my JSP code in which it will show three radio button. 下面是我的JSP代码,它将在其中显示三个单选按钮。 One is Insert , second radio button is Update and third radio button is Delete . 一个是Insert ,第二个单选按钮是Update ,第三个单选按钮是Delete

Once I click Insert radio button, it show two text box next to Insert radio button, and same thing with other two radio button as well - 一旦我点击Insert单选按钮,它会在插入单选按钮旁边显示两个文本框,同样的东西与其他两个单选按钮一样 -

<script type="text/javascript">
function doDisplay(radio) {
    switch (radio.value) {
    case "Insert":
        document.getElementById("inserts").style.display = "inline";
        document.getElementById("updates").style.display = "none";
        document.getElementById("delete").style.display = "none";
        document.getElementById("dynamicName").value = "insert";
        break;
    case "Update":
        document.getElementById("inserts").style.display = "none";
        document.getElementById("updates").style.display = "inline";
        document.getElementById("delete").style.display = "none";
        document.getElementById("dynamicName").value = "update";
        break;
    case "Delete":
        document.getElementById("inserts").style.display = "none";
        document.getElementById("updates").style.display = "none";
        document.getElementById("delete").style.display = "inline";
        document.getElementById("dynamicName").value = "delete";
        break;
    }
}
</script>

<body>

  <form method="post" action="testOperation">
  <!-- I used only one hidden box to store value -->
  <input type="hidden" name="name" id="dynamicName">

    <input type="radio" name="tt" value="Insert"
        onclick="doDisplay(this);" /> Insert <span id="inserts"
        style="display: none"> <label for="Node"> Node </label> <input
        type="text" name="node" size="20" /> <label for="Data"> Data </label>
        <input type="text" name="data" size="100" />

    </span> <br /> <input type="radio" name="tt" value="Update"
        onclick="doDisplay(this);" /> Update <span id="updates"
        style="display: none"> <label for="Node"> Node </label> <input
        type="text" name="node" size="20" /> <label for="Data"> Data </label>
        <input type="text" name="data" size="100"/>

    </span> <br /> <input type="radio" name="tt" value="Delete"
        onclick="doDisplay(this);" /> Delete <span id="delete"
        style="display: none"> <label for="Node"> Node </label> <input
        type="text" name="node" size="20" /> <label for="Data"> Data </label>
        <input type="text" name="data" size="100"/>

    </span> <br />
    <input type="submit">
</form>

Below is 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 name, 
                                              @RequestParam String node, 
                                              @RequestParam String data) {
        final Map<String, String> model = new LinkedHashMap<String, String>();

        System.out.println(name);
        System.out.println(node);
        System.out.println(data);

        return model;
    }

Suppose I click Insert radio button and type Hello in the first text and World in the second text box, then I will hit the submit button, and after that I am seeing insert value in the name variable which is good but hello,, value in the node variable and world,, value in the data variable which I don't want. 假设我单击Insert单选按钮并在第一个文本中键入Hello ,在第二个文本框中键入World ,然后我将点击提交按钮,之后我在name变量中看到insert值,这是好的但是hello,,值为node变量和world,,我不想要的data变量中的值。

And same thing with update and delete. 更新和删除同样的事情。

Is there any way to avoid this? 有什么方法可以避免这种情况吗? Meaning I would like to see only hello value in the node variable and world value in the data variable instead of being separated by comma.? 含义我想在data变量中只看到节点变量和world值中的hello值,而不是用逗号分隔。

Can anyone help me with this? 谁能帮我这个?

Clicking radio buttons you just hide fields for user, but params are still visible and send via POST request. 单击单选按钮,您只需为用户隐藏字段,但params仍然可见并通过POST请求发送。 In spring params with that same names are seen as an array. 在春天,具有相同名称的params被视为一个数组。 If you dont want to send multiply params with that same name you should not only hide input but remove it calling following code from jquery 如果你不想发送具有相同名称的乘法参数,你不仅应该隐藏输入,还要删除它从jquery调用以下代码

$('#input-id').remove();


Remember that you have also to service adding fields by clicking radio buttons. 请记住,您还必须通过单击单选按钮来添加字段。 I found an example how to to it: http://jsfiddle.net/jaredwilli/tZPg4/4/ 我找到了一个如何实现的例子: http//jsfiddle.net/jaredwilli/tZPg4/4/
More info you can find also here Add / remove input field dynamically with jQuery 您可以在此处找到更多信息使用jQuery动态添加/删除输入字段

EDIT 编辑
According to your comment i recomend you to use this code http://jsfiddle.net/tFLnd/ 根据您的评论,我建议您使用此代码http://jsfiddle.net/tFLnd/
It is much more shorter, maybe not the best one because i wrote it just now but do the job 它要短得多,也许不是最好的,因为我刚刚写了它但是做了这个工作
Of course you have to add jQuery to your page to use this code 当然,您必须将jQuery添加到您的页面才能使用此代码

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

相关问题 如何将单击的单选按钮的值传递给Spring MVC控制器? - How to pass value of radio button which is clicked to Spring MVC controller? SPRING MVC:如何将单击的单选按钮值传递给 controller? - SPRING MVC : How to pass the radio button value which is clicked to controller? 如何传递文本框值以及对spring mvc控制器的作用是什么? - How to pass textbox values and what was the action to spring mvc controller? java - 如何将动态添加的文本框传递给spring MVC控制器java - How to pass dynamically added textbox to spring MVC controller java 如何将@RequestBody 值传递给转发控制器 Spring MVC - How to pass @RequestBody value to forwarded controller Spring MVC 如何将 URL 参数的 null 值传递给 Spring MVC controller? - How to pass a null value for a URL parameter to Spring MVC controller? Spring MVC:如何将表单数据从JSP传递到控制器 - Spring mvc : How to pass the form data from jsp to controller 如何在 Spring-MVC 中将数据从视图传递到控制器? - How to pass data to from view to controller in Spring-MVC? 如何通过 <Select>和<input>数据从视图到控制器(Spring MVC) - How to Pass <Select> and <input> data from View to a Controller (Spring MVC) 将值传递给Spring MVC中的控制器,有什么区别? - Pass value to controller in Spring MVC, what is the difference?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM