Hi
I am new to the spring and stuck with the a problem. Please help me to come out from that.
we.xml
MyApp-servlet.xmlCode:<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>MyApplication</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>MyApp</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>MyApp</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
MyController.javaCode:<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" 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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <mvc:annotation-driven /> <context:component-scan base-package="com.bansal.controller" /> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="WEB-INF/view/" /> <property name="suffix" value=".jsp" /> </bean> <bean id="passedModel" class="com.bansal.model.UserInformation"></bean> </beans>
In the above controller class when i hit for the registration page is shows me the registraion page. But when i hit the submit button and leave the field blank then the Binding Result object gets the error and return the NewRegistrationPageView from the showInformation method.Code:package com.bansal.controller; import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import com.bansal.model.MyModel; import com.bansal.model.UserInformation; @Controller public class MyController { String formName; MyModel modelObject; @Autowired MyController(MyModel passedModel) { modelObject = passedModel; } @RequestMapping(value = "/RegistartionPage") public String showRegistarionForm(ModelMap model) { model.addAttribute("NewRegistrationPageModel", new UserInformation()); return "NewRegistrationPageView"; } @RequestMapping(value = "/showInformation", method = RequestMethod.POST) public String showInformation(@Valid UserInformation userInfo, BindingResult result) { if (result.hasErrors()) { return "NewRegistrationPageView"; } return "UserInfomationPageView"; } }
Now when i go to the NewRegistrationPageView .jsp . This time it doesn't get command class.
Here is my JSP
First time the page is loaded successfully but when i submit the form it give me error.Code:<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <!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"> <title>${NewRegistarionPageModel.formName}</title> </head> <body> <center>${NewRegistarionPageModel.formName}</center> <form:form method="post" action="showInformation" commandName="NewRegistrationPageModel"> <table> <tr> <td><form:label path="fName">First Name:</form:label></td> <td><form:input path="fName" /></td> <td><form:errors path="fName" cssClass="error"/> </tr> <tr> <td><form:label path="lName">Last Name:</form:label></td> <td><form:input path="lName" /></td> <td><form:errors path="lName" cssClass="error"/> </tr> <tr> <td><form:label path="emailAddress">Email Address:</form:label> </td> <td><form:input path="emailAddress" /></td> <td><form:errors path="emailAddress" cssClass="error"/> </tr> <tr> <td><form:label path="password">Password:</form:label></td> <td><form:password path="password" /></td> <td><form:errors path="password" cssClass="error"/> </tr> <tr> <td><form:label path="confPass"> Confirm Password</form:label> </td> <td><form:password path="confPass" /></td> <td><form:errors path="confPass" cssClass="error"/> </tr> <tr> <td><form:label path="gender">Gender</form:label></td> <td><form:radiobutton path="gender" value="male" label="Male" /> <form:radiobutton path="gender" value="female" label="Female" /></td> <td><form:errors path="gender" cssClass="error"/> </tr> <tr> <td colspan="2"><input type="submit" id="submit" value="submit" /></td> </tr> </table> </form:form> </body> </html>
Please help me to comeout from this error. I have visited many forum and spent 3-4 days but no solution.Code:org.apache.jasper.JasperException: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'NewRegistrationPageModel' available as request attribute org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:548) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:466) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389) org.apache.jasper.servlet.JspServlet
Thanks in advance.


Reply With Quote
