-->

Alfresco - HTTP Sessions

2019-08-07 03:34发布

问题:

Trying to access the HTTP session through Javascript Webscripts on QuickStart.

I am unable to find any help in saving elements to some private object for each http session.

Does anyone know of any solutions?

回答1:

  1. Create a custom JavaScript root object - it is a Java class which implements org.springframework.extensions.webscripts.processor.BaseProcessorExtension class, for example:

    package pl.test;
    
    import javax.servlet.http.HttpSession;
    
    import org.springframework.extensions.surf.ServletUtil;
    import org.springframework.extensions.webscripts.processor.BaseProcessorExtension;
    
    public class HttpSessionHelper extends BaseProcessorExtension {
    
    public void setInSession(String paramName, String paramValue) {
        HttpSession session = ServletUtil.getSession();
        session.setAttribute(paramName, paramValue);
    }
    
    public String getFromSession(String paramName) {
        HttpSession session = ServletUtil.getSession();
    
        Object paramValue = session.getAttribute(paramName);
        if (paramValue != null) {
            return paramValue.toString();
        } else {
            return null;
        }
    }
    }
    
  2. Compile and package this class to jar archive and copy it to Share (or Surf based application).

  3. Register this class in spring-surf-script-services-context.xml, for example:

    <bean id="exampleExtension" parent="baseScriptExtension" 
          class="pl.test.HttpSessionHelper">
      <property name="extensionName">
         <value>httpSessionHelper</value>
      </property>
    </bean>
    
  4. Use your new custom root object in webscript, for example:

    var paramName = httpSessionHelper.getFromSession('paramName');
    httpSessionHelper.setInSession('paramName','paramValue');