2

I am implementing SessionAware in my action class. But, the sessionMap is always null. The setSession method doesn't seem to be called. Here is the code. In the execute method, the sessionMap is always null.

What am I doing wrong?

Action class:

public class HelloWorldAction extends ActionSupport implements SessionAware
{
    private static final long serialVersionUID = 544659976107736338L;
    private Map<String, Object> sessionMap;
    
    private String name;

       public String execute() throws Exception {
    
           if(sessionMap != null) {
               sessionMap.put("TestID", "Test");
           }
           return "success";
       }
       
       public String getName() {
          return name;
       }

       public void setName(String name) {
          this.name = name;
       }

    @Override
    public void setSession(Map<String, Object> arg0) {
        this.sessionMap = arg0;
    }
    }

struts xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
   "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
   <include file="struts-default.xml"/>
        
 <package name="my-projects" namespace="/" extends="struts-default">

      <action name="hello" 
            class="com.my.projects.actions.HelloWorldAction" 
            method="execute">
            <result name="success">/jsp/HelloWorld.jsp</result>
      </action>      
       
Roman C
  • 49,761
  • 33
  • 66
  • 176

2 Answers2

0

The struts-default.xml is the name of the default configuration file used by the Struts in it's core library. You should not use this name to your custom configurations.

A base configuration file named struts-default.xml is included in the struts2-core.jar file. This file is automatically included into struts.xml file to provide the standard configuration settings without having to copy them.

To exclude the struts-default.xml or to provide your own version, see the struts.configuration.files setting in struts.properties.

Remove this line from the configuration:

<include file="struts-default.xml"/> 

This file can override the default settings such as the interceptor stack and your application won't work in the default mode.

By default it's configured to populate the session to the actions that implement SessionAware.

Roman C
  • 49,761
  • 33
  • 66
  • 176
0

You can try this to get Session Map

Map session = ActionContext.getContext().getSession();

and remove below line

<include file="struts-default.xml"/>
Saurabh Ande
  • 427
  • 3
  • 13