0

这是我的控制器

@Controller
@RequestMapping("VIEW")
public class SearchController {

    private static final Log LOGGER = LogFactoryUtil.getLog(SearchController.class);

    @RenderMapping
    public String render() {

        return "view";
    }

    @ActionMapping(params = "action = getResults")
    public void getResults(@ModelAttribute("search") Search search, ActionRequest actionRequest,    ActionResponse actionResponse) {
        String keyword = search.getKeyword();
        LOGGER.info("Keyword: " + keyword);
    }

}

还有我的豆子,

public class Search {

    private String keyword;

    public String getKeyword() {
        return keyword;
    }

    public void setKeyword(String keyword) {
        this.keyword = keyword;
    }

}

和我的 view.jsp

<%@page import="org.springframework.web.bind.annotation.RequestMethod"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<portlet:defineObjects />

<portlet:actionURL var = "getResultsURL">
    <portlet:param name="action" value="getResults"/>
</portlet:actionURL>

<form:form action="${getResultsURL}" commandName="search" method="POST">
    <form:input path="keyword"/>
    <input type="submit" value="Search">
</form:form>

我收到以下异常

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name   'search' available as request attribute

如果我将 @ModelAttribute("search") 作为参数放在渲染方法中,它工作正常,但我知道这是绝对错误的(纠正我)

有什么建议么?

4

1 回答 1

0

当 JSP 页面被渲染时,你会得到这个异常,对吗?

Spring MVC 告诉您它在当前请求中找不到“搜索”属性。实际上,您的控制器不会将任何 Search 类实例放入 Spring MVC 模型中。

两种选择:

  1. 使用 @ModelAttribute 注释为 Search 类实例创建 getter:

    @ModelAttribute public Search getSearch() { return new Search(); }

  2. 在 render 方法中将 Search 类实例放到 Spring 模型中:

    @RenderMapping public String render(Model model) { model.addAttribute("search", new Search()); 返回“视图”;}

这样,表单标签将在给定的命令名称下找到模型。

于 2014-12-15T14:27:35.827 回答