【问题标题】:Spring SimpleFormController - Including Search Form In Success ViewSpring SimpleFormController - 在成功视图中包含搜索表单
【发布时间】:2009-11-03 00:24:17
【问题描述】:

2010 年 1 月 31 日更新:由于该线程继续获得大量浏览量...我很好奇它最近是否对任何人有所帮助?请随时留下 cmets/反馈,谢谢。


我有一个 Spring 表单,我想在其中重用搜索页面以将结果包含在搜索表单下。目前,当我这样做时,我在加载成功视图时收到以下错误:

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

这是我的 bean 配置:

<bean name="/search.html" class="myapp.web.AccountSearchController">
        <property name="sessionForm" value="true"/>
    <property name="commandName" value="searchAccounts"/>
    <property name="commandClass" value="myapp.service.AccountSearch"/>
    <property name="validator">
        <bean class="myapp.service.AccountSearchValidator"/>
    </property>
    <property name="formView" value="accountSearch"/>
    <property name="successView" value="accountSearchResults"/>
</bean>

这是包含搜索表单的 JSP 的 sn-p:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

<form:form method="post" commandName="searchAccounts">

<table valign="top" cellspacing="0" cellpadding="0" width="500" border="0">

    <tr>
        <td valign="top">

        <div class="border-title">Account Search</div>

        <div id="navhome">
        <div class="border">
        <div id="sidebarhome">


        <table id="form">
            <tr>
                <td colspan="2">Search by Account ID or Domain Name. If
                values are provided for both, only accounts matching both values
                will be returned.</td>
            </tr>

            <tr>
                <td colspan="2">&nbsp;</td>
            </tr>

            <tr>
                <td align="right" valign="top"><form:label path="accountId">Account ID</form:label>:</td>
                <td><form:input path="accountId" size="30"/></td>
            </tr>
            <c:set var="accountIdErrors"><form:errors path="accountId"/></c:set>
            <c:if test="${not empty accountIdErrors}">
            <tr>
                 <td>&nbsp;</td>
                 <td>${accountIdErrors}</td>
            </tr>
            </c:if>
            <tr>
                <td align="right" valign="top"><form:label path="domainName">Domain Name</form:label>:</td>
                <td><form:input path="domainName" size="30"/></td>
            </tr>
            <c:set var="domainNameErrors"><form:errors path="domainName"/></c:set>
            <c:if test="${not empty domainNameErrors}">
            <tr>
                 <td>&nbsp;</td>
                 <td>${domainNameErrors}</td>
            </tr>
            </c:if>
            <tr>
                <td colspan="2">&nbsp;</td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td><input type="submit" name="submit" value="Search">
                </td>
            </tr>
        </table>

        </div>
        </div>
        </div>

        </td>
    </tr>
</table>
</form:form>

而且...这是我的表单控制器类(减去导入):

    public class AccountSearchController  extends SimpleFormController {

    protected final Log logger = LogFactory.getLog(getClass());

    public ModelAndView onSubmit(Object command, BindException errors) throws ServletException {
        String accountId = ((AccountSearch) command).getAccountId();
        String domainName = ((AccountSearch) command).getDomainName();

        logger.info("User provided search criteria...\n\tDomain Name: " + domainName + "\n\tAccountId: " + accountId);

        //TODO do search

        logger.info("returning from AccountSearch form view to " + getSuccessView());

        return new ModelAndView(getSuccessView());
    }

    protected Object formBackingObject(HttpServletRequest request) throws ServletException {
        AccountSearch accountSearch = new AccountSearch();
        return accountSearch;
    }
}

提前感谢您的帮助!

-aj

更新:

我根据下面的答案将此移植到带注释的控制器。这是新的/工作代码:

@Controller
@RequestMapping("/search.html")
public class AccountSearchController {

    // note: this method does not have to be called setupForm
    @RequestMapping(method = RequestMethod.GET)
    public String setupForm(Model model) {
        AccountSearchCriteria accountSearchCriteria = new AccountSearchCriteria();
        model.addAttribute("accountSearchCriteria", accountSearchCriteria);
        model.addAttribute("title", "Account Search");
        return "accountSearch";
    }

    // note: this method does not have to be called onSubmit
    @RequestMapping(method = RequestMethod.POST)
    public String onSubmit(@ModelAttribute("accountSearchCriteria") AccountSearchCriteria accountSearchCriteria, BindingResult result, SessionStatus status, Model model) {
        new AccountSearchValidator().validate(accountSearchCriteria, result);
        if (result.hasErrors()) {
            return "accountSearch";

        } else {
            ArrayList<AccountSearchCriteria> accountSearchResults = new ArrayList<AccountSearchCriteria>();

            AccountSearchCriteria rec = new AccountSearchCriteria();
            rec.setDomainName("ajcoon.com");
            accountSearchResults.add(rec);

            AccountSearchCriteria rec2 = new AccountSearchCriteria();
            rec2.setDomainName("ajcoon2.com");
            accountSearchResults.add(rec2);

            //TODO do search
            //ArrayList<HashMap<String,String>> accountSearchResults = new AccountSearchService().search(accountId,domainName);

            if( accountSearchResults.size() < 1 ){
                result.rejectValue("domainName", "error.accountSearch.noMatchesFound", "No matching records were found.");
                return "accountSearch";

            } else if(accountSearchResults.size() > 1){
                model.addAttribute("accountSearchResults", accountSearchResults);
                return "accountSearch";

            } else {
                status.setComplete();
                return "redirect:viewAccount?accountId=";
                //return "redirect:viewAccount?accountId=" + accountSearchResults.get(0).getAccountId();
            }
        }
    }   
}

【问题讨论】:

  • 您好,我正在尝试使用 JSP 中的
    标记在同一搜索表单页面上显示结果的搜索表单。关于使用这些标签,您是否完全更改了您的 JSP?我可能误读了您的标题,因为注意到您的 successView 与您的 formView 不同。我目前正在使用 SimpleFormController 并考虑使用带注释的方法。在网上找到任何例子都是一个挑战。不知道它会如何抑制我,因为我也在使用 referenceData 方法。任何帮助,将不胜感激。谢谢!

标签: java spring jsp spring-mvc jstl


【解决方案1】:

尝试使用(抛出异常而不是..)

protected Object formBackingObject(HttpServletRequest request)
                            throws Exception {
        AccountSearch accountSearch = new AccountSearch();
        System.out.println("inside formBackingObject");
        return accountSearch;
}

您的 formBackingObject 方法似乎没有执行。重新运行上述更改的代码并查看日志控制台以查看该方法是否已执行。

--

您应该使用注解而不是扩展控制器。 Spring 3.0 将弃用控制器层次结构。

【讨论】:

  • 感谢 surajz ...我继续将其移植到带注释的控制器并设法解决了问题。我会将新控制器发布到编辑中。 +1 的建议!
猜你喜欢
  • 1970-01-01
  • 2010-10-20
  • 2014-02-02
  • 2023-01-25
  • 2021-08-11
  • 1970-01-01
  • 1970-01-01
  • 2012-09-04
相关资源
最近更新 更多