Java – spring 2.5 jstl view error

frameworksjavaspring

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.

Best Answer

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.

Related Topic