中国java开源文档大全
java教程,java开源文档大全
java教程,java开源文档大全
首页 java基础 web开发框架 开发工具 应用系统 组件类库 搜索爬虫 J2EE服务器 持久层相关 测试工具 访客留言 投稿专栏 站内搜索
java教程,java开源文档大全
>首页 -> web开发框架 -> WebWork

TOP

webwork中使用JSTL
[ 录入者:admin | 时间:2007-11-30 14:41:42 | 作者: | 来源:原创 | 浏览:11次 ]
  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"});
//skip getClass,...

       
//Invoked after Action.execute() but before Result
       
//Calls all getters of the action and insert the values into the request
        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......,如果您有什么意见,欢迎评论和留言.
[下一篇]webwork标签的使用(最基本的三种.. [上一篇]关注WebWork

评论

称  呼:
内  容:

google

相关栏目

最新文章

热门文章

推荐文章

更多友情链接>>>