我刚接触Spring,在两天之后遇到了一些错误。希望有人帮助解决问题。我也搜索了错误,但无法理解错误的根本原因。 我还没有研究过春天的注释,所以无法理解大多数在线的例子。
java.lang.IllegalStateException
:bean名称“BindingResult
”的login
和普通目标对象都不可用作请求属性
data.jsp :(这是一个接受用户数据的视图页面)
<form:form commandName="login" method="POST" name="login">
Username:<form:input path="name"/>
<font color="red"><form:errors path="name"/></font><br/><br/>
<input type="submit" value="Login"/>
</form:form>
LoginFormController.java :(处理请求的控制器页面)
package Controller;
import model.Student;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
public class LoginFormController extends SimpleFormController {
public LoginFormController() {
//Initialize controller properties here or
//in the Web Application Context
setCommandClass(Student.class);
setCommandName("login");
setSuccessView("success");
setFormView("data");
}
@Override
protected ModelAndView onSubmit(Object command) throws Exception {
Student s1= (Student)command;
ModelAndView mv = new ModelAndView(getSuccessView());
mv.addObject("message", "Hello! "+s1.getName());
return mv;
}
}
Student.java :( java模型类)
package model;
public class Student {
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Student() {
}
public Student(String name) {
this.name = name;
}
}
dispatcher-servlet.xml :( xml文件说明)
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.htm">indexController</prop>
<prop key="data.htm">dataController</prop>
<prop key="/login.htm">loginDataController</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<bean name="dataController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="data" />
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
<bean name="Student" class="model.Student"/>
<bean id="loginDataController" class="Controller.LoginFormController"/>
任何人都可以提供寻求解决方案的帮助吗?
答案 0 :(得分:0)
<form:form commandName="login" method="POST" name="login">
spring的form标签的现在回到你的案子:
“login”应该可以作为你没有做的模型属性(请求属性),因此你的错误。
因此,将您的onSubmit
方法修改为
@Override
protected ModelAndView onSubmit(Object command) throws Exception {
Student s1= (Student)command;
ModelAndView mv = new ModelAndView(getSuccessView());
mv.addObject("login",s1);
mv.addObject("message", "Hello! "+s1.getName());
return mv;
}
我还建议你在弹簧表格中查看Java Dzone Tutorial。