Spring入门之spring:bind

最近在学习spring,按照一个spring的例子,结果出错拉。错误是:
Neither BindingResult nor plain target object for bean name 'command' available as request attribute

使用的是jdk1.5 + tomcat5.5 +Spring2.1

下面说说我的代码:
spring.login.LoginAction.java
spring.login.LoginForm.java
web.xml
login-serlvet.xml
form.jsp
success.jsp
这些是主要的文件,部署的话是普通的web部署,当然,jsp文件在WEB-INF/jsp/内。
1、 LoginAction.java:
package spring.login;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class LoginAction extends SimpleFormController ... {

protectedModelAndViewonSubmit(Objectcommand,BindExceptionerrors)
throwsException...{
LoginFormform
=(LoginForm)command;
if("madic".equals(form.getUsername())
&&"123".equals(form.getPassword()))...{
returnnewModelAndView(this.getSuccessView(),"user",form.getUsername());
}
else...{
errors.reject(
"loginfail","用户名或密码错");
returnnewModelAndView(this.getFormView(),errors.getModel());
}


}


}

LoginForm.java:
package spring.login;

public class LoginForm ... {
privateStringusername;
privateStringpassword;
publicStringgetUsername()...{
returnusername;
}

publicvoidsetUsername(Stringusername)...{
this.username=username;
}

publicStringgetPassword()...{
returnpassword;
}

publicvoidsetPassword(Stringpassword)...{
this.password=password;
}

}


loginValidator.java

package spring.login;

import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

public class LoginValidator implements Validator ... {
publicbooleansupports(Classclazz)...{
returnclazz.equals(LoginForm.class);
}


publicvoidvalidate(Objectobj,Errorserrors)...{
LoginFormform
=(LoginForm)obj;
if(form.getPassword().length()<2)...{
errors.rejectValue(
"password","less-than-two",null,"密码不得小于2个字符");
}

}

}

web.xml
<? xmlversion="1.0"encoding="UTF-8" ?>
< web-app id ="WebApp_ID" version ="2.4" xmlns ="http://java.sun.com/xml/ns/j2ee" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
< display-name >
login
</ display-name >
< description >
SpringLoginExamples.
</ description >
< display-name > SpringLogin2Examples </ display-name >

<!-- <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
-->


< context-param >
< param-name > contextConfigLocation </ param-name >
< param-value > WEB-INF/login-servlet.xml </ param-value >
</ context-param >

< servlet >
< servlet-name > login </ servlet-name >
< servlet-class > org.springframework.web.servlet.DispatcherServlet </ servlet-class >
< init-param >
< param-name > contextConfigLocation </ param-name >
< param-value > /WEB-INF/login-servlet.xml </ param-value >
</ init-param >
</ servlet >

< servlet-mapping >
< servlet-name > login </ servlet-name >
< url-pattern > *.htm </ url-pattern >
</ servlet-mapping >

< taglib >
< taglib-uri > /spring </ taglib-uri >
< taglib-location > /WEB-INF/spring.tld </ taglib-location >
</ taglib >
</ web-app >

login-servlet.xml
<? xmlversion="1.0"encoding="UTF-8" ?>
<! DOCTYPEbeansPUBLIC"-//SPRING/DTDBEAN/EN""http://www.springframework.org/dtd/spring-beans.dtd" >
< beans >
< bean id ="loginValidator" class ="spring.login.LoginValidator" />

< bean id ="urlMapping" class ="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" >
< property name ="mappings" >
< props >
< prop key ="login.htm" > loginAction </ prop >
</ props >
</ property >

</ bean >

< bean id ="viewResolver" class ="org.springframework.web.servlet.view.InternalResourceViewResolver" >
< property name ="viewClass" >
< value > org.springframework.web.servlet.view.InternalResourceView </ value >
</ property >

< property name ="prefix" >
< value > /WEB-INF/jsp/ </ value >
</ property >

< property name ="suffix" >
< value > .jsp </ value >
</ property >
</ bean >

< bean id ="loginAction" class ="spring.login.LoginAction" >
< property name ="sessionForm" >
< value > true </ value >
</ property >
< property name ="commandClass" >
< value > spring.login.LoginForm </ value >
</ property >
< property name ="successView" >
< value > success </ value >
</ property >
< property name ="formView" >
< value > form </ value >
</ property >
< property name ="commandName" >
< value > loginForm </ value >
</ property >

< property name ="validator" >
< ref bean ="loginValidator" />
</ property >
</ bean >


</ beans >


form.jsp
<% ... @tagliburi="/spring"prefix="spring" %>
<% ... @pagelanguage="java"contentType="text/html;charset=UTF-8"
pageEncoding
="UTF-8"
%>
< html >
< head >< title > Login </ title ></ head >
< body >

< spring:bind path ="command.*" >
< font color ="red" >< b > ${status.errorMessage} </ b ></ font >< br >
</ spring:bind >

请输入使用者
的名称与密码:
< p >
< form name ="loginForm" action ="login.htm" method ="post" >

< spring:bind path ="command.username" >
名称
< br >
< input type ="text" name ="${status.expression}" value ="${status.value}" />
</ spring:bind >
< br >
< spring:bind path ="command.password" >
密码
< br >
< input type ="password" name ="${status.expression}" value ="${status.value}" />< br >
</ spring:bind >

< input type ="submit" value ="确定" />

</ form >
</ body >
</ html >


success.jsp就不用说了。很简单。
大家看到什么错误了没有?

< property name ="commandName" >
< value > loginForm </ value >
</ property >
看看这里,这个commandName就是在login-servlet.xml里的 controller bean loginAction的属性之一。
我们要改为: command
这样在<Spring:bind path="command.*">就不会找不到了。

此外,这里还要注意:

<input>的name属性值必须与<spring:bind>的path属性的匹配,否则就绑定不了!

例如下面的代码就绑定不了

<spring:bind path="user.age">
<input type="text" name="theAge" value="${status.value}">
<font color="red">${status.errorMessage}</font>
</spring:bind>

为了避免手误,强烈推荐下列方法来绑定:

<spring:bind path="user.age">
<input type="text" name="${status.expression}" value="${status.value}">
<font color="red">${status.errorMessage}</font>
</spring:bind>

这个倒是没有注意,看来只有错误,才能更深理解阿。

你可能感兴趣的:(Spring入门)