【发布时间】:2016-06-01 15:04:13
【问题描述】:
经过 15 小时的试验和恐惧,我决定与全世界分享我的问题,因为坦率地说,我不知道如何解决我的问题了:
我创建了一个自定义表单,供用户检查复选框并按下保存按钮: 下面是相关代码的sn-p:
<form th:action="@{/profile}" action="/profile" th:object="${profileModel}" method="POST">
<div class="row" style="margin-left: 5px;"><h4>Select</h4></div>
<div class="row">
<!-- Barbell -->
<section class="panel panel-default panel-equipment col-md-3">
<header class="panel-heading panel-heading-profileform">
<h3 class="panel-title"><b>Barbell</b></h3>
</header>
<div class="col-md-12">
<div><input type="checkbox" th:field="*{equipment.barbellLight}" th:value="1"> Light</input></div>
<div><input type="checkbox" th:field="*{equipment.barbellMedium}" th:value="1"> Medium</input></div>
<div><input type="checkbox" th:field="*{equipment.barbellHeavy}" th:value="1"> Heavy</input></div>
</div>
</section>
</div>
</div>
</form>
我的控制器: 请求:
@RequestMapping(value = "/profilestage", method=RequestMethod.POST)
public String updateProfileStage(@ModelAttribute ProfileModel profileModel, Model model){
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
String username = auth.getName();
model.addAttribute("signedin", true);
model.addAttribute("username", username.substring(0, 1).toUpperCase() + username.substring(1));
UserEntity user = userService.findByUsername(username);
ProfileEntity profile = user.getProfile();
model.addAttribute("profileModel", ModelEntityConvertor.profileEntityToModel(profile));
return "profile_new";
}
这不是控制器的全部,但据您所知,我已经非常彻底地调试了所有这些代码,并且进入的 profileModel(th:object 中使用的那个)永远不会为空,并且始终设置了所有必填字段(在我的情况下全为 0)。
表单正在发布到此控制器:
@RequestMapping(value = "/profile", method=RequestMethod.POST)
public String updateProfile(@ModelAttribute ProfileModel profileModel, Model model){
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
String username = auth.getName();
UserEntity user = userService.findByUsername(username);
ProfileEntity profile = user.getProfile();
profile.setEquipment(ModelEntityConvertor.equipmentModelToEntity(profileModel.getEquipment(), profile.getEquipment()));
profileService.updateProfile(profile);
return this.getProfile(model);
}
每当我的应用程序决定不工作时,永远无法访问此控制器,但无论何时访问它,它都会像魅力一样工作,并且完全按照它应该的方式工作。
我试图解决的一个大问题是,所有这些偶尔都会起作用。我运行代码一分钟,没有任何问题。然后我重新运行完全相同的代码,尝试与另一个用户一起执行此操作,或者两者都执行,然后单击save 后会显示 spring 的默认错误页面。
任何地方都没有提供错误消息...
我试过了:
- 清理项目
- 关闭日食
- 完全重建项目
- 在 chrome 的开发者模式下工作(我总是这样做,css 的任何更改都会显示出来)
- 将 spring.template.cache: false 和 spring.thymeleaf.cache: false 添加到属性中
老实说,我不明白我做错了什么,非常感谢您的帮助 =)
请提前阅读我的文字墙并为后者道歉。 如果需要更多我的代码,我会不停地在这里。
【问题讨论】:
-
也许尝试在您的记录器中将其设置为 DEBUG:
org.springframework.web和org.spring.web.context并尝试重现您的问题。它可能会让您对未正确命名的页面等有所了解。 -
另外,不确定您的设计,但您是否尝试在您的第一个控制器块上将参数更改为
method=RequestMethod.GET?那里有 POST 吗? -
我对此比较陌生,你是对的,显然它应该是一个 GET。我已经设置了日志记录级别,我现在实际上得到了一个错误!我在第二个控制器中的 ProfileModel 的 2 个元素上检查了 3 个复选框中的 1 个 -> null。我检查了所有 3 个,就像一个魅力!猜猜我已经解决了我的问题 =) 需要将其他值设置为 0,因为我正在使用 primitves
标签: java html spring-boot thymeleaf