Skip to content

Commit

Permalink
Merge pull request #678 from apache/WW-5302-unevaluated-id
Browse files Browse the repository at this point in the history
[WW-5302] Evaluates the name attribute before assigning it to the id attribute
  • Loading branch information
lukaszlenart committed May 16, 2023
2 parents 92705b9 + f9f05dc commit 1eedfe3
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 29 deletions.
18 changes: 9 additions & 9 deletions core/src/main/java/org/apache/struts2/components/FormButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,33 +96,33 @@ public void evaluateExtraParams() {
* </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));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/resources/template/simple/submit.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,4 @@
<#include "/${parameters.templateDir}/${parameters.expandTheme}/common-attributes.ftl" />
<#include "/${parameters.templateDir}/${parameters.expandTheme}/dynamic-attributes.ftl" />
/>
</#if>
</#if>
21 changes: 10 additions & 11 deletions core/src/test/java/com/opensymphony/xwork2/TestBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,18 @@

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;
}
Expand All @@ -63,13 +56,19 @@ public String getName() {
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,7 +32,7 @@
*/
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();
Expand All @@ -47,7 +48,7 @@ public void testPopulateComponentHtmlId1() throws Exception {
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();
Expand All @@ -63,7 +64,7 @@ public void testPopulateComponentHtmlId2() throws Exception {
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();
Expand All @@ -80,7 +81,7 @@ public void testPopulateComponentHtmlId3() throws Exception {
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();
Expand All @@ -93,7 +94,7 @@ public void testPopulateComponentHtmlId4() throws Exception {
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();
Expand All @@ -106,7 +107,7 @@ public void testPopulateComponentHtmlId5() throws Exception {
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();
Expand All @@ -119,4 +120,38 @@ public void testPopulateComponentHtmlId6() throws Exception {

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"));
}
}
39 changes: 37 additions & 2 deletions core/src/test/java/org/apache/struts2/views/jsp/ui/SubmitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -699,4 +698,40 @@ public void testSubmitWithBodyNotHTMLEscaped() throws Exception {

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"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<input type="submit" value="Save" id="entryEdit_saveDraft" name="entryEdit!saveDraft"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<input type="submit" value="Save" id="mainAction_saveDraft" name="action:mainAction!saveDraft"/>

0 comments on commit 1eedfe3

Please sign in to comment.