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

Struts2中如何通过Result注解在Action类重定向至不同命名空间

解决Struts2 Convention Plugin重定向Action时URL自动添加当前命名空间前缀的问题

问题分析

使用redirectAction类型的Result时,Struts2默认会基于当前Action的命名空间(此处为/pub)解析目标路径,导致即使指定了绝对路径,也会被自动拼接当前命名空间前缀。

解决方案

1. 在@Result中明确指定目标命名空间

修改@Result注解,通过params参数指定目标Action的命名空间,避免依赖当前命名空间拼接:

@Action(results = { 
    @Result(
        name = "success", 
        location = "complete-enrollment", 
        type = "redirectAction", 
        params = {"namespace", "/prot"}
    ) 
}, interceptorRefs = { @InterceptorRef("BasicStack") })
public class Enrollment extends ActionSupport {
    @Override
    public String execute() throws Exception {
        return "success";
    }
}

这里location仅需填写目标Action的名称(Convention插件会自动转换为短横线命名的URL),通过namespace="/prot"明确指定目标命名空间。

2. 改用redirect类型直接重定向到URL

若无需通过Action名称解析路径,可将Result类型改为redirect,直接跳转至目标URL:

@Action(results = { 
    @Result(
        name = "success", 
        location = "/prot/complete-enrollment", 
        type = "redirect"
    ) 
}, interceptorRefs = { @InterceptorRef("BasicStack") })
public class Enrollment extends ActionSupport {
    @Override
    public String execute() throws Exception {
        return "success";
    }
}

该方式不受当前命名空间影响,直接跳转至指定URL。

3. 调整Convention Plugin全局配置

在struts.xml中添加以下常量,强制使用绝对命名空间解析规则:

<constant name="struts.convention.action.relative.paths" value="false" />

此配置会让Convention插件优先识别绝对命名空间,避免相对路径拼接问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 02:45:55