在HTML中,需要传值给controller页面,然后跳转到指定页面,不可以直接页面直接跳转——jsp可以,所以这个时候就有了thymeleaf模板引擎。
首先,在传值页面设置需要传值的参数:
在跳转页面的URL上直接拼接参数:
context + "/user/userDetails?" + userId
然后到controller中,指定需要跳转的页面,并使用Model来接收前一个页面传递过来的参数:
@RequestMapping("/userDetailsPage")
public String userDetails(String userId, Model model){
model.addAttribute("userId", userId);
return PageConstant.USER_DETAILS_PAGE;
}
最后,在需要使用参数的页面去接收并赋值使用:
// 使用span标签,或者其他都可以,并隐藏
<span id="userId" th:text="${userId}"></span>
// 在需要使用userId的地方获取userId的值
var userId = $('#userId').html();
至此,HTML中页面传值问题完美解决。
当然也可以使用sessionStorage方法,详情参考上一篇文章: