错误/ BindingResult参数是预期的

时间:2013-09-02 07:26:26

标签: java spring-mvc

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

  

错误/ BindingResult参数应该紧跟在控制器方法签名中的模型属性参数之后: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)抛出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);
    }

}

2 个答案:

答案 0 :(得分:2)

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

模型属性 NOT Model相同(如提示)。 model属性是Model

的一部分

答案 1 :(得分:0)

如果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);
 }