我試圖通過註釋在Spring MVC 3.0中創建一個簡單的登錄網頁。處理好幾個小時後,我一直無法運行它。我相信這個問題是與調度程序,但我已經嘗試了很多東西,但沒有成功......我會後一些代碼:Simple SpringMVC 3登錄不起作用
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非常簡單,因爲它只包含用戶名和密碼屬性及其設置者/獲取者。
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)
我會提供任何幫助! !
您是否在接到帖子後收到錯誤消息?我猜你可以拉登錄表單確定,但只是不能讓您的文章後顯示的視圖。 –
爲什麼你有這條線:login =(Login)model.get(「login」); ...傳遞給方法的Login對象應該填充所有的表單信息。 –