Merge pull request #678 from apache/WW-5302-unevaluated-id

[WW-5302] Evaluates the name attribute before assigning it to the id attribute
This commit is contained in:
Lukasz Lenart
2023-05-16 20:18:17 +02:00
committed by GitHub
7 changed files with 100 additions and 29 deletions
@@ -96,33 +96,33 @@ public abstract class FormButton extends ClosingUIBean {
* </ol>
*/
protected void populateComponentHtmlId(Form form) {
String _tmp_id = "";
String tmpId = "";
if (id != null) {
// this check is needed for backwards compatibility with 2.1.x
_tmp_id = findString(id);
tmpId = findString(id);
} else {
if (form != null && form.getParameters().get("id") != null) {
_tmp_id = _tmp_id + form.getParameters().get("id").toString() + "_";
tmpId = tmpId + form.getParameters().get("id").toString() + "_";
}
if (name != null) {
_tmp_id = _tmp_id + escape(name);
tmpId = tmpId + escape(findString(name));
} else if (action != null || method != null) {
if (action != null) {
_tmp_id = _tmp_id + escape(action);
tmpId = tmpId + escape(findString(action));
}
if (method != null) {
_tmp_id = _tmp_id + "_" + escape(method);
tmpId = tmpId + "_" + escape(findString(method));
}
} else {
// if form is null, this component is used, without a form, i guess
// there's not much we could do then.
if (form != null) {
_tmp_id = _tmp_id + form.getSequence();
tmpId = tmpId + form.getSequence();
}
}
}
addParameter("id", _tmp_id);
addParameter("escapedId", escape(_tmp_id));
addParameter("id", tmpId);
addParameter("escapedId", escape(tmpId));
}
/**
@@ -88,4 +88,4 @@
<#include "/${parameters.templateDir}/${parameters.expandTheme}/common-attributes.ftl" />
<#include "/${parameters.templateDir}/${parameters.expandTheme}/dynamic-attributes.ftl" />
/>
</#if>
</#if>
@@ -20,25 +20,18 @@ package com.opensymphony.xwork2;
import java.util.Date;
/**
* TestBean
*
* @author Jason Carreira
* Created Aug 4, 2003 12:39:53 AM
*/
public class TestBean {
private Date birth;
private String name;
private int count;
private String subName;
private TestChildBean child = new TestChildBean();
public TestBean() {
}
public void setBirth(Date birth) {
this.birth = birth;
}
@@ -63,13 +56,19 @@ public class TestBean {
return name;
}
public TestChildBean getChild() {
return child;
}
public void setChild(TestChildBean child) {
this.child = child;
}
public String getSubName() {
return subName;
}
public void setSubName(String subName) {
this.subName = subName;
}
}
@@ -18,6 +18,7 @@
*/
package org.apache.struts2.components;
import com.opensymphony.xwork2.TestBean;
import org.apache.struts2.StrutsInternalTestCase;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
@@ -31,7 +32,7 @@ import com.opensymphony.xwork2.util.ValueStack;
*/
public class FormButtonTest extends StrutsInternalTestCase {
public void testPopulateComponentHtmlId1() throws Exception {
public void testPopulateComponentHtmlId1() {
MockHttpServletRequest req = new MockHttpServletRequest();
MockHttpServletResponse res = new MockHttpServletResponse();
ValueStack stack = ActionContext.getContext().getValueStack();
@@ -47,7 +48,7 @@ public class FormButtonTest extends StrutsInternalTestCase {
assertEquals("submitId", submit.getParameters().get("id"));
}
public void testPopulateComponentHtmlId2() throws Exception {
public void testPopulateComponentHtmlId2() {
MockHttpServletRequest req = new MockHttpServletRequest();
MockHttpServletResponse res = new MockHttpServletResponse();
ValueStack stack = ActionContext.getContext().getValueStack();
@@ -63,7 +64,7 @@ public class FormButtonTest extends StrutsInternalTestCase {
assertEquals("formId_submitName", submit.getParameters().get("id"));
}
public void testPopulateComponentHtmlId3() throws Exception {
public void testPopulateComponentHtmlId3() {
MockHttpServletRequest req = new MockHttpServletRequest();
MockHttpServletResponse res = new MockHttpServletResponse();
ValueStack stack = ActionContext.getContext().getValueStack();
@@ -80,7 +81,7 @@ public class FormButtonTest extends StrutsInternalTestCase {
assertEquals("formId_submitAction_submitMethod", submit.getParameters().get("id"));
}
public void testPopulateComponentHtmlId4() throws Exception {
public void testPopulateComponentHtmlId4() {
MockHttpServletRequest req = new MockHttpServletRequest();
MockHttpServletResponse res = new MockHttpServletResponse();
ValueStack stack = ActionContext.getContext().getValueStack();
@@ -93,7 +94,7 @@ public class FormButtonTest extends StrutsInternalTestCase {
assertEquals("submitId", submit.getParameters().get("id"));
}
public void testPopulateComponentHtmlId5() throws Exception {
public void testPopulateComponentHtmlId5() {
MockHttpServletRequest req = new MockHttpServletRequest();
MockHttpServletResponse res = new MockHttpServletResponse();
ValueStack stack = ActionContext.getContext().getValueStack();
@@ -106,7 +107,7 @@ public class FormButtonTest extends StrutsInternalTestCase {
assertEquals("submitName", submit.getParameters().get("id"));
}
public void testPopulateComponentHtmlId6() throws Exception {
public void testPopulateComponentHtmlId6() {
MockHttpServletRequest req = new MockHttpServletRequest();
MockHttpServletResponse res = new MockHttpServletResponse();
ValueStack stack = ActionContext.getContext().getValueStack();
@@ -119,4 +120,38 @@ public class FormButtonTest extends StrutsInternalTestCase {
assertEquals("submitAction_submitMethod", submit.getParameters().get("id"));
}
public void testPopulateComponentHtmlId7() {
MockHttpServletRequest req = new MockHttpServletRequest();
MockHttpServletResponse res = new MockHttpServletResponse();
ValueStack stack = ActionContext.getContext().getValueStack();
TestBean bean = new TestBean();
bean.setName("secondAction");
stack.push(bean);
Submit submit = new Submit(stack, req, res);
submit.setName("%{name}");
submit.populateComponentHtmlId(null);
assertEquals("secondAction", submit.getParameters().get("id"));
}
public void testPopulateComponentHtmlId8() {
MockHttpServletRequest req = new MockHttpServletRequest();
MockHttpServletResponse res = new MockHttpServletResponse();
ValueStack stack = ActionContext.getContext().getValueStack();
TestBean bean = new TestBean();
bean.setName("boo");
bean.setSubName("foo");
stack.push(bean);
Submit submit = new Submit(stack, req, res);
submit.setAction("%{name}");
submit.setMethod("%{subName}");
submit.populateComponentHtmlId(null);
assertEquals("boo_foo", submit.getParameters().get("id"));
}
}
@@ -18,16 +18,15 @@
*/
package org.apache.struts2.views.jsp.ui;
import com.opensymphony.xwork2.TestBean;
import org.apache.struts2.TestAction;
import org.apache.struts2.views.jsp.AbstractUITagTest;
import java.util.HashMap;
import java.util.Map;
/**
* Unit test for {@link SubmitTag}.
*
*/
public class SubmitTest extends AbstractUITagTest {
@@ -699,4 +698,40 @@ public class SubmitTest extends AbstractUITagTest {
verify(TextFieldTag.class.getResource("Submit-12.txt"));
}
public void testSubmitWithGeneratedId_shouldUseEvaluatedName() throws Exception {
TestAction testAction = (TestAction) action;
testAction.setFoo("entryEdit");
SubmitTag tag = new SubmitTag();
tag.setTheme("simple");
tag.setPageContext(pageContext);
tag.setName("%{foo}!saveDraft");
tag.setValue("Save");
tag.doStartTag();
tag.doEndTag();
verify(TextFieldTag.class.getResource("Submit-13.txt"));
}
public void testSubmitWithGeneratedId_shouldUseEvaluatedAction() throws Exception {
TestAction testAction = (TestAction) action;
testAction.setFoo("entryEdit");
TestBean bean = new TestBean();
bean.setName("mainAction");
stack.push(bean);
SubmitTag tag = new SubmitTag();
tag.setTheme("simple");
tag.setPageContext(pageContext);
tag.setAction("%{name}!saveDraft");
tag.setValue("Save");
tag.doStartTag();
tag.doEndTag();
verify(TextFieldTag.class.getResource("Submit-14.txt"));
}
}
@@ -0,0 +1 @@
<input type="submit" value="Save" id="entryEdit_saveDraft" name="entryEdit!saveDraft"/>
@@ -0,0 +1 @@
<input type="submit" value="Save" id="mainAction_saveDraft" name="action:mainAction!saveDraft"/>