0

我在我的 java web 项目中使用 struts 2。我想将 Action 成功重定向到另一个 jsp,但它不起作用。这是我的代码:

public class ConfigurerDureeStockageAction extends ActionSupport implements ServletRequestAware{
private final static Logger logger = Logger.getLogger(ConfigurerDureeStockageAction.class);
    private static final long serialVersionUID = 1L;

    private HttpServletRequest request;

    public void setServletRequest(HttpServletRequest request){
            this.request = request;
        }

    public String execute()throws Exception{
        logger.debub("Execute SUCCESS");
        return SUCCESS;

        }

        public String savePeriod(){
            logger.debub("savePeriod SUCCESS");
            return SUCCESS;
        }

}

我的 struts.xml 是

<action name="AdminIndex" class="fr.si2m.occ.web.actions.admin.AdminIndexAction"
            method="execute">
            <result name="success">adminIndex.jsp</result>
        </action>
    <action name="ConfigurerDureeStockageSave" class="fr.si2m.occ.web.actions.admin.ConfigurerDureeStockageAction"
                method="savePeriod">
                <result name="success">adminIndex.jsp</result>
            </action>

我试试这个

<action name="ConfigurerDureeStockageSave" class="fr.si2m.occ.web.actions.admin.ConfigurerDureeStockageAction"
            method="savePeriod">
            <result name="success" type="redirect"> 
             <param name="location">adminIndex.jsp</param >
            </result>
        </action>

和这个

<action name="ConfigurerDureeStockageSave" class="fr.si2m.occ.web.actions.admin.ConfigurerDureeStockageAction"
            method="savePeriod">
            <result name="success" type="redirect">
            <param name="actionName">AdminIndex</param>
            </result>
        </action>

最后一个配置给了我以下错误:

Caught OgnlException while setting property 'actionName' on type 'org.apache.struts2.dispatcher.ServletRedirectResult'.
Caught OgnlException while setting property 'actionName' on type 'org.apache.struts2.dispatcher.ServletRedirectResult'. - Class: ognl.ObjectPropertyAccessor
File: ObjectPropertyAccessor.java
Method: setProperty
Line: 166 - ognl/ObjectPropertyAccessor.java:166:-1
    at com.opensymphony.xwork2.ognl.OgnlUtil.internalSetProperty(OgnlUtil.java:430)
    at com.opensymphony.xwork2.ognl.OgnlUtil.setProperty(OgnlUtil.java:160)
    at com.opensymphony.xwork2.ognl.OgnlReflectionProvider.setProperty(OgnlReflectionProvider.java:91)
    at com.opensymphony.xwork2.ObjectFactory.buildResult(ObjectFactory.java:233)
    at com.opensymphony.xwork2.DefaultActionInvocation.createResult(DefaultActionInvocation.java:221)
    at com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:368)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:278)
    at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265)
    at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:236)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:236)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:90)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:243)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:192)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)
    at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:511)
    at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
    at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)
    at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
    at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
    at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:598)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    at java.lang.Thread.run(Thread.java:662)
Caused by: ognl.NoSuchPropertyException: org.apache.struts2.dispatcher.ServletRedirectResult.actionName
    at ognl.ObjectPropertyAccessor.setProperty(ObjectPropertyAccessor.java:166)
    at com.opensymphony.xwork2.ognl.accessor.ObjectAccessor.setProperty(ObjectAccessor.java:27)
    at ognl.OgnlRuntime.setProperty(OgnlRuntime.java:2315)
    at ognl.ASTProperty.setValueBody(ASTProperty.java:127)
    at ognl.SimpleNode.evaluateSetValueBody(SimpleNode.java:220)
    at ognl.SimpleNode.setValue(SimpleNode.java:301)
    at ognl.Ognl.setValue(Ognl.java:737)
    at com.opensymphony.xwork2.ognl.OgnlUtil.setValue(OgnlU
12:12:06,608 INFO  [STDOUT] til.java:217)
    at com.opensymphony.xwork2.ognl.OgnlUtil.setValue(OgnlUtil.java:209)
    at com.opensymphony.xwork2.ognl.OgnlUtil.internalSetProperty(OgnlUtil.java:423)
    ... 72 more

总是不工作。我的问题是:1-为什么配置<result name="success">adminIndex.jsp</result>不起作用?2- 为什么配置

<result name="success" type="redirect"> 
                 <param name="location">adminIndex.jsp</param >
                </result>

不工作?3-为什么我收到配置错误

<result name="success" type="redirect">
            <param name="actionName">AdminIndex</param>
            </result>

? AdminIndex 操作在 struts.xml 中声明。我很困惑。有人可以帮助我吗?

4

1 回答 1

2

您的问题是您不应该使用 property <param name="actionName">AdminIndex</param>,这是解决方案:

<result name="success" type="redirectAction">
    <param name="actionName">AdminIndex</param>
</result>

关于文档Struts2 Redirect Action Result Type,您应该redirectAction改用redirect

回答您的问题:

  1. 请给我们更多的细节。

  2. 试试这个(假设你的命名空间配置是正确的):

    <action name="ConfigurerDureeStockageSave" class="fr.si2m.occ.web.actions.admin.ConfigurerDureeStockageAction" method="savePeriod">
        <result name="success" type="redirect"> 
            <param name="location">adminIndex.action</param >
        </result>
    </action>
    

    或者

    <action name="ConfigurerDureeStockageSave" class="fr.si2m.occ.web.actions.admin.ConfigurerDureeStockageAction" method="savePeriod">
        <result name="success"> 
            <param name="location">adminIndex.jsp</param >
        </result>
    </action>
    
  3. 因为结果类型redirect没有属性actionName,所以你应该使用redirectAction结果类型,它有这个属性

于 2012-09-19T10:40:06.193 回答