Skip to content

Commit

Permalink
WW-3804 adds support of dynamic attributes to <s:radio/> tag and exte…
Browse files Browse the repository at this point in the history
…nds 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
  • Loading branch information
lukaszlenart committed Apr 20, 2012
1 parent 5a90480 commit 0d8f4d4
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 16 deletions.
5 changes: 5 additions & 0 deletions core/src/main/java/org/apache/struts2/util/StrutsUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
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;
Expand Down Expand Up @@ -148,6 +149,10 @@ public String getContext() {
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/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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/>
1 change: 1 addition & 0 deletions core/src/main/resources/template/simple/radiomap.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -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/>
Expand Down
31 changes: 19 additions & 12 deletions core/src/test/java/org/apache/struts2/util/StrutsUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -184,6 +179,18 @@ public void testToString() throws Exception {
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

Expand Down
30 changes: 27 additions & 3 deletions core/src/test/java/org/apache/struts2/views/jsp/ui/RadioTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;


/**
*/
Expand Down Expand Up @@ -175,6 +175,30 @@ public void testGenericXhtml() throws Exception {
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");
Expand Down
Original file line number Diff line number Diff line change
@@ -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>

0 comments on commit 0d8f4d4

Please sign in to comment.