如果我的动作类如下:
<!-- language: lang-java -->
package org.tutorial.struts2.action;
import java.util.Map;
import org.apache.struts2.interceptor.RequestAware;
import org.tutorial.struts2.service.TutorialFinder;
import com.opensymphony.xwork2.Action;
public class TutorialAction implements Action, RequestAware {
private String language;
private String bestTutorialSite;
public String execute() {
System.out.println(language);
setBestTutorialSite(new TutorialFinder().getBestTutorialSite(language));
System.out.println(bestTutorialSite);
if (getBestTutorialSite().contains("Java"))
return SUCCESS;
else
return ERROR;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
public String getBestTutorialSite() {
return bestTutorialSite;
}
public void setBestTutorialSite(String bestTutorialSite) {
this.bestTutorialSite = bestTutorialSite;
}
@Override
public void setRequest(Map<String, Object> requestObj) {
System.out.println(bestTutorialSite);
requestObj.put("message", bestTutorialSite);
}
}
在执行方法之前调用此操作时,该语言已由 Struts2 框架填充。在执行方法中
setBestTutorialSite
方法是填充私有字段bestTutorialSite
.
现在我想到将这个私有字段
bestTutorialSite
设置到请求属性中(在setRequest
方法中)。但是我注意到在填充任何私有字段(如语言)之前首先调用此方法。因此在setRequest
方法中,bestTutorialSite
的系统打印总是空的。
我以为我可以在调用 JSP 页面之前使用
bestTutorialSite
(从执行方法捕获)设置此属性。
我不认为我完全掌握了对 Struts2 流程的理解 - 显然! :OP
请帮忙。 谢谢。
我假设你正在使用 defaultStack 看起来像这样:
<interceptor-stack name="defaultStack">
<interceptor-ref name="exception"/>
<interceptor-ref name="alias"/>
<interceptor-ref name="servletConfig"/>
<interceptor-ref name="prepare"/>
<interceptor-ref name="i18n"/>
<interceptor-ref name="chain"/>
<interceptor-ref name="debugging"/>
<interceptor-ref name="profiling"/>
<interceptor-ref name="scopedModelDriven"/>
<interceptor-ref name="modelDriven"/>
<interceptor-ref name="fileUpload"/>
<interceptor-ref name="checkbox"/>
<interceptor-ref name="staticParams"/>
<interceptor-ref name="params">
<param name="excludeParams">dojo\..*</param>
</interceptor-ref>
<interceptor-ref name="conversionError"/>
<interceptor-ref name="validation">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
<interceptor-ref name="workflow">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
</interceptor-stack>
如您所见,servletConfig拦截器在params拦截器之前,这意味着第一个请求将设置在您的操作上(使用servletConfig),然后您的操作将填充请求参数(使用params)。
你想要实现的是改变拦截器的顺序,如果以错误的方式使用它可能是有害的。
我认为请求拦截器会先执行,然后再执行execute()方法。 这可能是问题所在。