java - Spring 3.0 MVC错误: Neither BindingResult nor plain target object for bean name 'login' available as request attribute

标签 java spring-mvc

我引用了 netbeans.org 上的示例 spring 应用程序,并尝试创建一个简单的登录应用程序。当我运行时出现此错误:

javax.servlet.ServletException:javax.servlet.jsp.JspTagException:BindingResult 和 bean 名称“login”的普通目标对象都不能作为请求属性

这是我的login.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Business SMS Login</title>            
</head>

<body>
    <div id="mainContainer">
        <spring:nestedPath path="login">
        <form name="frmBSMSLogin" action="" method="post">
        <div id="controls">
            <div id="lgnUsername">
                <label for="txtUsername">Username</label>                     
                <spring:bind path="login.username">
                    <input type="text" name="${status.expression}" value="${status.value}" id="txtUsername" maxlength="20" class="textInput"/>
                </spring:bind>
            </div>
            <br/>
            <div id="lgnPassword">
                <label for="txtPassword">Password:</label>
                <spring:bind path="login.password">
                    <input type="password" id="txtPassword" maxlength="20" name="${status.expression}" value="${status.value}" class="textInput"/>
                </spring:bind>
            </div>
        </div>

        <div id="submitSection">
            <input type="button" value="Submit" class="buttonInput"/>
            <input type="reset" value="Reset" class="buttonInput"/>
        </div>
        </form>
        </spring:nestedPath>
    </div>
</body>

这是 LoginController.java

package controller;

import org.springframework.web.servlet.mvc.SimpleFormController;
import java.net.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;   
import org.springframework.web.portlet.ModelAndView;
import service.Login;

public class LoginController extends SimpleFormController {
private Login login;
public LoginController() {
    setCommandClass(GetLoginDetails.class);
    setCommandName("login");
    setSuccessView("dashboard");
    setFormView("index");
}

public void setLogin(Login login){
    this.login = login;
}

protected ModelAndView onSubmit( HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)throws Exception {           
    //System.out.println("are we here?");
    GetLoginDetails l = (GetLoginDetails) command;
    ModelAndView mv = new ModelAndView(getSuccessView());
    mv.addObject("helloMessage", login.authenticate(l.getUsername(),l.getPassword()));
    return mv;
}    

}

这是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:p="http://www.springframework.org/schema/p"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

<bean name="login" class="service.Login"/>

</beans>

我想从上周开始我就一直在做这件事。我是通过引用 netbeans 的示例来做到这一点的。这是链接http://netbeans.org/kb/docs/web/quickstart-webapps-spring.html 。我还没有找到任何解决方案。从PHP背景开始我已经发现spring框架太难了,但是我正在全力尝试。我使用 Netbeans 作为我的 IDE。它是一个好的选择还是 Eclipse 是标准选择?另外请给我一些关于如何调试应用程序的建议。 php 中的简单 echo 或 print_r 在这里看起来太奢侈了:)

P.s 我之前曾发布过类似的问题,而这里的某人提示这是一个代码转储并对我投了反对票。有很多问题,有一个巨大的代码转储,但人们很友善地提供帮助。因此,任何认为这是代码转储或有些粗鲁语言的人,请不要浪费宝贵的时间来否决这个问题。我是一个真正的学习者,来这里寻求帮助

最佳答案

尝试删除

<spring:nestedPath path="login">

之后

<body>
    <div id="mainContainer">

spring:nestedPath - 设置绑定(bind)标签路径使用的嵌套路径。

更新:

了解此标签 here

关于java - Spring 3.0 MVC错误: Neither BindingResult nor plain target object for bean name 'login' available as request attribute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10655269/

相关文章:

java - 如果它不是 spring MVC 中的 bean,如何验证请求参数?

java - Spring Tool Suite:“属性”中的“部署程序集”选项

java - Spring无法创建bean

java - 在 Ant 构建中使用 linecontainsregexp 的正确方法是什么?

java - 不使用接口(interface)创建 Mixin

java - 根据位置和跟踪关键字过滤推文

spring - 覆盖 J_Spring_Security_Check

java - apache mina sshd core 版本 1.3.0 版本的客户端是否有 gss/kerberos auth 支持

java - 如何将 java.sql.Date 对象添加到 JSON 数组?

java - 如何避免在 SpringResourceTemplateResolver 上调用 setApplicationContext?