由於某種原因,我似乎無法解決此問題,即使在查看此處的多個示例之後。Spring MVC無論是BindingResult還是bean名稱的普通目標對象
我試圖處理使用Spring 3 MVC形式,但我收到以下錯誤:
Neither BindingResult nor plain target object for bean name 'reverseString' available as request attribute
所以,我有種越來越這裏有什麼問題,但我沒能解決這個問題。這裏是延伸SimpleFormController在我
ReverseController類中的方法:
protected ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors)
throws Exception {
ReverseString revString = (ReverseString) command;
ModelAndView mav = new ModelAndView();
ModelMap map = new ModelMap();
map.addAttribute("reverseString", revString);
mav.addAllObjects(map);
return mav;
}
我的應用-servlet.xml中定義了控制器如下:
<bean name="/reverse.app" class="ReverseController">
<property name="commandName" value="reverseString"></property>
<property name="commandClass" value="ReverseString" />
<property name="successView" value="reverseResult" />
<property name="formView" value="reverse" />
</bean>
這是在我的反向形式。 jsp:
<form:form method="POST" modelAttribute="reverseString" action="handleForm">
<form:errors path="*" cssClass="errorblock" element="div" />
<table>
<tr>
<td>String to be reversed:</td>
<td><form:input path="stringToReverse" />
</td>
<td><form:errors path="stringToReverse" cssClass="error" />
</td>
</tr>
<tr>
<td colspan="3"><input type="submit" /></td>
</tr>
</table>
</form:form>
這就是我的web.xml中定義的內容:
<web-app>
<display-name>Spring Reverse String</display-name>
<welcome-file-list>
<welcome-file>reverse.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>app</servlet-name>
<servlet-class>Dispatcher</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>app</servlet-name>
<url-pattern>*.app</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/app-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
最後,這是我的ReverseString對象:
public class ReverseString {
public String stringToReverse;
public String reversedString;
public ReverseString(String rawString) {
this.reversedString = reverseString(stringToReverse);
}
public String getStringToReverse() {
return stringToReverse;
}
public void setStringToReverse(String stringToReverse) {
this.stringToReverse = stringToReverse;
}
public String reverseString(String rawString) {
if(rawString == null || rawString.length() <= 1) return rawString;
return reverseString(rawString.substring(1)) + rawString.charAt(0);
}
}
您是否混合了Spring MVC 2.x和3.x概念? - 請發佈完整的控制器類。 – Ralph