问题
<%@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>
Action file:
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>
My question is that I am returning a different string from the interceptor than that of the action file, still the view of action is being generated using action string to map result, and not of the interceptor why?
回答1:
You're still invoking the action. The JSP is rendered by the time the intercept call finished. This is why the PreResultListener
interface exists.
http://struts.apache.org/development/2.x/docs/writing-interceptors.html
In particular, note the text in the big box with the exclamation point in it:
Keep in mind that invoke will return after the result has been called (eg. after you JSP has been rendered), making it perfect for things like open-session-in-view patterns. If you want to do something before the result gets called, you should implement a PreResultListener.
回答2:
The string returned by the interceptor is the result code, which is used by the framework to build and execute result with such name configured to the action config. You have used
<result name="Myjsp">/MyJsp.jsp </result>
So, this result is available to the interceptor, which is working on action invocation and returns a result code. The interceptor stack continues invocation (and at the top action is executed) if you call actionInvocation.invoke()
, which returns a result code from the invoked stack. You can also stop invoking a stack and return a result code immediately.
来源:https://stackoverflow.com/questions/23439271/different-string-from-action-and-interceptor