-1

如何从 spring 控制器设置隐藏的输入字段值(在 jsp 中)?

4

1 回答 1

0

您可以从数据库中获取一些值(id)(使用 findAllIdForUsers() 方法)并使用模型转发到您的 JSP:

@Autowired
ServiceInterface service;

    @RequestMapping(value = "example.html", method = RequestMethod.GET)
    public String viewUsers(Model model){       
    model.addAttribute("usersId", service.findAllIdForUsers());
    return "example";
    }

在 JSP 上,您可以在某个按钮上设置此 ID:

<c:forEach items="${ requestScope['users'] }" var="users">

<form action="<%=response.encodeURL(request.getContextPath() + "delete.html") %>" method="POST"> 
<input type="hidden" name="id" value="<c:out value="${ usersId.id }"/>" />
<input type="SUBMIT" value="Delete User" /></form>
</c:forEach>

然后单击按钮,您可以将 id 发送到另一个控制器并工作或删除该用户:

@RequestMapping(value = "delete.html", method = RequestMethod.POST)
    public String deleteUser(@RequestParam("id") int id, Model model) {
    service.delete(id);
    model.addAttribute("usersId", service.findAllIdForUsers());
        return "example";
    }
于 2012-11-19T18:42:31.623 回答