gpt4 book ai didi

java - 从 Struts1 迁移到 Struts2

转载 作者:行者123 更新时间:2023-11-30 03:16:31 25 4
gpt4 key购买 nike

我正在将应用程序从 Struts 1 迁移到 Struts 2。我遇到了以下代码片段。请让我知道如何替换 Struts 2 中的代码片段。

protected ActionForward getActionForward(FilterContext ctx, String key, boolean redirect) {
HashMap filterForwards = ctx.getFilterForwards();
String forwardPage = (String)filterForwards.get(key);
if(forwardPage == null)
return null;
return new ActionForward(forwardPage, redirect);
}

另一个代码片段是这样的:-

protected void setError(HttpServletRequest req, String msg) {
ActionMessages errors = new ActionMessages();
errors.add("exception", new ActionMessage(MSG_KEY, msg));
req.setAttribute(Globals.ERROR_KEY, errors);
}

我应该用 addActionError(msg) 替换上面的代码吗?

最佳答案

在 Struts 1 中,您应该从 execute 方法返回 ActionForward。 Struts 2 返回String 类型的结果代码。因此,预期 ActionForward 的代码应替换为结果代码。操作结果应配置为操作,就像在 Struts 1 中配置转发一样。

创建两个结果配置:一个是 redirectAction 结果类型,另一个是 dispatcher 结果类型。像这样

<result name "redirect" type="redirectAction">${forwardPage}</result>
<result>${forwardPage}</result>

代码应替换为

private String forwardPage; 

public String getForwardPage() { return forwardPage; }

public void setForwardPage(String forwardPage) {
this.forwardPage = forwardPage;
}

protected String getActionForward(FilterContext ctx, String key, boolean redirect) {
HashMap filterForwards = ctx.getFilterForwards();
String forwardPage = (String)filterForwards.get(key);
if(forwardPage == null)
return NONE;
if (redirect) {
setForwardPage(forwardPage);
return "redirect";
} else {
setForwardPage(forwardPage)
return SUCCESS;
}
}

错误由您的操作应继承的 ActionSupport 类提供。然后就可以使用代码了

protected void setError(String msg) {
addActionError(getText("exception", new Object[]{msg}));
}

在 JSP 中,您可以使用

显示错误
<s:actionerror/>

关于java - 从 Struts1 迁移到 Struts2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32434941/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com