最近学习spring mvc 老报这个错误,哪位大虾帮忙看看
javax.servlet.jsp.JspTagException: Neither BindingResult nor plain target object for bean name 'users' available as request attribute
at org.springframework.web.servlet.tags.BindTag.doStartTagInternal(BindTag.java:122)
at org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:79)
at org.apache.jsp.index_jsp._jspService(index_jsp.java:95)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:619)
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.3" 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/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_3.xsd"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/controller-servlet.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter </filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>*.do</url-pattern> </filter-mapping> <servlet> <servlet-name>controller</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>controller</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <taglib> <taglib-uri>http://java.sun.com/jsp/jstl/core</taglib-uri> <taglib-location>/WEB-INF/tld/c.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jsp/jstl/fmt</taglib-uri> <taglib-location>/WEB-INF/tld/fmt.tld</taglib-location> </taglib> <taglib> <taglib-uri>/spring</taglib-uri> <taglib-location>/WEB-INF/tld/spring.tld</taglib-location> </taglib> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
controller-servlet.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/SpringMVC/" /> <property name="suffix" value=".jsp" /> </bean> <bean id="usersValidator" class="com.model.UsersValidator" /> <bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/users.do">usersController</prop> </props> </property> </bean> <bean id="springController" class="com.controller.SpringController"> </bean> <bean id="usersController" class="com.controller.UsersController"> <property name="commandClass"> <value>com.model.Users</value> </property> <property name="commandName"> <value>users</value> </property> <property name="successView"> <value>welcome</value> </property> <property name="formView"> <value>index</value> </property> <property name="validator"> <ref bean="usersValidator" /> </property> </bean> </beans>
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib prefix="spring" uri="/spring"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <html> <head> <title>Spring MVC</title> </head> <body> <form name="loginForm" action="users.do" method="POST"> <spring:bind path="users.*"> <c:forEach items="${status.errorMessages}" var="error"> 错误:<c:out value="${error}"></c:out> </c:forEach> </spring:bind> <spring:bind path="users.userName"> 用户名: <input type="text" name="${status.expression}" id="userName" value="<c:out value="${status.value}"/>" />(必须输入) <c:if test="${status.error}"> <c:forEach items="${status.errorMessages}" var="error"> 错误:<c:out value="${error}"></c:out> </c:forEach> </c:if> </spring:bind> <spring:bind path="users.passWord"> 密码: <input type="passWord" name="${status.expression}" id="passWord" value="<c:out value="${status.value}"/>" />(必须输入) <c:if test="${status.error}"> <c:forEach items="${status.errorMessages}" var="error"> 错误:<c:out value="${error}"></c:out> </c:forEach> </c:if> </spring:bind> <spring:hasBindErrors name="users.*"> <b>Please fix all errors!</b> </spring:hasBindErrors> <input type="submit" value="submit" /> </form> </body> </html>
package com.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.validation.BindException; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.SimpleFormController; import com.model.Users; public class UsersController extends SimpleFormController { protected ModelAndView onSubmit(Object users,BindException exception) throws Exception { Users us = (Users)users; if("test".equals(us.getUserName()) &&"test".equals(us.getPassWord())){ return new ModelAndView(this.getSuccessView(),this.getCommandName(),us); }else{ exception.reject("loginfail","用户名或密码错 "); return new ModelAndView(this.getFormView(),exception.getModel()); } } }
package com.model; import org.springframework.validation.Errors; import org.springframework.validation.ValidationUtils; import org.springframework.validation.Validator; public class UsersValidator implements Validator { public boolean supports(Class<?> arg0) { // TODO Auto-generated method stub return arg0.equals(Users.class); } public void validate(Object command, Errors arg1) { //users us=(users)arg0; ValidationUtils.rejectIfEmpty(arg1, "userName", "userName", "用户名不能为空"); ValidationUtils.rejectIfEmpty(arg1, "passWord", "passWord", "密码不能为空"); } }