Announcement Announcement Module
Collapse
No announcement yet.
Simple Form Question Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Simple Form Question

    Hi Guys,

    I am stepping away from the controller hierchy and am using the annotaions for my controller @Controller. I am having a issue where I get the following error:

    Neither BindingResult nor plain target object for bean name 'test' available as request attribute

    My code is below:

    Code:
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
    <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/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>Insert title here</title>
    </head>
    <body>
    <form:form action="home.htm" method="post" modelAttribute="test">
    	
    	<form:input path="test.test1"/>
    	<form:input path="test.test2"/>
    	<input type="submit" value="submit"/>	
    </form:form>
    </body>
    </html>
    Code:
    package com.talkmuslim.Controllers;
    
    import javax.xml.ws.BindingType;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.ModelAttribute;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.SessionAttributes;
    
    @Controller
    @RequestMapping("/home.htm")
    @SessionAttributes("test")
    public class InsertCandidateDetails{
    
    	public InsertCandidateDetails(){};
    	
    	@ModelAttribute("test")
    	public Test getTest()
    	{
    		System.out.println("Within the test method");
    		return new Test();
    	}
    	
    	@RequestMapping(method = RequestMethod.GET)
    	public String test(@ModelAttribute("test")Test test, Model model)
    	{
    		System.out.println("Within the get method");
    		model.addAttribute("test", test);
    		
    		//System.out.println("The value of test is: " + test.getTest1());
    		
    		return "home";
    	}
    	
    	@RequestMapping(method = RequestMethod.POST)
    	public String showTheTest(@ModelAttribute("test")Test test,  Model model)
    	{
    		
    		model.addAttribute("value1", test.getTest1());
    		model.addAttribute("value2", test.getTest2());
    		model.addAttribute(test);
    		
    		System.out.println("The value of test is: " + test.getTest1());
    		
    		return "home";
    	}
    }
    Any help would be great.

    Regards

    Adnan

  • #2
    Let's check this a little bit, cause it looks like you don't really know what you are doing.

    First, let's see what will happen with your code as it is now.
    First your method getTest() will be called and the newly instantiated object will be placed in the model.

    Then the method test() will be called and the instance previously created will be passed in the 'test' parameter (Please note that if the attribute did not exist a new one would have been instantiated placed in the model and passed in. So your getTest() method is actually useless here).

    Then you use model.addAttribute() which will replace the Test object already in the model with itself. So this line is useless too.

    The same goes in the showTheTest method

    Then, I guess, the model attribute is placed in the session cause you used @SessionAttributes. So far you are not using it, plus the attributes value2 and value2 are not used anywhere.

    Then in your jsp you specify you modelAttribute to be this 'test' attribute.
    But the path in your form controls is wrong. The path must be a property in your modelAttribute, so you don't have to specify 'test.' since that will cause it to lookup for a 'test.test.test1' property.

    So please, fix your code a little bit and tell us if you still get the error.
    Code:
    <form:form action="home.htm" method="post" modelAttribute="test">
    	<form:input path="test1" />
    	<form:input path="test2" />
    	<input type="submit" value="submit" />
    </form:form>
    Code:
    @Controller
    @RequestMapping("/home.htm")
    public class InsertCandidateDetails{
    
    	public InsertCandidateDetails(){};
    
    	@RequestMapping(method = RequestMethod.GET)
    	public String test(@ModelAttribute("test")Test test)
    	{
    		System.out.println("Within the get method");
    		// test has just been initialized
    		System.out.println("Test instance: " + test);
    		// test values should all be null here
    		System.out.println("The value of test is: " + test.getTest1());
    		System.out.println("The value of test is: " + test.getTest2());
    		test.setTest1("Default value1");
    		test.setTest2("Default value2");
    		
    		return "home";
    	}
    	
    	@RequestMapping(method = RequestMethod.POST)
    	public String showTheTest(@ModelAttribute("test")Test test)
    	{
    		System.out.println("Within the get method");
    		// a new test object have been initialized (I think)
    		System.out.println("Test instance: " + test);
    		// the form input binding has already taken place
    		System.out.println("The value1 of test is: " + test.getTest1());
    		System.out.println("The value2 of test is: " + test.getTest2());
    		
    		return "home";
    	}
    }

    Comment


    • #3
      Will Test Later

      Hi,

      thanks for the explaination, I am reading Spring in Action which is a fantastic book but does not cover annotation controllers. I will implement as you suggested and will let you know later on today.

      Thanks!!

      Comment


      • #4
        No Result

        Hi Triqui,

        I think that you have mis understood the problem. After having had a look at your answer you have pointed out what would happen during a request. The issue here is trying to get a request to the server. When my browser loads with index.jsp its then that this exception israised not after a response is produced. Could someone please point me in the right direction.

        Thanks

        Comment


        • #5
          Originally posted by aadz5 View Post
          When my browser loads with index.jsp its then that this exception israised not after a response is produced.
          I don't know if you tried what I posted, but maybe you should give it a try.
          Maybe I'm wrong but I really think your exception is thrown when the request is being processed and the response being built. In fact, it's probably in this line
          Code:
          <form:form action="home.htm" method="post" modelAttribute="test">
          And that's because you are not handling your model properly and in the end, it looks like your object is not in the request.
          What I posted was a cleanup of your code just to make sure there was not a side effect from one of the multiple mistakes. But I think your error can still be somewhere else.

          Do you get any output from any of the following lines:
          Code:
          System.out.println("Within the get method");
          System.out.println("Within the test method"); // <- You should fix this line. I just noticed
          System.out.println("The value of test is: " + test.getTest1());
          I mean, are the methods from your controller actually invoked?


          Well, Now i read again your post.
          You have "home.htm" mapped to your controller then it returns "home" which is the id of the view you want to render. Then you must have some configuration to make spring load "index.jsp" when the view "home" is invoked.

          Maybe you should post a bit more of your config files, basically the configuration of your views.
          Again, are the methods from your controller actually invoked?

          Comment


          • #6
            Solution

            Hi Triqui,

            thanks for coming back to me.

            I went through the code with a fine tooth combe on the weekend and low and behold my initial index.jsp was not being retrieved via the dispatcher servlet so the form backing bean was not being created and hence the exception.

            Once I went through the DisptacherServlet it all worked fine. I made the corrections that you suggested as well so most appreciated. Onwards and upwards as they say!

            Thanks and Regards

            Comment

            Working...
            X