webwork中使用JSTL
lhf_z 发表于 2005-6-8 11:56:00

参考: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"});
//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有跟深的认识。。


阅读全文() | 回复(0) | 引用通告() | 编辑

发表评论:[拥有一个自己的Blog?]

    昵称:
    密码: (游客无须输入密码)
    主页:
    标题:
    处理 SSI 文件时出错


June 2005
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30


  公告

我的blog,他很帅。。

过节前是最难熬得。。


  我的分类

    


  最新日志

  最新回复

  留言本

  BLOG信息

   

  • 日志:34
  • 评论:2
  • 留言:3
  • 访问:



  我的链接