Store variables in a jsp page scope with the spring framework

How to store variables in the jsp page scope with the spring 2.5.x framework?

This has compile errors.

import javax.servlet.jsp.PageContext;

public ModelAndView handleRequestInternal(HttpServletRequest request,
		HttpServletResponse response) throws Exception {
		PageContext pageContext;
		Object o = pageContext.getAttribute("com.mycompany.pageId");

Description Resource Path Location Type
The local variable pageContext may not have been initialized /com.mycompany.monitoring.war/src/com/mycompany/monitoring/war line 59 Java Problem

PageContext pageContext;

pageContext is an Object of PageContext type, but has no value.

Which means the following line actually means:

Object o = null.getAttribute(“com.mycompany.pageId”);

null has no values or methods, which is why the compiler is complaining.

I’m not familiar with Spring, so I don’t know if a pageContext is supposed to be available where you’re trying to do this, if one is supposed to be available, you simply start using it - it’s already been declared. By declaring yours locally, the name, pageContext, now refers to your local, unassigned, pageContext and not the ‘globally’ available one.

What is the proper way to initialize pageContext?

What exactly are you trying to do? First, you should not store variables into Spring context. It’s used for resource only! More likely, you’re looking to store that into request or session scope. In more ways than one… I don’t think you’re using Spring for what’s meant to be.

I want to store some variables (e.g. number of rows per table) at page scope in Spring so that each page has its own group of variables. What is the proper way to do that? My approach of using the PageContext for that purpose may or may not be wrong.

Before I answer, let me ask

Are you using Spring Form Beans? Also, you need to be using either Spring MVC or Web Flow.

If not, I advise looking at documentation for Spring MVC/Web Flow.

I use Spring MVC - in particular the ParameterizableViewController.
This has compile error and I am not sure whether this is the correct approach to initialize pageContext .

	public ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
	javax.servlet.Servlet servlet;
        String errorPageURL = null;
        boolean needsSession = false;
        int buffer = 100;
        boolean autoflush = true;
		PageContext pageContext = JspFactory.getPageContext(
				servlet, request, response, errorPageURL, needsSession, buffer, autoflush);

Description Resource Path Location Type
Cannot make a static reference to the non-static method getPageContext(Servlet, ServletRequest, ServletResponse, String, boolean, int, boolean) from the type JspFactory /com.mycompany.monitoring.war/src/com/mycompany/monitoring/war line 63 Java Problem

When I look at your code, I’m not even sure if it’s Spring MVC. Perhaps, it’s really old version Spring 1.X which I have not played with. With Spring 2.X+, this is how the simplest code look like. You store the results into the model. If you want more fine control of the model then you must use Spring Web Flow which has the scope “View”. This is all I’m going to say for this topic, g’luck.

public String list(Model model) {
model.setAttribute(“totalTableRows”, 100);
return “/WEB-INF/accounts/list.jsp”