You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Liferay Struts Action(DXP7.1)在浏览器显示JSP内容?

Liferay DXP 7.1中Struts Action无法显示JSP的解决方案

问题背景

在Liferay DXP 7.1环境下,使用Scala+SBT构建项目,尝试通过Struts Action加载resources/META-INF/resources目录下的JSP文件,但浏览器无内容显示,日志也无报错。曾参考6.x版本的Struts重写文档配置struts-config.xml无效,现尝试requestDispatcher方案但未生效。

当前代码:

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")
    }
    ""
  }
}

解决步骤

1. 修正ServletContext获取方式

Liferay 7.x是OSGi模块化环境,每个模块拥有独立的ServletContext,从request的session获取的是Portal全局的ServletContext,无法访问模块内部的META-INF/resources资源。需通过模块符号名获取自身的ServletContext:

// 替换为你的模块符号名,可在build.sbt中定义
val moduleSymbolicName = "com.yourcompany.your.module"
val moduleServletContext = PortalUtil.getServletContext(moduleSymbolicName)
val requestDispatcher = moduleServletContext.getRequestDispatcher("/page.jsp")
requestDispatcher.include(request, response)

2. 用OSGi注解注册Struts Action(替代6.x的struts-config.xml)

Liferay 7.x不再依赖传统的struts-config.xml配置Struts Action,需通过OSGi组件注解完成注册:

import org.osgi.service.component.annotations.Component
import com.liferay.portal.kernel.struts.StrutsAction

@Component(
  immediate = true,
  property = Array(
    "path=/custom/redirect-info" // 替换为你要映射的Action访问路径
  ),
  service = Array(classOf[StrutsAction])
)
class RedirectInfoAction extends BaseStrutsAction with LiferayLogSupport {
  // 保留原execute方法逻辑
}

3. 验证JSP打包路径

确认SBT构建时已将page.jsp正确打包到模块JAR的META-INF/resources目录下。可解压生成的JAR包,检查文件位置是否符合预期。

4. 响应完整性检查

确保include操作执行后,没有其他代码修改或关闭响应流。当前代码返回空字符串是正确的,因为已经通过include直接向响应写入内容。

内容的提问来源于stack exchange,提问作者wexsaz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 17:40:17