2015-10-05 85 views
0

//類型異常報告彈簧3.6.4 +形式綁定錯誤

消息java.lang.IllegalStateException:既不BindingResult也不對bean名稱「測試」可作爲請求屬性

描述普通目標對象服務器遇到一個內部錯誤,阻止它履行這個請求。

HomeController.java 


import org.springframework.ui.Model; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 

/** Handles requests for the application home page. */ 
@Controller 
public class HomeController { 

    private static final Logger logger = LoggerFactory.getLogger(HomeController.class); 

    /** Simply selects the home view to render by returning its name. */ 
    @RequestMapping(value = "/", method = RequestMethod.GET) 
    public String home(Model model, Locale locale) { 
     logger.info("Welcome home! The client locale is {}.", locale); 

     Date date = new Date(); 
     DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale); 

     String formattedDate = dateFormat.format(date); 

     model.addAttribute("serverTime", formattedDate); 

     return "home"; 
    } 

} 

HomeModel.java 
package com.test.app; 

public class HomeModel { 

    private String name; 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

} 

HometController.java 
package com.test.app; 

import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.validation.BindingResult; 
import org.springframework.web.bind.annotation.ModelAttribute; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 

@Controller 
public class HometController { 

    @RequestMapping(value = "/try", method = RequestMethod.POST) 
    public String trynew(@ModelAttribute("test") HomeModel hm, BindingResult br, Model model) { 
     model.addAttribute("test", new HomeModel()); 
     System.out.println("in try"); 
     return "tst"; 
    } 

} 

home.jsp 

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %> 
<%@ page session="false" %> 
<html> 
<head> 
    <title>Home</title> 
</head> 
<body> 
<h1> 
    Hello world! 
</h1> 

<P> The time on the server is ${serverTime}. </P> 
</body> 
<form:form method="POST" modelAttribute="test" name="test" action="try"> 

<form:input path="name"/> 

<input type="submit" value="test"/> 
</form:form> 
<!-- onClick="document.test.submit()" --> 
</html> 

tst.jsp 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
     <title>Hello World with Spring 3 MVC</title> 
     <meta http-equiv="Content-Type" content="text/html; charset=windows-1251"> 
    </head> 
    <body> 
     <h1>Registration Form</h1><br /> 
     <form:form commandName="USER"> 
     <table> 
     <form:errors path="*" cssStyle="color : red;"/> 

      <tr><td>Name : </td><td><form:input path="name" /></td></tr> 

      <tr><td colspan="2"><input type="submit" value="Save Changes" /></td></tr> 
     </table> 
     </form:form> 
    </body> 
</html> 


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

請仔細閱讀[MCVE]來看看如何寫代碼,不工作一個清晰的問題。 – AdrianHHH

回答

0

刪除此參數@ModelAttribute("test") HomeModel hm擺脫你的異常

@Controller 
    public class HometController { 

     @RequestMapping(value = "/try", method = RequestMethod.POST) 
     public String trynew(BindingResult br, Model model) { 
      model.addAttribute("test", new HomeModel()); 
      System.out.println("in try"); 
      return "tst"; 
     } 

    }