简单的SpringMVC 3登录不起作用

时间:2013-02-05 19:21:48

标签: java xml spring-mvc

我正在尝试通过注释在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 / getters。

LoginController.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)

我会帮助你!

1 个答案:

答案 0 :(得分: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">