这是错误代码
Servlet.service() for servlet dispatcher threw exception
org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 2 errors
Field error in object 'command' on field 'username': rejected value []; codes [username.is.null.command.username,username.is.null.username,username.is.null.java.lang.String,username.is.null]; arguments []; default message [用户名不能为空]
Field error in object 'command' on field 'password': rejected value []; codes [password.is.null.command.password,password.is.null.password,password.is.null.java.lang.String,password.is.null]; arguments []; default message [密码不能为空] 这是MultiActionController代码:
Java code
 public class AdminsController extends MultiActionController {
    private Admins admin;
    private AdminServiceImp adminservice;
    /**
     * 初始化页面方法
     * @param request
     * @param response
     * @return
     */
    public ModelAndView initPage(HttpServletRequest request,HttpServletResponse response){
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("command", admin);
        System.out.println(model);
        return new ModelAndView("Login",model);
    }
    /**
     * 验证登陆的方法
     * @param request
     * @param response
     * @param admin
     * @return
     */
    public ModelAndView login(HttpServletRequest request,HttpServletResponse response,Admins command){
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("Login", command);
        HttpSession session = request.getSession();    
        boolean flag=adminservice.checkLogin(command.getUsername(), command.getPassword());
        if(flag){
        session.setAttribute("admin", command);
        return new ModelAndView("Index");
        }
        return new ModelAndView("Index",model);
    } 这是Validator中的代码:
Java code
 public class LoginValidator implements Validator {
    public boolean supports(Class clazz) {
        // TODO Auto-generated method stub
        return clazz.equals(Admins.class);
    }    public void validate(Object admins, Errors errors) {
        // TODO Auto-generated method stub
        
        ValidationUtils.rejectIfEmptyOrWhitespace(errors,"username", "username.is.null", "用户名不能为空");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "password.is.null", "密码不能为空");
        Admins admin=(Admins)admins;
    }
} 这是dispatcher-servlet配置文件:
XML code
<bean name="/admins.do"
        class="cn.inspur.hzp.controller.AdminsController">
        <property name="methodNameResolver" ref="methodResolver"></property>
        <property name="admin" ref="admins"></property>
        <property name="adminservice" ref="adminservice"></property>
        <property name="validators">
            <list>
                <bean id="loginValidator"
                    class="cn.inspur.hzp.validator.LoginValidator" />
            </list>
        </property>
    </bean> 这是HTML页面中的代码:
HTML code
<table width="100%" border="0" align="center" cellpadding="0"
                        cellspacing="0" background="images/position_line.jpg" ID="Table4">
                        <form:form action="admins.do?method=login" method="post"
                            name="Form1">
                            <tr>
                                <td width="14" height="38" align="right" valign="top">
                                    <img src="<%=basePath%>/Admin/images/position.jpg" width="12"
                                        height="38" />
                                </td>
                                <td width="116" background="images/position_line.jpg">
                                    <img src="<%=basePath%>/Admin/images/memberLogin.jpg"
                                        width="116" height="33" />
                                </td>
                                <td background="<%=basePath%>/Admin/images/position_line.jpg"
                                    class="search_field">
                                    <div class="search_form">
                                        &nbsp; 用户名
                                        <form:input path="username" cssClass="search" id="uid"
                                            size="20" />
                                        <form:errors path="username"></form:errors>
                                        密码
                                        <form:password path="password" cssClass="search" />
                                        <form:errors path="password"></form:errors>
                                        <input type="submit" value="登录" class="submit"
                                            onclick="return loginCheck();" ID="Submit1" />
                                    </div>
                                </td>
                                <td width="15" class="ProductText">
                                    <img src="<%=basePath%>/Admin/images/position_End.jpg"
                                        width="12" height="38" />
                                </td>
                            </tr>
                        </form:form>
                    </table>如何用MultiActionController的validators属性实现验证,高手帮忙吧

