Announcement Announcement Module
Collapse
No announcement yet.
Getting IllegalStateException while running a simple Spring MVC application Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Getting IllegalStateException while running a simple Spring MVC application

    Hi,

    I am new to Spring MVC. I have created an application. Here's my code.

    HTML Code:
    <form:form method="post" action="save.html" commandName="user">
    
    	
    		<table>
    			<tr>
    				<td><form:label path="loginName">
    						<s:message code="nameLabel" />
    					</form:label></td>
    				<td><form:input path="loginName" /></td>
    			</tr>
    			<tr>
    				<td><form:label path="password">
    						<s:message code="passwordLabel" />
    					</form:label></td>
    				<td><form:input path="password" /></td>
    			</tr>
    			<tr>
    				<td><form:label path="role">
    						<s:message code="roleLabel" />
    					</form:label></td>
    				<td><form:input path="role" /></td>
    			</tr>
    			
    			<tr>
    				<td colspan="2"><input type="submit"
    					value="<s:message code="login"/>" /></td>
    			</tr>
    		</table>
    	</form:form>
    UserController.java

    Code:
    @Controller
    public class UserController {
    
    @RequestMapping(value = "/save", method = RequestMethod.POST)
        public String saveUser(@ModelAttribute("user") User user, BindingResult result) {;
            return ApplicationConstants.SAVE;
        }
    }
    User.java
    Code:
    @Entity
    @Table(name = "spring_user")
    public class User implements Serializable {
    
        /** use serialVersionUID from JDK 1.0.2 for interoperatiblity */
        private static final long serialVersionUID = 1L;
    
        /** The userId is used to uniquely identify a user **/
        @Id
        @GeneratedValue
        @Column(name = "ID")
        private long userId;
    
        /** The name is used for name of the user */
        @Column(name = "NAME")
        private String name;
    
        /** The password is used for the password of the user */
        @Column(name = "PASSWORD")
        private String password;
    
        /** The age is used for age of the user */
        @Column(name = "AGE")
        private Integer age;
    
        /** The dob is used for date of birth of the user */
        @Column(name = "DOB")
        private Date dob;
    
        /** The loginName is used for loginName of the user */
        @Column(name = "LOGINNAME")
        private String loginName;
    
        /** The confirmPassword is used for reconfirm the password of the user */
        @Column(name = "CONFIRMPASSWORD")
        private String confirmPassword;
    
        /** The address is used for address of the user */
        @Column(name = "ADDRESS")
        private String address;
    
        /** The role is used for role (admin/guest) of the user */
        @Column(name = "ROLE")
        private String role;
    
    //Getters and Setters
    servlet-context.xml
    Code:
    <?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:context="http://www.springframework.org/schema/context"
    	xmlns:mvc="http://www.springframework.org/schema/mvc"
    	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
    	<!-- Scans the classpath of this application for @Components to deploy as beans -->
    	<context:component-scan base-package="com.nagarro.user" />
    
    	<!-- Configures the @Controller programming model -->
    	<mvc:annotation-driven />
    
    	<!-- Forwards requests to the "/" resource to the "welcome" view -->
    	<mvc:view-controller path="/" view-name="index"/>
    
    	<!-- Configures Handler Interceptors -->	
    	<mvc:interceptors>
    		<!-- Changes the locale when a 'locale' request parameter is sent; e.g. /?locale=de -->
    		<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
    	</mvc:interceptors>
    
    	<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources/ directory -->
    	<mvc:resources mapping="/resources/**" location="/resources/" />
    
    	<!-- Saves a locale change using a cookie -->
    	<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver" />
    
    	<!-- Application Message Bundle -->
    	<!-- <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    		<property name="basename" value="/WEB-INF/messages/messages" />
    		<property name="cacheSeconds" value="0" />
    	</bean> -->
    	<bean id="messageSource"
    		class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    		<property name="basename" value="classpath:messages" />
    		<property name="defaultEncoding" value="UTF-8" />
    	</bean>
    	<!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory -->
    	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    		<property name="prefix" value="/WEB-INF/pages/"/>
    		<property name="suffix" value=".jsp"/>
    	</bean>
    
    </beans>
    When I build and run this sample application, then on accessing http://localhost:8080/SpringMVC-web/

    it throws an error

    java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'user' available as request attribute


    Please help....
    Last edited by pankaj1907; Jul 17th, 2012, 04:44 AM.

  • #2
    Please use the forum search as this question has been answered numerous times before...

    Each request should go through the dispatcher servlet and the view should be selected by your controller and the model filled. You only have a POST method so this isn't happening. I suggest you take a look at the samples in the springsource repository on Github and read the reference guide on form handling.

    Comment


    • #3
      Originally posted by Marten Deinum View Post
      Please use the forum search as this question has been answered numerous times before...

      Each request should go through the dispatcher servlet and the view should be selected by your controller and the model filled. You only have a POST method so this isn't happening. I suggest you take a look at the samples in the springsource repository on Github and read the reference guide on form handling.
      Hi Marten,

      Thanks for your response.

      I searched about the same, but still could not find the answer, how can i make each request go thru the dispatcher servlet and view select by controller.

      Comment

      Working...
      X