Spring thymeleaf向控制器发送javascript对话框消息

时间:2018-02-07 03:20:33

标签: javascript spring thymeleaf

表格显示记录列表,每个记录都有一个接受拒绝按钮。

点击拒绝后,会出现一个javascript提示,输入拒绝原因。

需要将拒绝原因+记录ID发送给控制器。

<script>
    <!-- prompt a box to type in reject reason -->
    function rejectPrompt() {
        var txt;
        var rejectMsg = prompt("Please enter your Reject message:", " ");
        document.getElementById("rejectMsg").innerHTML = rejectMsg;
    }
</script>

/*table to show record accept-reject button*/
<td>
    <form action="#" data-th-action="@{/accountantApplication}" method="post">
        <input type="hidden" name="id" th:value="${acc.id}" />
        <input type="hidden" id="rejectMsg" name="rejectMsg" th:value="${acc.id}" />                                                     
        <button type="submit" name="action" value="Accept">Accept</button>
        <button type="submit" name="action" onclick="rejectPrompt()" value="Reject">Reject</button>
    </form>
</td>

@PostMapping(value="/accountantApplication", params="action=Reject")
public ModelAndView Reject(@RequestParam String id,@RequestParam String rejectMsg) {
    ModelAndView modelAndView = new ModelAndView();

    System.out.println("rejectMsg:"+rejectMsg);
    System.out.println("id:"+id);

    accountantService.rejectAccountant(id);
    modelAndView.setViewName("RejectAccountant");
    return modelAndView;
}

问题是,拒绝消息未到达控制器。只发送正确的ID。我如何发送id和消息?

或者,如果有更好的方法来实施它,请提出建议。非常感谢!

1 个答案:

答案 0 :(得分:2)

设置document.getElementById("rejectMsg").innerHTML = rejectMsg就像HTML:

 <input type="hidden" id="rejectMsg" name="rejectMsg" value="someAccId">Some rejectMsg</input>

要将"Some rejectMsg"发送到服务器,您必须设置value的{​​{1}}:

<input>

请注意,这会覆盖document.getElementById("rejectMsg").value = rejectMsg

中的效果th:value="${acc.id}"