Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 4.0
Info

The example code for this tutorial, http_session, is available for checkout at http://struts.apache.org/2.3.1.2/docs/junit-plugin.htmlImage Removed

Introduction

...

The SessionAware interface has one method, setSession, that your Action class will need to override. In the example application (see above), the HelloWorldAction class implements the SessionAware interface and includes this code:

java
Code Block
java
titleHelloWorldAction.java setSession Method
java

private Map<String, Object> userSession ;

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

   userSession = session ;

}

...

The example application keeps track of how many times the user clicks on a Hello link or submits the hello form. It stores this count in the HTTP session object in the increaseHelloCount method.

Code Block
javajava
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);
	
}


...

Struts 2 provides an easy way to get an object stored in the HTTP session from within the view page. In the example application is HelloWorld.jsp with this markup:

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

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

...

  1. Do not have a public Map<String, Object) getSession method in the Action class. You only need a public void setSession method to implement the SessionAware interface.
  2. Also have the Action class implement the ParameterNameAware interface and override its acceptableParameterName method:
Code Block
javajava
titleHelloWorldAction.java acceptableParameterName Method
java

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

...

Instead of having each action that implements SessionAware also implement the ParameterNameAware interface you can tell the params interceptor to exclude specific request attributes for all actions in a package. In struts.xml configure the struts-default set of interceptors as follows:

Code Block
xmlxml
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" />

...