问题描述
出于某种原因,即使查看了此处的多个示例,我似乎也无法解决此问题.
For some reason I cannot seem to fix this issue, even after looking at multiple examples on here.
我正在尝试使用 Spring 3 MVC 处理表单,但出现以下错误:
I am trying to handle a form using Spring 3 MVC, but I am getting the following error:
Neither BindingResult nor plain target object for bean name 'reverseString' available as request attribute
所以我有点明白这里的问题是什么,但我没能解决它.这是我的方法
So I am kind of getting what is the problem here, but I failed to solve it. Here is the method in my
ReverseController 类,它扩展了 SimpleFormController:
ReverseController class which extends SimpleFormController:
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;
}
我的 app-servlet.xml 定义控制器如下:
My app-servlet.xml defines the controller as follows:
<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>
这是我的reverse.jsp中的表格:
This is the form in my reverse.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 中定义的内容:
And this is what is being defined in my 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 对象:
And finally, this is my ReverseString object:
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);
}
}
推荐答案
在您的 reverse.jsp 文件中,您将 modelAttribute 添加为 reverseString.因此,在加载 reverse.jsp 之前,它会查找您的 ReverseString.java 对象.
In your reverse.jsp file, you added modelAttribute as reverseString.So while before loading reverse.jsp, it will look for your ReverseString.java object.
您只需要在您的控制器方法的请求范围内添加 ReverseString.java 对象,从那里您将用户导航到 reverse.jsp.
You just need to add ReverseString.java object in request scope of your controller method from where you navigate user to reverse.jsp.
例如
@RequestMapping(value = "/getReverseStringForm")
public String cmLogin(Model model) {
model.addAttribute(new ReverseString());
return "reverse";
}
这篇关于Spring MVC 既不是 BindingResult 也不是 bean 名称的普通目标对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!