Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Info

The example code for this tutorial, http_session, is available for checkout at httphttps://strutsgithub.com/apache.org/2.3.1.2/docs/junit-plugin.html/struts-examples

Introduction

Your Struts 2 application may need to access the HTTP session object. Struts 2 provides an interface, SessionAware, that your Action class should implement to obtain a reference to the HTTP session object.

...

Code Block
java
titleHelloWorldAction.java setSession Method
java


private Map<String, Object> userSession ;

public void setSession(Map<String, Object) session) {

   userSession = session ;

}

...

Code Block
java
titleHelloWorldAction.java increaseHelloCount Method
java


private void increaseHelloCount() {
			
   Integer helloCount = (Integer) userSession.get(HELLO_COUNT);
		
   if (helloCount == null ) {
		
     helloCount = 1;
			
   } else {
			
     helloCount++;

   }
		
   userSession.put(HELLO_COUNT, helloCount);
	
}


...

Code Block
html
titleHelloWorld.jsp Get helloCount Value From HTTP Session
html


   <p>I've said hello to you <s:property value="#session.helloCount" /> times!</p>

...

Code Block
java
titleHelloWorldAction.java acceptableParameterName Method
java


	public boolean acceptableParameterName(String parameterName) {
		
		boolean allowedParameterName = true ;
		
		if ( parameterName.contains("session")  || parameterName.contains("request") ) {
		
			allowedParameterName = false ;
			
		} 
		
		return allowedParameterName;
	}

...

Code Block
xml
titlestruts.xml configure params interceptor
xml


	<package name="basicstruts2" extends="struts-default">

 		<interceptors>
	 		<interceptor-stack name="appDefault">
	        	 <interceptor-ref name="defaultStack">
	      			<param name="exception.logEnabled">true</param>
	      			<param name="exception.logLevel">ERROR</param>
	      			<param name="params.excludeParams">dojo\..*,^struts\..*,^session\..*,^request\..*,^application\..*,^servlet(Request|Response)\..*,parameters\...*</param>
	   			</interceptor-ref>
	 	    </interceptor-stack>
		</interceptors>
		
		<default-interceptor-ref name="appDefault" />

...