[英]Migrating from Struts1 to Struts2
我正在将应用程序从Struts 1迁移到Struts2。我遇到了以下代码片段。 请让我知道如何替换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/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.