views:

363

answers:

4

Hi,

I have the following controller


@Controller
@RequestMapping("/project/view.html")
public class ProjectViewController {

 private static final String viewName = "projectView";


 @RequestMapping(method = RequestMethod.GET)
 public String showPage(Model model,
   @RequestParam(value="id",required=false) Long id) {

  //code.....

  return viewName;
 }

}

My views.properties contains


projectView.(class)=org.springframework.web.servlet.view.JstlView
projectView.url=/WEB-INF/jsp/project/view.jsp

Everything works as expected.

But I needed to change the method to return a View(), cause I have to return a RedirectView() to some error page if something fails. So I've changed the method to:


@Controller
@RequestMapping("/project/view.html")
public class ProjectViewController {

 private static final String viewName = "projectView";


 @RequestMapping(method = RequestMethod.GET)
 public View showPage(Model model,
   @RequestParam(value="id",required=false) Long id) {

  //code.....

  return new JstlView(viewName);
 }

}

But in this case I get a NPE :


java.lang.NullPointerException
 at org.springframework.web.context.support.WebApplicationObjectSupport.getServletContext(WebApplicationObjectSupport.java:121)
 at org.springframework.web.servlet.view.JstlView.exposeHelpers(JstlView.java:133)
 at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:209)
 at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:257)
 at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1183)
 at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:902)
 at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:807)
 at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:571)
 at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:501)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at com.opensymphony.sitemesh.webapp.SiteMeshFilter.obtainContent(SiteMeshFilter.java:129)
 at com.opensymphony.sitemesh.webapp.SiteMeshFilter.doFilter(SiteMeshFilter.java:77)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
 at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
 at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
 at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
 at java.lang.Thread.run(Thread.java:619)

I've debugged and it seems that getWebApplicationContext() returns null.

Any help appreciated.

Thanks.

+1  A: 

JstlView implements ApplicationContextAware and ServletContextAware, so you should call setApplicationContext and setServletContext (and, probably, some other initializations) if you want to instantiate it manually.

But you can define a RedirectView in your views.properties, so you don't have to return a View object yourself:

someRedirectView.(class)=org.springframework.web.servlet.view.RedirectView
someRedirectView.url=/redirectHere 
axtavt
Thanks, it looks like an elegant solution to define redirect views in views.properties. But what if I need to build the redirect url at request time, something like "/redirectHere?id=" + id; Is it still possible to define it somehow in views.properties, and parameterize it?
adancu
By default RedirectView exposes model attributes as URL parameters, so you can add your id to the model.
axtavt
A: 

The cause of your exception is that instances of JstlView need to be injected with certain things before they can be used, for example the current WebApplicationContext. Normally, this is transparent to you, because Spring instantiates and prepares JstlView for you. It's only when you do it yourself, that you see what's involved.

Now, you could call setApplicationContext on the JstlView that you instantiate, but you don't want your controllers getting involved in that sort of thing. You really want to keep Spring doing the work.

Now, since you're using views.properties, it follows that you're also using ResourceBundleViewResolver to resolve your views. The trick here is to realise that Spring can handle multiple view resolvers in the same context, it'll simply ask them one after the other, until one of them resolves the view name.

So my suggestion would be to add another resolver to your context, this time an InternalResourceViewResolver (this is actually the default resolver, but that default is suppressed when you specified your ResourceBundleViewResolver).

Now, when you return a view name that the ResourceBundleViewResolver can't resolve, the InternalResourceViewResolver will be consulted.

InternalResourceViewResolver just takes the view name as the actual JSP path (e.g. return /WEB-INF/page.jsp directly from your controller method). It also lets you use syntax like redirect:/path/to/my/url.

So yours other controllers can keep on returning view names as specified in views.properties, but now in addition you can return dynamically-assembled redirect:x/y/ view names, which will be translated by InternalResourceViewResolver into a RedirectView. No need to construct these views yourself.

One last point to note: the view resolvers are consulted in the order in which they appear in the beans file, and InternalResourceViewResolver must come last in that list.

skaffman
A: 

Not tested, but this should work

> projectView.url=redirect:/WEB-INF/jsp/project/view.jsp
Teja Kantamneni
A: 

This is a question. where should i place the view.properties in my proyect. i have troubles with ResourceBundleViewResolver because it's not finding the file and it´s throwing an exception.

*java.util.MissingResourceException: Can't find bundle for base name views, locale en_GB*

this is the mapping

<bean id="resourceBundleView" 
    class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
    <property name="basename" value="views"/>
</bean> 

Thank you.

OJVM