在springmvc中commons validator验证框架

wuyuehang 发布于 2015/05/29 16:32
阅读 1K+
收藏 1

现在我已经配置好了但是一直说<spring:bind path="user.userName">标签里面的user找不到

这是控制器代码

package com.kjt;




import javax.validation.Valid;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;


import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Validator;




import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
public class LoginController {





@Autowired(required=false)
Validator validator;


@RequestMapping(value="/login1.do")
public String login1(@Valid @ModelAttribute("user")User user,BindingResult bindingResult,ModelMap modelMap){
return"index";

}
}


pojo类

package com.kjt;


public class User {
private String userName;
private String password;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}

}


这是validation.xml校验

<form-validation>

 <formset>
       
        <form name="user">
        <field property="userName" depends="required"><!--depands 字段是配置需要满足的验证器,可配置多个-->
        <!--
arg0是该字段的显示名,用于验证失败时显示提示信息

key是定义在messageResource中的消息的key
-->
<arg0 key="用户名" />
        </field>
        <field property="password" depends="maxlength,required">
        <arg0 key="密码" />
        <arg1 name="maxlength" key="${var:maxlength}" resource="false" />
        <var>
        <var-name>maxlength</var-name>
        <var-value>10</var-value>
        </var>
        </field>
        </form>
  </formset>
</form-validation>

还有一个validator-rules.xml文件  这里面的代码不是自己写的不用管


这是web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
 
 
 
 
  <display-name>commonvalidators</display-name>
  
  
  
  <listener> 
<listener-class> 
org.springframework.web.context.ContextLoaderListener 
</listener-class> 
</listener> 
  <context-param>
<param-name>webAppRootKey</param-name>
<param-value>app.springmodules</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<!--init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-servlet.xml</param-value>
</init-param-->
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>/WEB-INF/jsp/login.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

这是applicationContext.xml代码

<?xml version="1.0" encoding="UTF-8"?>


<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages" />
</bean>

<bean id="validatorFactory" class="org.springmodules.validation.commons.DefaultValidatorFactory">
<property name="validationConfigLocations">
<list>
<value>WEB-INF/validator-rules.xml</value>
<value>WEB-INF/validation.xml</value>
</list>
</property>
</bean>

<bean id="validator" class="org.springmodules.validation.commons.DefaultBeanValidator">
<property name="validatorFactory" ref="validatorFactory"/>
</bean>

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>


这是spring-servlet.xml代码

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<!--不用配置bean,spring自动扫描该包下得所有类,
扫描类前标识有@Repository、@Service和 @Controller和@Component注解标识的类,实现bean的自动载入-->
<context:component-scan base-package="com.kjt" />

<mvc:annotation-driven />


</beans>

接下来是jsp代码

<%@ page language="java" contentType="text/html; charset=GB18030"    pageEncoding="GB18030"%>
<%@ taglib uri="/WEB-INF/tld/c-rt.tld" prefix="c"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib uri="/WEB-INF/tld/spring-form.tld" prefix="form"%>
<%@ taglib uri="http://www.springmodules.org/tags/commons-validator" prefix="validator" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>登录页面</title>
</head>
<body>
<h1>欢迎访问,登录页面</h1>
<form action="login1.do" method="post" id="user" name="user" commandName="user" onsubmit="return validateUser(this);">
<table>
<tr>
<td>
用户名:
</td>
<td>
<spring:bind path="user.userName">
<input type="text" id="userName" name="userName" value="${status.value}"/>
<font color="red"><c:out value="${status.errorMessage}"/></font>
</spring:bind>

</td>
</tr>
<tr>
<td>
密码:
</td>
<td>
<spring:bind path="user.password">
<input type="text" id="password" name="password" value="${status.value}"/>
<font color="red"><c:out value="${status.errorMessage}"/></font>
</spring:bind>
</td>
</tr>
<tr>
<td>
信息提示:
</td>
<td>
<spring:bind path="user">
<c:if test="${not empty status.errorMessages }">
<c:forEach var="error" items="${status.errorMessages}">
<c:out value="${error }" escapeXml="false"/>
</c:forEach>
</c:if>
</spring:bind>
</td>
</tr>
<tr>
<td>
<input type="submit"  value="登录" >
</td>
<td></td>
</tr>
</table>
</form>
</body>
</html>

报错的消息

五月 29, 2015 4:31:32 下午 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet [jsp] in context with path [/commonvalidators] threw exception [javax.servlet.ServletException: javax.servlet.jsp.JspTagException: Neither BindingResult nor plain target object for bean name 'user' available as request attribute] with root cause
javax.servlet.jsp.JspTagException: Neither BindingResult nor plain target object for bean name 'user' 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.WEB_002dINF.jsp.login_jsp._jspService(login_jsp.java:143)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:617)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1521)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1478)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)



 请问问题出在那里啊 各位大神,一只说<spring:bind path="user"> 这里面的user找不到,在线等急急急,不用连接数据库的

加载中
0
麦壳原野
麦壳原野
应该是login1方法的问题,去掉@modelattribute试试
0
wuyuehang
wuyuehang
还是不行
返回顶部
顶部