解决方案 »

  1.   

      @Override
        public void doFilter(ServletRequest requestSer, ServletResponse responseSer, FilterChain chain)
                throws IOException, ServletException {
            HttpServletRequest request = (HttpServletRequest) requestSer;
            HttpServletResponse response = (HttpServletResponse) responseSer;
          
            // 设置请求参数
            setParameters(WebUtils.getParametersStartingWith(request, ""));
            // 获取根目录所对应的绝对路径:
            String currentURI = request.getRequestURI();
            log.info("doFilter.currentURI: " + currentURI);
            log.info("request.getContextPath():"+ request.getContextPath()+".");
            // 截取命名空间、action、路径参数
            String targerURI ="";
            if ("".equals(request.getContextPath())) {
                targerURI = currentURI.substring(currentURI.indexOf("/", 0), currentURI.length());
            }else{
                targerURI = currentURI.substring(currentURI.indexOf("/", 1), currentURI.length());
            }
            log.info("doFilter.targerURI: " + targerURI);
            
            if ("/".equals(targerURI)|| (!targerURI.endsWith(".htm"))) {
                chain.doFilter(request, response);
                return;
           }
            
            // 如果属于白名单,直接返回
            if (checkBlank(targerURI)) {
                 chain.doFilter(request, response);
                 return;
            }        HttpSession session = request.getSession();        // 子系统登录
            if (null == session || session.getAttribute(SessionConstants.SESSION_USER) == null) {
             log.info("session 地址是"+request.getRequestURL()+"");
                request.getSession().setAttribute(SessionConstants.SESSION_PRE_URL, request.getRequestURL());
                response.sendRedirect(request.getContextPath() + VGS_LOGIN_URL);
                return;
            }
            
            // 从COOKIE中取出B2C登陆信息
            Cookie[] cookies = request.getCookies();
            String memberId = "";
            if (null != cookies) {
                for (Cookie cookie : cookies) {
                    if (cookie.getName().startsWith("WC_USERACTIVITY_")) {
                        memberId = cookie.getName().substring(cookie.getName().lastIndexOf("_") + 1); // 取出B2C登陆的会员ID
                        //log.info("doFilter.memberId: "+memberId); 
                        break;
                    }
                }
            }        if (StringUtils.isNotEmpty(memberId) && !"-1002".equals(memberId)) {// memberId不
                                                                                // 为空,且不是-1002,说明B2C已经登陆
                // 检查子系统是否登录,如果子系统登录,检查账号是否一致,
                SessionUserModel sessionData = (SessionUserModel) session
                        .getAttribute(SessionConstants.SESSION_USER);
                if ((null == sessionData) || (!memberId.equals(sessionData.getMemberId()))) { // 子系统没有登录,或者
                                                                                              // B2C账号和子系统账号不同,则需要从B2C重新登录
                    // 清空子系统登录信息
                    session.removeAttribute(SessionConstants.SESSION_USER);
                }
            } else { // B2C没有登录,清空子系统登录信息
                session.removeAttribute(SessionConstants.SESSION_USER);
            }        chain.doFilter(request, response);
            return; 
        }    /**
         * 判断白名单
         * 
         * @param url
         * @return
         */
        private boolean checkBlank(String url) {
            if (null == blank || blank.size() == 0)
                return false;
            else
                for (int i = 0; i < blank.size(); i++)
                    //if (blank.get(i).equals(url))
                    if(url.startsWith(blank.get(i)))  
                        return true;
            return false;
        }    /**
         * 初始化白名单
         * 
         * @param servletContext
         * @return
         */
        private List<String> getBlank(ServletContext servletContext) {
            List<String> blank = new ArrayList<String>();
            try {
                ApplicationContext ctx = WebApplicationContextUtils
                        .getWebApplicationContext(servletContext);
                InputStream is = ctx.getResource("classpath:conf/auth/blank.txt").getInputStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                String line;
                while ((line = br.readLine()) != null) {
                    blank.add(line);
                }
                if (null != br) {
                    br.close();
                }
            } catch (IOException e) {
                log.error("读取白名单出错");
            }
            return blank;
        }    public void setBlank(List<String> blank) {
            this.blank = blank;
        }    public Map<String, Object> getParameters() {
            return parameters;
        }    public void setParameters(Map<String, Object> parameters) {
            this.parameters = parameters;
        }
      

  2.   

    我想实现的非空校验,当输入用户名或密码为空时,会提示,用户名不能为空,密码不能为空,用Validator实现。
      

  3.   

    你再controller层也可以验证啊 ,这个最简单 当用户名和密码为空的话 ,通过modelandview 设置信息,返回到jsp页面显示 。
      

  4.   

    http://www.cnblogs.com/tanhao/archive/2011/01/09/1931052.html 你要不参考下这个吧