Announcement Announcement Module
Collapse
No announcement yet.
Does Session scope work with MVC controllers on a page refresh? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Does Session scope work with MVC controllers on a page refresh?

    I have a form to choose stats to display. Once it's submitted, I want the resulting view to refresh at regular intervals (say every 60 seconds).

    I'd figured on putting a command-style object on the session and re-using that every time the page is refreshed.

    From what I've read about session scoped objects, I should be able to define an object as a session scoped proxy and the actual object will be instantiated (and put on the session) when it's actually referenced. I thought I'd be able to use the same object over several controllers for the duration of the session.

    Does this include page refreshes?

    I'd be pulling my hair out now if I had any!

    Here's what I get now:
    1. Form Controller populates session scoped object from the form's command object
    2. View (jsp) looks fine, uses the model and view from the form controller
    3. View (jsp) refreshes via http-equiv="refresh"
    4. View Controller attempts to read the session object - should be the same object the form controller created
    5. Session objects don't match

    I'm using Spring MVC 2.5.6, with xml config rather than annotations.

    The bits of my code that seem relevant are:
    Form Controller:
    Code:
    public class TestResultsFormController extends SimpleFormController {
    	private ResultCommandInterface resultsToDisplay;
    
    	public ResultCommandInterface getResultsToDisplay() {
    		return resultsToDisplay;
    	}
    	public void setResultsToDisplay(ResultCommandInterface resultsToDisplay) {
    		this.resultsToDisplay = resultsToDisplay;
    	}
    	protected ModelAndView onSubmit(Object command, BindException bindException) throws Exception {
    		setResultsToDisplay( ( ResultCommandInterface )command );
    		...
    		ModelAndView mav = new ModelAndView( getSuccessView(), "results", results );
    		return mav;
    	}
    }
    View Controller:
    Code:
    public class TestResultsController extends AbstractController {
    	private ResultCommandInterface resultsToDisplay;
    
    	public ResultCommandInterface getResultsToDisplay() {
    		return resultsToDisplay;
    	}
    	public void setResultsToDisplay(ResultCommandInterface resultsToDisplay) {
    		this.resultsToDisplay = resultsToDisplay;
    	}
    	protected ModelAndView handleRequestInternal(HttpServletRequest req,
    		HttpServletResponse response) throws Exception {
    		log.debug("*****  resultsToDisplay from session is this: [" + getResultsToDisplay().getEnvironmentId() + ", " + getResultsToDisplay().getMetricId() + "]");
    			...
    		ModelAndView mav = new ModelAndView( "testResults", "results", results );
    		return mav;
    	}
    }
    The interface:
    Code:
    public interface ResultCommandInterface {
    	public abstract int getEnvironmentId();
    	public abstract void setEnvironmentId(int environmentId);
    	public abstract void setMetricId(int metricId);
    	public abstract int getMetricId();
    }
    The object in question:
    Code:
    public class ResultCommand implements ResultCommandInterface {
    
    	private int environmentId;
    	private int metricId;
    
    	public ResultCommand() {
    	}
    	public int getEnvironmentId() {
    		return environmentId;
    	}
    	public void setEnvironmentId(int environmentId) {
    		this.environmentId = environmentId;
    	}
    	public void setMetricId(int metricId) {
    		this.metricId = metricId;
    	}
    	public int getMetricId() {
    		return metricId;
    	}
    }
    The Servlet.xml:
    Code:
    	<!-- Mapping  -->
    	<bean id="urlMapping"
    		class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    		<property name="mappings">
    			<props>
    				<prop key="/chooseTestResults.html">testResultsFormController</prop>
    				<prop key="/testResults.html">testResultsController</prop>
    			</props>
    		</property>
    	</bean>
    	<!-- MVC Controllers -->
    	<bean id="testResultsFormController" class="...TestResultsFormController">
    		<property name="formView" value="chooseTestResults" />
    		<property name="successView" value="testResults" />
    		<property name="commandName" value="resultCommand" />
    		<property name="commandClass" value="...ResultCommand" />
    		<property name="resultsToDisplay" ref="resultsToDisplay" />
    	</bean>
    
    	<bean id="testResultsController" class="...TestResultsController">
    		<property name="resultsToDisplay" ref="resultsToDisplay" />
    	</bean>
    	<bean id="resultsToDisplay" class="...ResultCommand" 
    		scope="session">
    	    <aop:scoped-proxy proxy-target-class="false"/>
    	</bean>
    	<!-- View Resolvers -->
    	<bean id="viewResolver"
    		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    		<property name="prefix">
    			<value>/</value>
    		</property>
    		<property name="suffix">
    			<value>.jsp</value>
    		</property>
    	</bean>
    And the view's jsp:
    HTML Code:
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    	<!--  refresh the form every 30 seconds  -->
    	<meta http-equiv="refresh" content="10;URL=testResults.html" />
    	...
    </head>
    ...
    What am I missing here?
    Last edited by craig.muir; Feb 24th, 2010, 07:05 PM.
Working...
X