Spring MVC IllegalStateException Error
I'm trying to create a simple test website using spring MVC and I cannot seem to get past the most simple case. I have a jsp website that is a "login" that has 2 inputs and a submit button, but it will not display anything except an error message. Using Spring 3.0.2 and Tomcat 6.0.29 Stand-alone.
IndexController.java:
@Controller
@RequestMapping("login.htm*")
public class IndexController {
@RequestMapping(method=RequestMethod.GET)
public String getLoginPage(Map model){
UserAccess user = new UserAccess();
model.put("user", user);
return "login";
}
login.jsp:
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<form:form action="login.htm" commandName="user">
<table>
<tr>
<td>Enter Username:</td>
<td><form:input path="username" /></td>
</tr>
<tr>
<td>Enter Password:</td>
<td><form:password path="password" /></td>
</tr>
<tr>
<td><input type="submit" value="Submit"></td>
</tr>
</table>
</form:form>
&开发者_JS百科lt;/body>
</html>
My UserAccess class is nothing more then a username, password and a bunch of getter/setters, just like any bean should be. Enclosed it a picture of the error I get upon build, please let me know if you need any more information to help me.
My servlet.xml
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="" />
<property name="suffix" value=".jsp" />
</bean>
As well as my project's web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
Your command name is not user
, it's userAccess
. The error is telling you it can't find a command name of user in your tag.
Edit: Ok, consider splitting the model attribute out into it's own method using the @ModelAttribute annotation.
@ModelAttribute("user")
public UserAccess formBackingObject() {
return new UserAccess();
}
Having said that what you've done should work. Alternatively try returning both the model and the view.
return new ModelAndView("login", model);
Perhaps its not picking up the model map in the signature... but I thought that would work.
I think you request directly for login.jsp
instead of going through the Spring controller. That's why there's no user attached to your request by the time the JSP file is rendered, since the request has not involved the DispatcherServlet
at all.
That is because your welcome-file-list
points to login.jsp
and I assume login.jsp
is not located under WEB-INF/{something}
, but it's in the root of the web application, therefore directly accessible.
To check if my assumption is right, try to request for: http://localhost:8080/login.htm
instead of simply http://localhost:8080
.
I cannot believe I was so stupid. I figured out my own problem. This is why I hate Annotations... in my servlet.xml file, I had the component-scan scanning the wrong package, so none of my annotations were being picked up. Refactoring is a pain, eh?
TL;DR: Make sure you list the correct package in your servlet.xml under
精彩评论