我的 SpringMVC 控制器中有下一个工作代码:
@RequestMapping(value = "/register", method = RequestMethod.GET)
public void registerForm(Model model) {
model.addAttribute("registerInfo", new UserRegistrationForm());
}
@RequestMapping(value = "/reg", method = RequestMethod.POST)
public String create(
@Valid @ModelAttribute("registerInfo") UserRegistrationForm userRegistrationForm,
BindingResult result) {
if (result.hasErrors()) {
return "register";
}
userService.addUser(userRegistrationForm);
return "redirect:/";
}
简而言之,create
尝试验证UserRegistrationForm
。如果表单有错误,它将使用户在同一页面上填写表单字段,并在其中显示错误消息。
现在,我需要将相同的行为应用于另一个页面,但是在这里我遇到了一个问题:
@RequestMapping(value = "/buy/{buyId}", method = RequestMethod.GET)
public String buyGet(HttpServletRequest request, Model model, @PathVariable long buyId) {
model.addAttribute("buyForm", new BuyForm());
return "/buy";
}
@RequestMapping(value = "/buy/{buyId}", method = RequestMethod.POST)
public String buyPost(@PathVariable long buyId,
@Valid @ModelAttribute("buyForm") BuyForm buyForm,
BindingResult result) {
if (result.hasErrors()) {
return "/buy/" + buyId;
}
buyForm.setId(buyId);
buyService.buy(buyForm);
return "redirect:/show/" + buyId;
}
我遇到了动态网址的问题。现在,如果表单有错误,我应该指定相同的页面模板以保留在当前页面上,而且还应该将其buyId
作为路径变量传递。这两个要求在哪里冲突。如果我按原样保留此代码,则会收到错误消息(我将
Thymeleaf 用作模板处理器):
Error resolving template "/buy/3", template might not exist or might not be accessible by any of the configured Template Resolvers
我可以写类似的东西return "redirect:/buy/" + buyId
,但是在这种情况下,我会丢失所有数据和form对象的错误。
我应该怎么做才能在buyPost
方法中实现与方法相同的行为create
?
我想在这个文件档案化管理解决方案后,在本周末,但它并不适用于BindingResult工作。
下面的代码有效,但并不完美。
@ModelAttribute("command")
public PlaceOrderCommand command() {
return new PlaceOrderCommand();
}
@RequestMapping(value = "/placeOrder", method = RequestMethod.GET)
public String placeOrder(
@ModelAttribute("command") PlaceOrderCommand command,
ModelMap modelMap) {
modelMap.put(BindingResult.MODEL_KEY_PREFIX + "command",
modelMap.get("errors"));
return "placeOrder";
}
@RequestMapping(value = "/placeOrder", method = RequestMethod.POST)
public String placeOrder(
@Valid @ModelAttribute("command") PlaceOrderCommand command,
final BindingResult bindingResult, Model model,
final RedirectAttributes redirectAttributes) {
if (bindingResult.hasErrors()) {
redirectAttributes.addFlashAttribute("errors", bindingResult);
//it doesn't work when passing this
//redirectAttributes.addFlashAttribute(BindingResult.MODEL_KEY_PREFIX + "command", bindingResult);
redirectAttributes.addFlashAttribute("command", command);
return "redirect:/booking/placeOrder";
}
......
}
我有这个代码,但它不起作用(我认为它是th:action=“@{/employee}”,因为它想从我这里输入html href)对不起我的英语)) 如果你知道,帮帮我,pleace))) 4.0.0 org.springframework.boot spring-boot-starter-parent 2.3.2.发布.... 1.8 在此输入图像说明
我正在使用一个旋转控制器来接受整数值。如何限制输入只接受整数?目前,当输入字符串值时,我会在控制台中得到错误消息。我不想让纺纱机接受任何其他类型-例如。双或串。 另外,如何在旋转器上实现错误处理?
我尝试使用spring boot 2.2和thymeleaf进行表单验证,如文档示例所示(https://spring.io/guides/gs/validating-form-input/)但当提交错误的值时,我有一个白色标签错误页面,而我应该在表单视图中绑定这些错误。如何解决这个问题? 我的控制器操作代码: 和视图: 堆栈跟踪是: Whiteblabel错误页面为: 典型情况下,name字段的
@将有效的打印信息发送到控制台,但不发送到页面 当我的字段有错误@Valid print info到控制台时,为什么不在第页? 字段hotelName上的对象formData中的字段错误:拒绝的值[];代码[Size.formData.hotelName, Size.hotelName, Size.java.lang.String, size];参数[org.springframework.con
我想使用@SessionAttributes注释在SpringMVC中的两个控制器之间共享会话属性。 下面是我用来测试属性共享的一个简单代码:AController。JAVA a.jsp BController.java b.jsp 我期望的行为是转到 /aURL,myParam将被设置为0到99之间的随机值,然后该值将在两个控制器之间共享。 但是,会发生以下情况:我转到/a URL,myPara
我知道有一个有效的注释,它指示spring根据JSR-303验证一个控制器参数,在这个示例中: 但我希望能够以某种方式配置Spring,以便在所有控制器中启用验证,而无需明确指定有效的注释。 这有可能吗?一些Spring配置?利用AOP?。。。
问题内容: 下面的代码对于执行基本的必需表单验证似乎效果很好。 当该字段是脏的+无效且为 Great 时,该表单将显示红色的 Name required 消息 。 如果该字段是脏的+有效消息。 但是,对于表单中的每个字段重复此代码是很混乱的: 我希望能够以更简单的方式指定和属性。 问题答案: 一种方法是将验证表达式抽象到范围方法: 控制者
"'. col-md-6%h3美国地址 %form.form-horizontal%标签街道地址%input.form控件{id:'Street',name:'Street',style:'margin bottom:10px;font size:13px;height:30px'} %标签城市%input。表单控件{id:'City',名称:'City',样式:'margin bottom:10