Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to get session attribute in velocity template in liferay

    • 0
    • 2
    • 1
    • 1
    • 2
    • 0
    • 0
    • 0
    • 3.64k
    Comment on it

    Hello Guys,

    Here, I am writing code for get session attribute in velocity. First of all set session attribute in view.jsp or class of the portlet then get session attribute in velocity template.

    Follow below steps to implement.

    step : 1 put below code in jsp or class to set session attribute.

    JSP :

    request.getSession().setAttribute("name", "bhagwan");
    

    Class :

    HttpServletResponse request = PortalUtil.getHttpServletResponse(actionResponse);
    HttpSession session = request.getSession();
    session.setAttribute("name", "bhagwan");
    

    step : 2 put below code in velocity template to get session attribute.

     #set ($name = $request.getSession().getAttribute("name"))
                 <span> $name</span> 
    

    Thank you.

 2 Comment(s)

  • Hi Please check liferay-portlet.xml as below :

    <portlet>
            <portlet-name>dashboard</portlet-name>
            <icon>/icon.png</icon>
            <private-session-attributes>false</private-session-attributes>
            <header-portlet-css>/css/main.css</header-portlet-css>
            <footer-portlet-javascript>
                /js/main.js
            </footer-portlet-javascript>
            <css-class-wrapper>dashboard-portlet</css-class-wrapper>
            <add-default-resource>true</add-default-resource>
        </portlet>
    

    Private session attribute should be false.

    <private-session-attributes>false</private-session-attributes>
    
  • Hi can you please tell me Fetch the session object set in liferay theme java method into portlet doView() method

    i have a below method in theme:

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)throws IOException,             ServletException {
      ((HttpServletRequest) request).getSession().setAttribute("test", "abc");
       }
    

    i am trying to fetch above session object in doView method of portlet.

    HttpSession httpSession =PortalUtil.getHttpServletRequest(renderRequest).getSession();
    System.out.println("httpSession obj"+(String)httpSession.getAttribute("test"));
    

    i even tried using as below :

    HttpServletRequest request1 =PortalUtil.getHttpServletRequest(renderRequest);
    HttpServletRequest originalRequest = PortalUtil.getOriginalServletRequest(request1);
    HttpSession httpsession = originalRequest.getSession();
    System.out.println("httpSession ==> " + httpsession);//session object gets printed
    System.out.println("test "+httpsession.getAttribute("test"));//null
    

    i get the null value when i try to fetch from doview method. Can anyone please suggest how to fetch the value from theme to portal

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: