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

Liferay 7中Struts Portlet操作:添加Action Hook及路径注册与调用

在Liferay 7中配置Struts Portlet Action Hook及调用指南

我来分享一下在Liferay 7中配置Struts Portlet Action Hook以及调用的具体步骤,都是实际项目中验证过的方法:

一、添加新的Struts Portlet Action Hook

Liferay 7基于OSGi架构,所以我们需要用模块的方式来实现Struts Action Hook,步骤如下:

  • 1. 创建OSGi模块项目
    先在Liferay Workspace里新建一个OSGi模块(Gradle或Maven都可以),类型选“Generic”或者“Portlet”都行。然后在构建文件里添加必要的依赖,确保能引入Struts和Portlet相关的API:

    // build.gradle示例
    dependencies {
        compileOnly group: "com.liferay.portal", name: "com.liferay.portal.kernel", version: "3.0.0"
        compileOnly group: "com.liferay", name: "com.liferay.struts", version: "2.0.0"
        compileOnly group: "javax.portlet", name: "portlet-api", version: "2.0"
    }
    
  • 2. 实现自定义StrutsPortletAction
    创建一个类继承com.liferay.struts.BaseStrutsPortletAction,重写processAction方法来处理你的业务逻辑。比如:

    import com.liferay.struts.BaseStrutsPortletAction;
    import javax.portlet.ActionRequest;
    import javax.portlet.ActionResponse;
    import javax.portlet.PortletConfig;
    
    public class CustomStrutsAction extends BaseStrutsPortletAction {
        @Override
        public void processAction(
            PortletConfig portletConfig, ActionRequest actionRequest,
            ActionResponse actionResponse) throws Exception {
    
            // 这里处理你的业务逻辑,比如获取请求参数、调用后端服务
            String customParam = actionRequest.getParameter("customParam");
            // 示例:打印参数
            System.out.println("收到自定义参数:" + customParam);
    
            // 处理完后可以指定渲染的JSP页面
            actionResponse.setRenderParameter("mvcPath", "/success.jsp");
        }
    }
    
  • 3. 注册为OSGi组件
    给这个Action类加上OSGi的@Component注解,让Liferay能够识别并注册这个Struts Action:

    import org.osgi.service.component.annotations.Component;
    
    @Component(
        immediate = true,
        property = {
            "struts.path=/custom/my-action", // 这就是你的Action路径,后面调用要用
            "struts.type=action"
        },
        service = BaseStrutsPortletAction.class
    )
    public class CustomStrutsAction extends BaseStrutsPortletAction {
        // ... 上面的processAction代码
    }
    

    这里的struts.path就是我们要注册的Action路径,后续JSP调用时会用到这个值。

  • 4. 部署测试
    把模块部署到Liferay服务器,启动后OSGi容器会自动加载这个组件,你的Struts Action Hook就注册完成了。

二、注册struts-action路径及从JSP调用

注册路径

其实注册路径已经在上面的@Component注解里完成了,就是struts.path属性,这个值就是你的Action唯一标识路径。

从JSP调用对应的Action

有两种常见的调用方式,都是用Portlet标签库来生成合法的Action URL:

方式1:通过表单提交(推荐,适合POST请求)

在JSP里用<portlet:actionURL>生成Action URL,指定name为你的struts.path值,然后在表单里提交:

<%@ taglib prefix="portlet" uri="http://java.sun.com/portlet_2_0" %>

<portlet:actionURL var="customActionUrl" name="/custom/my-action">
    <!-- 可以传递自定义参数 -->
    <portlet:param name="customParam" value="hello-liferay" />
</portlet:actionURL>

<form action="${customActionUrl}" method="post">
    <button type="submit">触发自定义Struts Action</button>
</form>

方式2:通过链接调用(适合GET请求,注意Struts Action通常处理POST)

如果需要用链接触发,也可以这样写:

<portlet:actionURL var="customActionUrl" name="/custom/my-action">
    <portlet:param name="customParam" value="from-link" />
</portlet:actionURL>

<a href="${customActionUrl}">点击触发Action</a>

注意事项

  • 确保JSP所在的Portlet能访问到这个Action,如果需要全局可用,可以调整组件的属性(比如添加javax.portlet.name=*,不过不建议全局,按需设置)。
  • 处理完Action后,通过actionResponse.setRenderParameter可以指定跳转的JSP页面,或者传递参数给渲染阶段。
  • 如果遇到权限问题,检查Liferay的权限配置,确保当前用户有执行该Action的权限。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 07:46:49