Versions Compared

Key

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

...

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
JAVAjavaJAVA
java
1titleHelloWorldAction.java increaseHelloCount Method

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
XML
XML
1titleHelloWorld.jsp Get helloCount Value From HTTP Session

   <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
JAVAjavaJAVA
java
1titleHelloWorldAction.java acceptableParameterName Method

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

...