0

我正在尝试通过注释在 Spring MVC 3.0 中制作一个简单的登录网页。处理了几个小时后,我无法运行它。我相信问题出在调度程序上,但我尝试了很多东西但没有成功......我将发布一些代码:

LoginView.jsp(仅显示表单)

<form:form method="post" action="doLogin" commandName="login" modelAttribute="login">
        <p> <form:input path="username"/> </p>
        <p> <form:input path="password"/> </p>
        <p class="submit"><input type="submit" name="commit" value="Login"></p>
</form:form>

Login.java 非常简单,因为它只包含用户名和密码属性以及它们的 setter/getter。

登录控制器.java

@Controller
@RequestMapping("doLogin")
public class LoginController {

    @RequestMapping(method = RequestMethod.GET)
    public String showForm(Map model) {
        Login login = new Login();
        model.put("login", login);
        return "LoginView";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String processForm(Login login, BindingResult result, Map model) {
        String userName = "Admin";
        String password = "root";
        if (result.hasErrors()) {
            return "login";
        }
        login = (Login) model.get("login");
        if (!login.getUsername().equals(userName) || !login.getPassword().equals(password)) {
            return "loginerror";
        }
        model.put("login", login);
        return "loginsuccess";
    }
}

调度程序-servlet.xml

<mvc:annotation-driven />

    <context:component-scan base-package="spring.blog.src"/>  

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>

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

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

    <bean name="indexController" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
        <property name="viewName">
            <value>LoginView</value>
        </property>
    </bean>

我知道这里有问题,但不知道到底是什么...... Tomcat抛出的错误是(对不起,有些行是西班牙语......)

org.apache.jasper.JasperException: Ha sucedido una excepción al procesar la página JSP /WEB-INF/jsp/LoginView.jsp en línea 26

24:       <form:form method="post" action="doLogin" commandName="login">
25:         <!-- <p><input type="text" name="login" value="" placeholder="Username or Email"></p> -->
26:         <p> <form:input path="username"/> </p>
27:         <!-- <p><input type="password" name="password" value="" placeholder="Password"></p> -->
28:         <p> <form:input path="password"/> </p>
29:         <p class="remember_me">

Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:521)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:424)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)

causa raíz

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'login' available as request attribute
    org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:141)
    org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:178)
    org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:198)

我会感谢任何帮助!

4

1 回答 1

1

您的方法签名应如下所示。签名需要 @ModelAttribute 注释。该模型不需要作为签名的一部分。

@RequestMapping(method = RequestMethod.POST)
public String processForm(@ModelAttribute Login login, BindingResult result) {
    String userName = "Admin";
    String password = "root";
    if (result.hasErrors()) {
        return "login";
    }
    if (!login.getUsername().equals(userName) || !login.getPassword().equals(password)) {
        return "loginerror";
    }
    return "loginsuccess";
}

此外,您的 jsp 只需要命令名称属性,而不需要 form:form 标记上的 modelAttribute。

<form:form method="post" action="doLogin" commandName="login">
于 2013-02-05T19:31:59.020 回答