spring simpleformcontroller-在success视图中包含搜索表单

3j86kqsm  于 2021-07-05  发布在  Java
关注(0)|答案(1)|浏览(304)

更新1/31/10:由于这个帖子继续得到很多的意见…我很好奇,如果它有帮助的人最近?欢迎留言/反馈,谢谢。
我有一个spring表单,我想在其中重用搜索页面以将结果包含在搜索表单下。当前执行此操作时,加载成功视图时出现以下错误:

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

下面是我的bean配置:

  1. <bean name="/search.html" class="myapp.web.AccountSearchController">
  2. <property name="sessionForm" value="true"/>
  3. <property name="commandName" value="searchAccounts"/>
  4. <property name="commandClass" value="myapp.service.AccountSearch"/>
  5. <property name="validator">
  6. <bean class="myapp.service.AccountSearchValidator"/>
  7. </property>
  8. <property name="formView" value="accountSearch"/>
  9. <property name="successView" value="accountSearchResults"/>
  10. </bean>

以下是包含搜索表单的jsp片段:

  1. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  2. <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
  3. <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
  4. <form:form method="post" commandName="searchAccounts">
  5. <table valign="top" cellspacing="0" cellpadding="0" width="500" border="0">
  6. <tr>
  7. <td valign="top">
  8. <div class="border-title">Account Search</div>
  9. <div id="navhome">
  10. <div class="border">
  11. <div id="sidebarhome">
  12. <table id="form">
  13. <tr>
  14. <td colspan="2">Search by Account ID or Domain Name. If
  15. values are provided for both, only accounts matching both values
  16. will be returned.</td>
  17. </tr>
  18. <tr>
  19. <td colspan="2">&nbsp;</td>
  20. </tr>
  21. <tr>
  22. <td align="right" valign="top"><form:label path="accountId">Account ID</form:label>:</td>
  23. <td><form:input path="accountId" size="30"/></td>
  24. </tr>
  25. <c:set var="accountIdErrors"><form:errors path="accountId"/></c:set>
  26. <c:if test="${not empty accountIdErrors}">
  27. <tr>
  28. <td>&nbsp;</td>
  29. <td>${accountIdErrors}</td>
  30. </tr>
  31. </c:if>
  32. <tr>
  33. <td align="right" valign="top"><form:label path="domainName">Domain Name</form:label>:</td>
  34. <td><form:input path="domainName" size="30"/></td>
  35. </tr>
  36. <c:set var="domainNameErrors"><form:errors path="domainName"/></c:set>
  37. <c:if test="${not empty domainNameErrors}">
  38. <tr>
  39. <td>&nbsp;</td>
  40. <td>${domainNameErrors}</td>
  41. </tr>
  42. </c:if>
  43. <tr>
  44. <td colspan="2">&nbsp;</td>
  45. </tr>
  46. <tr>
  47. <td>&nbsp;</td>
  48. <td><input type="submit" name="submit" value="Search">
  49. </td>
  50. </tr>
  51. </table>
  52. </div>
  53. </div>
  54. </div>
  55. </td>
  56. </tr>
  57. </table>
  58. </form:form>

还有…这是我的窗体控制器类(减去导入):

  1. public class AccountSearchController extends SimpleFormController {
  2. protected final Log logger = LogFactory.getLog(getClass());
  3. public ModelAndView onSubmit(Object command, BindException errors) throws ServletException {
  4. String accountId = ((AccountSearch) command).getAccountId();
  5. String domainName = ((AccountSearch) command).getDomainName();
  6. logger.info("User provided search criteria...\n\tDomain Name: " + domainName + "\n\tAccountId: " + accountId);
  7. //TODO do search
  8. logger.info("returning from AccountSearch form view to " + getSuccessView());
  9. return new ModelAndView(getSuccessView());
  10. }
  11. protected Object formBackingObject(HttpServletRequest request) throws ServletException {
  12. AccountSearch accountSearch = new AccountSearch();
  13. return accountSearch;
  14. }
  15. }

提前感谢您的帮助!
-阿杰
更新:
根据下面的答案,我将其移植到一个带注解的控制器。以下是新代码/工作代码:

  1. @Controller
  2. @RequestMapping("/search.html")
  3. public class AccountSearchController {
  4. // note: this method does not have to be called setupForm
  5. @RequestMapping(method = RequestMethod.GET)
  6. public String setupForm(Model model) {
  7. AccountSearchCriteria accountSearchCriteria = new AccountSearchCriteria();
  8. model.addAttribute("accountSearchCriteria", accountSearchCriteria);
  9. model.addAttribute("title", "Account Search");
  10. return "accountSearch";
  11. }
  12. // note: this method does not have to be called onSubmit
  13. @RequestMapping(method = RequestMethod.POST)
  14. public String onSubmit(@ModelAttribute("accountSearchCriteria") AccountSearchCriteria accountSearchCriteria, BindingResult result, SessionStatus status, Model model) {
  15. new AccountSearchValidator().validate(accountSearchCriteria, result);
  16. if (result.hasErrors()) {
  17. return "accountSearch";
  18. } else {
  19. ArrayList<AccountSearchCriteria> accountSearchResults = new ArrayList<AccountSearchCriteria>();
  20. AccountSearchCriteria rec = new AccountSearchCriteria();
  21. rec.setDomainName("ajcoon.com");
  22. accountSearchResults.add(rec);
  23. AccountSearchCriteria rec2 = new AccountSearchCriteria();
  24. rec2.setDomainName("ajcoon2.com");
  25. accountSearchResults.add(rec2);
  26. //TODO do search
  27. //ArrayList<HashMap<String,String>> accountSearchResults = new AccountSearchService().search(accountId,domainName);
  28. if( accountSearchResults.size() < 1 ){
  29. result.rejectValue("domainName", "error.accountSearch.noMatchesFound", "No matching records were found.");
  30. return "accountSearch";
  31. } else if(accountSearchResults.size() > 1){
  32. model.addAttribute("accountSearchResults", accountSearchResults);
  33. return "accountSearch";
  34. } else {
  35. status.setComplete();
  36. return "redirect:viewAccount?accountId=";
  37. //return "redirect:viewAccount?accountId=" + accountSearchResults.get(0).getAccountId();
  38. }
  39. }
  40. }
  41. }
9njqaruj

9njqaruj1#

尝试使用(引发异常而不是..)

  1. protected Object formBackingObject(HttpServletRequest request)
  2. throws Exception {
  3. AccountSearch accountSearch = new AccountSearch();
  4. System.out.println("inside formBackingObject");
  5. return accountSearch;
  6. }

似乎没有执行formbackingobject方法。使用上述更改重新运行代码,并查看日志控制台以查看是否执行了该方法。

您应该使用注解而不是扩展控制器。Spring3.0将否决控制器层次结构。

相关问题