WW-3863 Allows to define encoding per result

git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@1421740 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Lukasz Lenart
2012-12-14 08:53:49 +00:00
parent b86d3d13fe
commit a4f1a77b2d
2 changed files with 54 additions and 14 deletions
@@ -65,6 +65,7 @@ public class JSONResult implements Result {
private static final Logger LOG = LoggerFactory.getLogger(JSONResult.class);
private String encoding;
private String defaultEncoding = "ISO-8859-1";
private List<Pattern> includeProperties;
private List<Pattern> excludeProperties;
@@ -217,10 +218,14 @@ public class JSONResult implements Result {
* Retrieve the encoding <p/>
*
* @return The encoding associated with this template (defaults to the value
* of 'struts.i18n.encoding' property)
* of param 'encoding', if empty default to 'struts.i18n.encoding' property)
*/
protected String getEncoding() {
String encoding = this.defaultEncoding;
String encoding = this.encoding;
if (encoding == null) {
encoding = this.defaultEncoding;
}
if (encoding == null) {
encoding = System.getProperty("file.encoding");
@@ -421,4 +426,16 @@ public class JSONResult implements Result {
public void setWrapSuffix(String wrapSuffix) {
this.wrapSuffix = wrapSuffix;
}
/**
* If defined will be used instead of {@link #defaultEncoding}, you can define it with result
* &lt;result name=&quot;success&quot; type=&quot;json&quot;&gt;
* &lt;param name=&quot;encoding&quot;&gt;UTF-8&lt;/param&gt;
* &lt;/result&gt;
*
* @param encoding valid encoding string
*/
public void setEncoding(String encoding) {
this.encoding = encoding;
}
}
@@ -20,6 +20,16 @@
*/
package org.apache.struts2.json;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.mock.MockActionInvocation;
import com.opensymphony.xwork2.util.ValueStack;
import org.apache.struts2.StrutsStatics;
import org.apache.struts2.StrutsTestCase;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockServletContext;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
@@ -34,18 +44,6 @@ import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.StrutsStatics;
import org.apache.struts2.StrutsTestCase;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockServletContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.mock.MockActionInvocation;
import com.opensymphony.xwork2.util.ValueStack;
/**
* JSONResultTest
*/
@@ -573,6 +571,31 @@ public class JSONResultTest extends StrutsTestCase {
assertEquals("application/json;charset=ISO-8859-1", response.getContentType());
}
public void testDefaultEncoding() throws Exception {
// given
JSONResult json = new JSONResult();
json.setDefaultEncoding("UTF-16");
// when
String encoding = json.getEncoding();
// thn
assertEquals("UTF-16", encoding);
}
public void testEncoding() throws Exception {
// given
JSONResult json = new JSONResult();
json.setEncoding("UTF-8");
json.setDefaultEncoding("UTF-8");
// when
String encoding = json.getEncoding();
// thn
assertEquals("UTF-8", encoding);
}
@Override
protected void setUp() throws Exception {
super.setUp();