views:

45

answers:

2

index.jsp

...
<h1> ${myobject} </h1>
...

HomeController.java

@RequestMapping(value = "/index")
public ModelAndView indexPath() {
    System.out.println("going home");
    return new ModelAndView("index", "myobject", "isastring");
}

Output:

going home

The <h1> on index doesn't show anything, how is this even possible? I absolutely cannot get my index.jsp to show this bean, I've tried using a usebean, I've tried storing it on the session, and now I'm directly placing it in the model. Nothing works. Spring 3 has been like every other spring release, intensely frustrating.

A: 

First off, ensure that the framework is in fact rendering the JSP you think it is. Add some other static content into the page and get that working first.

Print to System.err, or rather, use a logging framework.

Try SLF4J, it's almost universally understood.

Logger logger = LoggerFactory.getLogger(MyClass.class);
logger.info("going home");

Then, I have found it far easier to use conventions and annotations..

@RequestMapping(value = "/index")
public void index(ModelMap model) {
    System.err.println("going home");
    model.addAttribute("myobject", "isastring");
}

By convention the view that will be rendered will be the one resolved from the name "index", which is the same as your code above. This is because the method return type is void.

Adding a ModelMap into a @RequestMapping annotated method is supported and automatically provides you with a Model to populate.

ptomli
I checked the JSP, that's rendering the right JSP, I really have no clue what's wrong, but since using spring 3 I've had nothing but problems, I understand you do it slightly different, but it should work the way I'm doing it too
walnutmon
So the JSP is being rendered but there is no text between the <h1> tags? Have you tried using <c:out value="${myobject}"/>?
ptomli
I did try that, the problem wasn't something someone could have solved without seeing my full class, take a look at my answer :o. this time the flexibility of Spring 3 was what caused all the confusion
walnutmon
+1  A: 

The mistake made wasn't in the posted code, it was in the imports

import org.springframework.web.**portlet**.ModelAndView;

instead of this:

import org.springframework.web.**servlet**.ModelAndView;
walnutmon
That would do it :)
ptomli