Search code examples
liferayliferay-7

Display content of JSP file in browser through Liferay Struts Action


How to display the content of jsp file in browser through Liferay Struts Action (DXP 7.1)? I use the execute method with parameters of HttpServletRequest and HtttpServletResponse, and place the jsp file in resources/META-INF/resources, but it is not showing in the browser, also no any errors in microservice logs.

The project, where I'm implementing new functionality, use Scala and SBT, so I can't use maven or something else.

I tried to follow the official documentation of overriding Liferay Struts action on their page, but it describe case for DXP 6.x version, despite this, I still tried the described approach using struts-config.xml, but it did not help.

The latest iteration of implementation based on of using requestDispatcher, but I'm not sure this is right:

class RedirectInfoAction extends BaseStrutsAction with LiferayLogSupport {

  override def execute(
    originalStrutsAction: StrutsAction,
    request: HttpServletRequest,
    response: HttpServletResponse
  ): String = {

    log.info("Strut action execution started...")

    getUserId(request) match {
      case Some(_) =>

        val servletContext = request.getSession().getServletContext

        val requestDispatcher = servletContext.getRequestDispatcher("/page.jsp")

        requestDispatcher.include(request, response)

      case None =>
        response.sendError(401, "The user is not authorized")
    }
    ""
  }

Solution

  • It all depends on how you deploy the code: You're just posting the code here, but if you look at the blade sample for a struts action, you'll see an additional critical part, which is the @Component section (granted, this requires using Java), as well as the @Reference to the servlet context.

    You can also manually register such a service to the OSGi runtime, but that requires quite a lot of internal OSGi knowledge.

    @Component(
        immediate = true, property = "path=/portal/blade",
        service = StrutsAction.class
    )
    public class BladeStrutsAction extends BaseStrutsAction {
    
        public String execute(
                HttpServletRequest httpServletRequest,
                HttpServletResponse httpServletResponse)
            throws Exception {
    
            RequestDispatcher requestDispatcher =
                _servletContext.getRequestDispatcher("/html/portal/blade.jsp");
            requestDispatcher.forward(httpServletRequest, httpServletResponse);
    
            return null;
        }
    
        @Reference(target = "(osgi.web.symbolicname=blade.strutsaction)")
        private volatile ServletContext _servletContext;
    }
    

    (see the rest of the project as well: There's a bnd.bnd that contains information for the osgi.web.symbolicname configuration, etc)