📄 staticparametersinterceptor.java
字号:
/* * Copyright (c) 2002-2006 by OpenSymphony * All rights reserved. */package com.opensymphony.xwork2.interceptor;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.config.entities.ActionConfig;import com.opensymphony.xwork2.config.entities.Parameterizable;import com.opensymphony.xwork2.util.TextParseUtil;import com.opensymphony.xwork2.util.ValueStack;import java.util.Iterator;import java.util.Map;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;/** * <!-- START SNIPPET: description --> * * This interceptor populates the action with the static parameters defined in the action configuration. If the action * implements {@link Parameterizable}, a map of the static parameters will be also be passed directly to the action. * * <p/> Parameters are typically defined with <param> elements within xwork.xml. * * <!-- END SNIPPET: description --> * * <p/> <u>Interceptor parameters:</u> * * <!-- START SNIPPET: parameters --> * * <ul> * * <li>None</li> * * </ul> * * <!-- END SNIPPET: parameters --> * * <p/> <u>Extending the interceptor:</u> * * <!-- START SNIPPET: extending --> * * <p/>There are no extension points to this interceptor. * * <!-- END SNIPPET: extending --> * * <p/> <u>Example code:</u> * * <pre> * <!-- START SNIPPET: example --> * <action name="someAction" class="com.examples.SomeAction"> * <interceptor-ref name="static-params"> * <param name="parse">true</param> * </interceptor-ref> * <result name="success">good_result.ftl</result> * </action> * <!-- END SNIPPET: example --> * </pre> * * @author Patrick Lightbody */public class StaticParametersInterceptor extends AbstractInterceptor { private boolean parse; private static final Log LOG = LogFactory.getLog(StaticParametersInterceptor.class); public void setParse(String value) { this.parse = Boolean.valueOf(value).booleanValue(); } public String intercept(ActionInvocation invocation) throws Exception { ActionConfig config = invocation.getProxy().getConfig(); Object action = invocation.getAction(); final Map parameters = config.getParams(); if (LOG.isDebugEnabled()) { LOG.debug("Setting static parameters " + parameters); } // for actions marked as Parameterizable, pass the static parameters directly if (action instanceof Parameterizable) { ((Parameterizable) action).setParams(parameters); } if (parameters != null) { final ValueStack stack = ActionContext.getContext().getValueStack(); for (Iterator iterator = parameters.entrySet().iterator(); iterator.hasNext();) { Map.Entry entry = (Map.Entry) iterator.next(); stack.setValue(entry.getKey().toString(), entry.getValue()); Object val = entry.getValue(); if (parse && val instanceof String) { val = TextParseUtil.translateVariables((String) val, stack); } stack.setValue(entry.getKey().toString(), val); } } return invocation.invoke(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -