参考:http://wiki.opensymphony.com/display/WW/Using+WebWork2+and+XWork1+with+JSP+2.0+and+JSTL+1.1
使用webwork,action返回页面后以下内容不放入request中:
actionMessages
locale
webwork.valueStack
errors
webwork.request_uri
model
fieldErrors
使用webwork中的拦截机制,在action返回页面后将必要的内容放入request
Interceptor代码如下:
package interceptors;
import com.opensymphony.webwork.WebWorkStatics;
import com.opensymphony.xwork.Action;
import com.opensymphony.xwork.ActionInvocation;
import com.opensymphony.xwork.interceptor.AroundInterceptor;
import com.opensymphony.xwork.interceptor.PreResultListener;
import org.apache.commons.beanutils.PropertyUtils;
import javax.servlet.http.HttpServletRequest;
import java.beans.PropertyDescriptor;
import java.util.*;
/**
* Populates HTTP Request Attributes with all gettable properties of the current action.
*/
public class ActionPropertyExportInterceptor extends AroundInterceptor {
protected void before(ActionInvocation invocation) throws Exception {
invocation.addPreResultListener( new PropertyExporter() );
}
protected void after(ActionInvocation dispatcher, String result) throws Exception { }
public static class PropertyExporter implements PreResultListener {
private static final List ignore = Arrays.asList(new String[] {"class", "texts"});
public void beforeResult(ActionInvocation invocation, String resultCode) {
Map props = extractGetterPropertyValues( invocation.getAction() );
HttpServletRequest request = getRequest(invocation);
for (Iterator it = props.entrySet().iterator(); it.hasNext();) {
Map.Entry e = (Map.Entry) it.next();
request.setAttribute((String) e.getKey(), e.getValue());
}
}
public Map extractGetterPropertyValues(Object bean) {
PropertyDescriptor[] descr = PropertyUtils.getPropertyDescriptors(bean);
Map props = new HashMap();
for (int i = 0; i < descr.length; i++) {
PropertyDescriptor d = descr[i];
if (d.getReadMethod() == null) continue;
if (ignore.contains(d.getName())) continue;
try {
props.put(d.getName(), PropertyUtils.getProperty(bean, d.getName()));
} catch (Exception e) { }
}
return props;
}
public HttpServletRequest getRequest(ActionInvocation invocation) {
return (HttpServletRequest) invocation.getInvocationContext().get(WebWorkStatics.HTTP_REQUEST);
}
}
}
代码中使用了Jakarta BeanUtils
然后在配置文件中定义这个interceptor,将定义后的interceptor加入action配置文件
虽然这有点多此一举,直接用webwork的标签会更方便(我不会用~~~),但设置好后对webwork的interceptor有跟深的认识。。