java开源文档大全致力于打造中国最大最全的开源文档,它提供了最全面最权威的开源资料,同时为大家提供一个交流的平台,如果您有好的想法,欢迎您投稿.
使用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有跟深的认识。
java开源文档研究struts,webwork,spring,tomcat,jboss,lucense,nutch,JUnit,eclipse......,如果您有什么意见,欢迎评论和留言. |