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

Spring Portlet(Spring 4.1)在Liferay中sendRedirect失效问题

解决Spring Portlet 4.1中ActionMapping里sendRedirect不生效的问题

首先先纠正你代码里的一个小拼写错误:System.out.printIn应该是System.out.println,这个错误会导致你看不到日志输出,可能误以为方法没执行,先改过来哦。

回到重定向不生效的核心问题,这是由Portlet的生命周期机制导致的:Action请求处理完成后,容器默认会触发对应的Render阶段生成页面响应,这会覆盖你直接调用sendRedirect()的指令。下面给你两种可行的解决方案:

方案一:使用Spring Portlet原生重定向返回值

Spring Portlet支持在Action方法中返回带redirect:前缀的字符串,框架会自动帮你处理Portlet生命周期的重定向逻辑,阻止后续的Render请求,这是最符合Spring风格的做法:

@ActionMapping(param="action=redirectTest")
public String handleRedirectTest(ActionRequest request, ActionResponse response, Model model){
    System.out.println("the value coming");
    // 用Spring的重定向语法触发跳转
    return "redirect:https://www.google.com";
}

方案二:手动阻止Render阶段(保留sendRedirect调用)

如果你坚持要直接使用response.sendRedirect(),需要在调用后清除所有Render参数,明确告诉容器不要触发后续的Render阶段:

@ActionMapping(param="action=redirectTest")
public void handleRedirectTest(ActionRequest request, ActionResponse response, Model model){
    System.out.println("the value coming");
    response.sendRedirect("https://www.google.com");
    // 清除Render参数,阻止Render阶段执行,确保重定向生效
    response.setRenderParameters(null);
    System.out.println("the value coming");
}

额外注意事项

  • 确认请求确实触发了该ActionMapping:检查请求参数中是否包含action=redirectTest,可以在方法开头加日志验证。
  • 若使用相对URL重定向,要注意Portlet的命名空间和上下文路径,避免出现路径错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 08:02:10