У меня есть следующее сопоставление действий:
...
one
two
...
nth-number
...
Я могу получить карту параметров, используя следующую строку в Interceptor:
Map params = ActionContext.getContext().getParameters();
Как и выше, есть ли какой-либо способ получить параметры перехватчика, как определено в следующем сопоставлении:
...
one
two
...
nth-number
...
Параметры действия определяются следующим образом: параметры действия и параметры перехватчика должны быть доступны отдельно.
...
one
two
...
nth-number
....
one
two
...
nth-number
...
Обратите внимание, что я не хочу объявлять поля параметров в своем перехватчике как
//all fields with their getters and setters
private String param1;
private String param2;
...
private String paramN;
После ответа Дева Бланкеда я применил его технику. Это не сработало, поэтому делюсь своим кодом здесь. Я использую Struts 2.3.1.2.
Библиотеки
[*]asm-3.3.jar
[*]asm-commons-3.3.jar
[*]asm-tree-3.3.jar
[*]commons-fileupload -1.2.2.jar
[*]commons-io-2.0.1.jar
[*]commons-lang-2.5.jar
[*]freemarker-2.3.18.jar
[*]javassist-3.11.0.GA.jar
[*]ognl-3.0.4.jar
[*]struts2-core-2.3.1.2.jar
[*]xwork-core-2.3.1.2.jar
struts.xml:
/the-action.jsp
no-store,no-cache
no-cache
-1
true
Перехватчик:
package demo.interceptors;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.StrutsStatics;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class HttpHeaderInterceptor extends AbstractInterceptor {
private final Map interceptorConfigs = new HashMap();
@Override
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("Calling 'intercept' method.");
HttpServletResponse response = (HttpServletResponse) invocation.getInvocationContext().get(StrutsStatics.HTTP_RESPONSE);
for(Entry entry: interceptorConfigs.entrySet()) {
String header = entry.getKey();
String value = entry.getValue();
System.out.printf("Adding header: %s=%s\n",header,value);
response.setHeader(header, value);
}
return invocation.invoke();
}
public Map getInterceptorConfigs() {
System.out.println("calling method 'getInterceptorConfigs'");
return interceptorConfigs;
}
public void addInterceptorConfig(final String configName, final String configValue) {
System.out.printf("Calling method 'addInterceptorConfig' with params configName = %s, configValue=%.\n",configName, configValue);
interceptorConfigs.put(configName, configValue);
}
}
Вывод в консоль: (при нажатии на Action)
Calling 'intercept' method.
Подробнее здесь: https://stackoverflow.com/questions/164 ... n-struts-2
Как получить параметры перехватчика в Struts 2 ⇐ JAVA
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Настройка диспетчера: FORWARD превращает struts.actionMapping в noActionMapping в Struts 2.
Anonymous » » в форуме JAVA - 0 Ответы
- 24 Просмотры
-
Последнее сообщение Anonymous
-