我正在尝试验证一些信息,所以我添加了一个验证器并在post方法的参数中使用了@Valid:
@Controller
@RequestMapping("/user.htm")
public class UserController {
@Autowired
private IUserService userService;
@RequestMapping(method = RequestMethod.GET)
public String userInfo(Model model) {
....
return "user";
}
@RequestMapping(method = RequestMethod.POST)
public String userInfoResult(@Valid @ModelAttribute UserForm userForm, BindingResult result, Model model ) {
UserInfo stat = userService.getStatitisque(userForm.getSearchCritera());
userForm.setListeExpediteur(listeExpediteur);
userForm.setUserInfo(stat);
model.addAttribute("userForm", userForm);
}
}
public class UserFormValidator implements Validator {
@Override
public boolean supports(Class<?> type) {
return UserForm.class.equals(type);
}
@Override
public void validate(Object o, Errors errors) {
UserForm userForm = (User) o;
...
}
}
当我调试时,我从未进入UserFormValidator类。
我是否需要在这些文件中添加内容?
web.xml
applicationContext.xml
dispatcher-servlet.xml
答案 0 :(得分:5)
您需要使用@InitBinder
方法添加验证程序:
@InitBinder(value="YourFormObjectName")
protected void initBinder(WebDataBinder binder) {
binder.setValidator(new FooValidator());
}
或通过XML全局:
<mvc:annotation-driven validator="globalValidator"/>
<强>参考:强>
答案 1 :(得分:0)
5.7.4.3配置一个JSR-303验证器供Spring MVC使用
使用JSR-303,单个javax.validation.Validator实例通常验证声明验证约束的所有模型对象。要使用Spring MVC配置支持JSR-303的Validator,只需将一个JSR-303提供程序(如Hibernate Validator)添加到类路径。 Spring MVC将检测它并自动为所有控制器启用JSR-303支持。
<mvc:annotation-driven/>
使用此最小配置,无论何时遇到@Valid @Controller输入,它都将由JSR-303提供程序验证。反过来,JSR-303将强制执行针对输入声明的任何约束。任何ConstraintViolations都将自动暴露为标准Spring MVC表单标签可渲染的BindingResult中的错误。
答案 2 :(得分:0)
@Controller
@RequestMapping("/user.htm")
public class UserController {
@Autowired
private IUserService userService;
@RequestMapping(method = RequestMethod.GET)
public String userInfo(Model model) {
....
return "user";
}
@RequestMapping(method = RequestMethod.POST)
public String userInfoResult(Model model,
@Valid @ModelAttribute UserForm userForm,
BindingResult result) {
/*
add custom validation check to standard validation error
(if not registry UserFormValidator in @InitBinder block)
*/
new UserFormValidator().validate(userForm, error);
// check all validation errors
if (errors.hasErrors()) {
// go back
return userInfo(model);
}
...
}
}
答案 3 :(得分:0)
您需要在@InitBinder方法中添加验证器
library(shiny)
library(shinydashboardPlus)
library(shinyWidgets)
css <- "
.inlinecheckbox .shiny-input-container {
display: inline-block;
width: auto;
}
"
ui <- fluidPage(
tags$style(css),
titlePanel("Dynamic to do list"),
useShinydashboardPlus(),
sidebarLayout(
sidebarPanel(),
mainPanel(
box(
"Sortable todo list demo",
status = "warning",
uiOutput("myToDoList")
)
)
)
)
checkboxIDs <- paste0("checkbox", seq_len(nrow(mtcars)))
mtcars$checked <- FALSE
# Define server logic required to draw a histogram
server <- function(input, output) {
reactiveMtcars <- reactiveVal(mtcars)
observe({
for (i in seq_along(checkboxIDs)) {
if(!is.null(input[[checkboxIDs[1]]])){
mtcars$checked[i] <- input[[checkboxIDs[i]]]
}
}
reactiveMtcars(mtcars)
})
output$myToDoList <- renderUI({
req(reactiveMtcars())
todoListItems <- list()
for(i in seq_len(nrow(reactiveMtcars()))){
todoListItems[[i]] <- todoListItem(
label = div(rownames(reactiveMtcars())[i], style = ""),
span(class = "inlinecheckbox", checkboxInput(inputId = paste0("checkbox", i), label = NULL, value = reactiveMtcars()$checked[i])),
checked = reactiveMtcars()$checked[i],
)
}
todoList(todoListItems)
})
}
shinyApp(ui = ui, server = server)