从弹簧控制器设置字段值

时间:2012-11-06 12:45:49

标签: java spring spring-security

我可以从spring Controller设置一个隐藏的输入字段值(在jsp中)吗?

1 个答案:

答案 0 :(得分: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";
    }