gpt4 book ai didi

java - 与 Action 和拦截器不同的字符串

转载 作者:行者123 更新时间:2023-12-02 05:52:15 24 4
gpt4 key购买 nike

<%@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;
}


}

我的拦截器:

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>

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

最佳答案

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

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

特别要注意大框中带有感叹号的文本:

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.

关于java - 与 Action 和拦截器不同的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23439271/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com