错误/ BindingResult的说法,预计(Errors/BindingResult argum

2019-10-18 14:03发布

我学习Spring MVC和我试图验证表单。 当用户搜索一个空字符串,它会显示错误。 当我执行我的代码,我得到以下错误

一个错误/ BindingResult参数预计模型在控制器方法签名属性参数之后立即:公共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)抛出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);
    }

}

Answer 1:

所述BindingResult参数只能在一起选择使用具有@ModelAttribute注解的方法。 如果没有模型属性也将是没有约束力的,因此没有BindingResult实例将是可用的。 所以你的情况,你必须删除BindingResult说法,因为它永远不会在你的情况下工作,由于缺乏一个模型属性的。

模型属性一样的Model (如被暗示上)。 该模型的属性是部分Model



Answer 2:

如果q是可选的修改@RequestParam(value = "q") Q (@RequestParam(required = false) String q

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


文章来源: Errors/BindingResult argument is expected