How to pass the value of the switch that pressed the MVC controller to Spring?

I am using Spring MVC in one of my projects.

Below is my JSP code, which will show three switches. One - Insert, the second switch - Update, and the third - Delete.

As soon as I press the button Insert, it shows two text fields next to the radio button Insert, and the same with the other two radio buttons. Here is my jsfiddle

<script type="text/javascript">
    $(document).ready(function(){
        $(".changeAction").on("click", function(){
            $('.changeable').html('')
            var divId = "#" + $(this).attr("div-id");
            var myInput = '<label for="Node"> Node </label> <input type="text" name="node" size="20" /> <label for="Data"> Data </label> <input type="text" name="data" size="100"/>'
            $(divId).html(myInput);
        })
    })      
</script>

<body>
<form method="post" action="testOperation">
    <!-- I used only one hidden box to store value -->
    <input type="hidden" name="name" id="dynamicName">
    <input class="changeAction" type="radio" name="tt" value="Insert" div-id="insert"/> Insert
    <div id="insert" class="changeable"></div>
    <br/> <input class="changeAction" type="radio" name="tt" value="Update" div-id="update"/> Update
    <div id="update" class="changeable"></div>
    <br/> <input class="changeAction" type="radio" name="tt" value="Delete" div-id="delete"/> Delete
    <div id="delete" class="changeable"></div>
    <br/>
    <input type="submit">
</form>
</body>

Below is my method in the 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;
    }

Problem: -

, Insert Hello World , , Hello node variable World data, .

- name , Insert, .

.

, ?

+4
2

Javascript . .

$(document).ready(function(){
    $(".changeAction").on("click", function(){
        $('.changeable').html('')
        var divId = $(this).attr("div-id");
        $("#dynamicName").val(divId);
        divId = "#"+divId;
        var myInput = '<label for="Node"> Node </label> <input type="text" name="node" size="20" /> <label for="Data"> Data </label> <input type="text" name="data" size="100"/>'
        $(divId).html(myInput);
    })
})

jsfiddle http://jsfiddle.net/EGJk8/5/

+3

,

,

@RequestParam String name

To,

@RequestParam String tt

tt. , Insert , Update , Delete.

0

Source: https://habr.com/ru/post/1530360/


All Articles