WW-3804 adds support of dynamic attributes to <s:radio/> tag and extends support for dynamic attributes with expression evaluation

git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@1328526 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Lukasz Lenart
2012-04-20 22:19:41 +00:00
parent 5a9048065f
commit 0d8f4d43bd
6 changed files with 62 additions and 16 deletions
@@ -25,6 +25,7 @@ import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ObjectFactory;
import com.opensymphony.xwork2.inject.Container;
import com.opensymphony.xwork2.util.ClassLoaderUtil;
import com.opensymphony.xwork2.util.TextParseUtil;
import com.opensymphony.xwork2.util.ValueStack;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;
@@ -148,6 +149,10 @@ public class StrutsUtil {
return (request == null)? "" : request.getContextPath();
}
public String translateVariables(String expression) {
return TextParseUtil.translateVariables(expression, stack);
}
/**
* the selectedList objects are matched to the list.listValue
* <p/>
@@ -23,6 +23,8 @@
<#if (parameters.dynamicAttributes?? && parameters.dynamicAttributes?size > 0)><#rt/>
<#assign aKeys = parameters.dynamicAttributes.keySet()><#rt/>
<#list aKeys as aKey><#rt/>
${aKey}="${parameters.dynamicAttributes[aKey]?html}"<#rt/>
<#assign keyValue = parameters.dynamicAttributes[aKey]/>
<#assign value = struts.translateVariables(keyValue)!keyValue/>
${aKey}="${value?html}"<#rt/>
</#list><#rt/>
</#if><#rt/>
@@ -61,6 +61,7 @@
</#if>
<#include "/${parameters.templateDir}/simple/scripting-events.ftl" />
<#include "/${parameters.templateDir}/simple/common-attributes.ftl" />
<#include "/${parameters.templateDir}/simple/dynamic-attributes.ftl" />
/><#rt/>
<label for="${parameters.id?html}${itemKeyStr?html}"><#rt/>
${itemValue}<#t/>
@@ -21,24 +21,19 @@
package org.apache.struts2.util;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.util.ValueStack;
import org.apache.struts2.StrutsTestCase;
import org.apache.struts2.TestAction;
import org.apache.struts2.util.ListEntry;
import org.apache.struts2.util.StrutsUtil;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockRequestDispatcher;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.util.ValueStack;
import com.opensymphony.xwork2.util.ValueStackFactory;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.util.ArrayList;
import java.util.List;
/**
* Test case for StrutsUtil.
@@ -184,6 +179,18 @@ public class StrutsUtilTest extends StrutsTestCase {
assertEquals(strutsUtil.toString(11l), "11");
}
public void testTranslateVariables() throws Exception {
stack.push(new Object() {
public String getFoo() {
return "bar";
}
});
Object obj1 = strutsUtil.translateVariables("try: %{foo}");
assertNotNull(obj1);
assertTrue(obj1 instanceof String);
assertEquals(obj1, "try: bar");
}
// === Junit Hook
@@ -21,14 +21,14 @@
package org.apache.struts2.views.jsp.ui;
import org.apache.struts2.TestAction;
import org.apache.struts2.views.jsp.AbstractUITagTest;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;
import org.apache.struts2.TestAction;
import org.apache.struts2.views.jsp.AbstractUITagTest;
/**
*/
@@ -175,6 +175,30 @@ public class RadioTest extends AbstractUITagTest {
verifyGenericProperties(tag, "xhtml", new String[]{"id","value"});
}
public void testDynamicAttributes() throws Exception {
TestAction testAction = (TestAction) action;
testAction.setFoo("bar");
testAction.setList(new String[][]{
{"hello", "world"},
{"foo", "bar"}
});
RadioTag tag = new RadioTag();
tag.setPageContext(pageContext);
tag.setLabel("mylabel");
tag.setName("myname");
tag.setValue("");
tag.setList("list");
tag.setListKey("top[0]");
tag.setListValue("top[1]");
tag.setDynamicAttribute(null, "dojo", "checked: %{top[0]}");
tag.doStartTag();
tag.doEndTag();
verify(RadioTag.class.getResource("Radio-7.txt"));
}
private void prepareTagGeneric(RadioTag tag) {
TestAction testAction = (TestAction) action;
testAction.setFoo("bar");
@@ -0,0 +1,7 @@
<tr>
<td class="tdLabel"><label for="myname" class="label">mylabel:</label></td>
<td>
<input type="radio" name="myname" id="mynamehello" value="hello" dojo="checked:hello"/><label for="mynamehello">world</label>
<input type="radio" name="myname" id="mynamefoo" value="foo" dojo="checked:foo"/><label for="mynamefoo">bar</label>
</td>
</tr>