如何从CONTROLLER访问JSP中的值?

时间:2017-02-24 09:49:25

标签: javascript jsp session controller setattribute

<div>
    <input name="wordCount" type="text"/>
    <input type="submit" name="start" value="Start" onclick=""/>
</div>

当我点击“开始”时按钮,我想从控制器访问此输入值。但是,我不想使用表单标签。我可以使用Javascript设置为会话值吗?如果是,我该怎么办?感谢名单。

1 个答案:

答案 0 :(得分:0)

You can use an ajax call to send your data to a controller.

<强> HTML

<div>
<input name="wordCount" id="wordCount" type="text"/>
<input type="submit" name="start" value="Start" onclick="ajaxCall()"/>
</div>

<强> JQuery的

 function ajaxCall() {
    var value = $('#wordCount').val();
    $.ajax({
            type : 'POST',
            url : 'controller.htm',
            data:{value:value},
            success : function(data) {
                alert('Success');
            }
        });
    }

<强>控制器

    @RequestMapping(value="/controller" ,method = RequestMethod.POST)
    @ResponseBody
    public String restructure(Model model, HttpSession session, HttpServletResponse response, final RedirectAttributes redirectAttributes,
    @RequestParam(value = "value",required=false) String value){
        System.out.println("Value from JSP "+ value);
    }

希望有所帮助:)