Results 1 to 4 of 4

Thread: Didn't get the model object on the JSP

  1. #1

    Default Didn't get the model object on the JSP

    Hi

    I am new to the spring and stuck with the a problem. Please help me to come out from that.

    we.xml
    Code:
    <?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>
    MyApp-servlet.xml
    Code:
    <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>
    MyController.java
    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";
    
    	}
    
    }
    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.

    Now when i go to the NewRegistrationPageView .jsp . This time it doesn't get command class.

    Here is my JSP

    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>
    First time the page is loaded successfully but when i submit the form it give me error.

    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
    Please help me to comeout from this error. I have visited many forum and spent 3-4 days but no solution.

    Thanks in advance.

  2. #2
    Join Date
    Jan 2009
    Location
    Huntington Beach, CA
    Posts
    718

    Default

    Um, this is for the Spring Data project. Your Question is a Spring MVC issue. Hopefully a moderator can move your thread to the Spring MVC forum.

    Thanks

    Mark

  3. #3
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    Moved thread to correct forum.

    For starters why are you injecting an object and not using it (I sincerely hope you aren't intending to use that object as a form object).

    Second your post method doesn't have a ModelAttribute it just has a class, annotate it with @ModelAttribute("NewRegistrationPageModel") next to @Valid.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  4. #4

    Default

    Thanks Marten.
    Its working now

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •