来自动作和拦截器的不同字符串

时间:2014-05-03 01:32:49

标签: java jsp configuration struts2 struts2-interceptors

<%@taglib uri="/struts-tags" prefix="s"%>
</s:form>
<br>
<b>Interceptor test</b>
<s:form action="simple">
<s:textfield name="message" label="message"/>
<s:submit value="submit"/>
</s:form>

操作文件:

package action;

public class Simple {
    String message,Status="action is not invoked";
    public String execute() throws Exception
    {
        Status="action is invoked";
        return "Success";
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
    public String getStatus() {
        return Status;
    }


}

Myinterceptor:

package interceptors;

import java.util.Enumeration;

import javax.servlet.ServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
import com.opensymphony.xwork2.util.ValueStack;

public class myinterceptor implements Interceptor {

    @Override
    public void destroy() {
        // TODO Auto-generated method stub

    }

    @Override
    public void init() {
        // TODO Auto-generated method stub

    }

    @Override
    public String intercept(ActionInvocation ae) throws Exception {
    //preprocessing logic
        ServletRequest req=ServletActionContext.getRequest();
        ValueStack v=ae.getStack();
        Enumeration<String> e=req.getParameterNames();
        while(e.hasMoreElements())
        {
            String pname=e.nextElement();
            String pvalue=req.getParameter(pname);

            v.setValue(pname, pvalue);
        }
        // get the next compponent invoked
        String str=ae.invoke();
        return "Myjsp";

    }

}

result.jsp

<%@taglib uri="/struts-tags" prefix="s"%>
<b>Result is:<s:property value="result"/></b>
<br/>
<jsp:include page="index.jsp"></jsp:include>

MyJsp.jsp

<b>notworking</b>

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC 
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
    <struts>
    <package name="action">
    <result-types>
    <result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>

    </result-types>
    <interceptors>
    <interceptor name="params" class="com.opensymphony.xwork2.interceptor.ParametersInterceptor"/>
    <interceptor name="ps" class="interceptors.myinterceptor"></interceptor>
    </interceptors>
    <default-interceptor-ref name="params"></default-interceptor-ref>
    <action name="adder" class="action.AdderAction">

    <result name="success" >/result.jsp</result>
    </action>
    <action name="simple" class="action.Simple">
    <interceptor-ref name="ps"/>
    <result name="Success">/status.jsp</result> 
    <result name="Myjsp">/MyJsp.jsp </result>
    </action>
    </package>
    </struts>

我的问题是我从拦截器返回的字符串不同于动作文件的字符串,仍然使用动作字符串生成动作视图来映射结果,而不是拦截器为什么?

2 个答案:

答案 0 :(得分:2)

您仍在调用此操作。在拦截调用完成时呈现JSP。这就是PreResultListener接口存在的原因。

http://struts.apache.org/development/2.x/docs/writing-interceptors.html

请特别注意大框中带有感叹号的文字:

  

请记住,在调用结果之后(例如,在渲染JSP之后)将返回invoke,这使得它非常适合开放式会话视图模式。如果要在调用结果之前执行某些操作,则应实现PreResultListener。

答案 1 :(得分:0)

拦截器返回的字符串是结果代码,框架使用它来构建和执行结果,并将此名称配置为操作配置。你用过了

<result name="Myjsp">/MyJsp.jsp </result>

因此,此结果可供拦截器使用,拦截器正在处理动作调用并返回结果代码。如果调用actionInvocation.invoke(),拦截器堆栈将继续调用(并执行顶部操作),该调用将从调用的堆栈返回结果代码。您也可以停止调用堆栈并立即返回结果代码。