java - How can I pass a hidden value from my Thymeleaf template to a controller(Spring Boot)? -
i'm running spring boot project , i'm trying pass id or object using post method keep returning null user. make user available when summary page created(post method hit separate form) user can added summary object.
i believe there might way pass id anchor link?
this html:
<div > <span th:text="${user.firstname}"></span> summary page <span th:text="${user.emailaddress}"></span> <form id="add" th:action="@{/addsummary}" method="post"> <input id="userid" name="${user.userid}" type="hidden" value="${userid}"/> <!-- <input id="userid" name="${user}" type="hidden" value="${user}"/> --> <button type="submit" value="save">add summary</button> </form> </div> <!-- way pass id? --> <!--<a href="createsummary.html?user.id">addsummary</a>-->
the controller:
@requestmapping(value ="/addsummary", method = requestmethod.post) public modelandview addsummary(user user) { try{ modelandview model = new modelandview("views/createsummary"); system.out.println("======post summary method hit====="); // model.addobject("summary", new summary()); user nuser = userservice.findbyid(user.getuserid()); model.addobject("user", nuser); system.out.println("user: " + nuser.getuserid()); return model; }catch(exception ex){ system.out.println(ex.tostring()); throw ex; } }
any or suggestions appreciated! - thanks
first, core problem code try use thymeleaf expressions in non-thymeleaf attributes, not work expected. thymeleaf @ attributes starting th:
(or data-th-
if using html5 syntax).
for case use th:object
, th:field
:
<form id="add" th:action="@{/addsummary}" method="post" th:object="${user}"> <input id="userid" th:field="*{userid}" type="hidden"/> <button type="submit" value="save">add summary</button> </form>
reference: creating form in thymeleaf + spring tutorial.
Comments
Post a Comment