gpt4 book ai didi

java - 需要 Errors/BindingResult 参数

转载 作者:行者123 更新时间:2023-11-30 04:15:12 24 4
gpt4 key购买 nike

我正在学习 Spring MVC,我正在尝试验证表单。当用户搜索空白字符串时,它会显示错误。执行代码时出现以下错误

An Errors/BindingResult argument is expected to be immediately after the model attribute argument in the controller method signature: public java.lang.String com.mycompany.controller.catalog.SearchController.search(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse,java.lang.String,org.springframework.validation.BindingResult,org.springframework.ui.Model) throws javax.servlet.ServletException,java.io.IOException,org.broadleafcommerce.common.exception.ServiceException

package com.mycompany.controller.catalog;

import org.broadleafcommerce.common.exception.ServiceException;

import org.broadleafcommerce.core.web.controller.catalog.Mysearchcontroller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.method.support.ModelAndViewContainer;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;

@Controller
@RequestMapping("/search")
public class SearchController extends Mysearchcontroller {


@RequestMapping("")
public String search(HttpServletRequest request, HttpServletResponse response,
@RequestParam(value = "q") String q,BindingResult errors,Model model) throws ServletException, IOException, ServiceException {
return search3(model, request,response, q ,errors);
}

}

最佳答案

BindingResult 参数只能与 @ModelAttribute 带注释的方法结合使用。如果没有模型属性,就没有绑定(bind),因此没有 BindingResult 实例可用。因此,在您的情况下,您必须删除 BindingResult 参数,因为由于缺少模型属性,它永远不会在您的情况下工作。

模型属性模型相同(正如所暗示的那样)。 model 属性是 Model 的一部分。

关于java - 需要 Errors/BindingResult 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18567706/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com