Hi,
I am new to Spring MVC. I have created an application. Here's my code.
UserController.javaHTML 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>
User.javaCode:@Controller public class UserController { @RequestMapping(value = "/save", method = RequestMethod.POST) public String saveUser(@ModelAttribute("user") User user, BindingResult result) {; return ApplicationConstants.SAVE; } }
servlet-context.xmlCode:@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
When I build and run this sample application, then on accessing http://localhost:8080/SpringMVC-web/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>
it throws an error
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'user' available as request attribute
Please help....


Reply With Quote
