Posted By:
Bernd_Schiffer
Posted On:
Wednesday, September 1, 2004 01:06 PM
I have a problem with, I think so, encoding in struts. The default encoding on my system is iso-8859-1 and I want to process everything in my webapp with UTF-8, which is the aim of all this. My locale ist german. What I'm trying to do ist to send a message through a simple form to my action class via a action form, only one string in a textarea. When I do so, everything will be okay and the string will be written in the same form the next time I enter the application. But when I post a link in this textarea with a parameter connected to this link, and I click on this link in the resulting html page, the parameter is ugly. A german umlaut, e.g. Ü or Ä, will be mutilated in my webapp. Example: My webapp's url is
More>>
I have a problem with, I think so, encoding in struts. The default encoding on my system is iso-8859-1 and I want to process everything in my webapp with UTF-8, which is the aim of all this. My locale ist german.
What I'm trying to do ist to send a message through a simple form to my action class via a action form, only one string in a textarea. When I do so, everything will be okay and the string will be written in the same form the next time I enter the application. But when I post a link in this textarea with a parameter connected to this link, and I click on this link in the resulting html page, the parameter is ugly. A german umlaut, e.g. Ü or Ä, will be mutilated in my webapp.
Example: My webapp's url is
http://localhost:8080/blank/spike.do
When I write in the textarea the following link
link
the next time I click on this link the parameter
pageName
will be mutilated: Ãhm is the result of an output on the html page, http://localhost:8080/blank/spike.do?pageName=%C3%96hm is the link in the browsers url window.
I've tried URLEncoder and URLDecoder, but this doesn't seem to make a difference. What can I do?
In the following I've attached all my sources of this little webapp, which is not that much, because it's only to find this ugly bug. All files are stored in UTF-8. This webbapp is based on the struts-blank.war. I'm using Struts 1.1. Java 1.4.2_04, Windows XP Home, and Tomcat 5.0.17.
<?xml version="1.0" encoding="utf-8" ?>
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<?xml version="1.0" encoding="utf-8"?>
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
Struts Blank Application
action
org.apache.struts.action.ActionServlet
config
/WEB-INF/struts-config.xml
debug
2
detail
2
2
action
*.do
index.jsp
/tags/struts-bean
/WEB-INF/struts-bean.tld
/tags/struts-html
/WEB-INF/struts-html.tld
/tags/struts-logic
/WEB-INF/struts-logic.tld
/tags/struts-nested
/WEB-INF/struts-nested.tld
/tags/struts-tiles
/WEB-INF/struts-tiles.tld
Welcome.jsp
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
<%@ page language="java" pageEncoding="utf-8" %>
SpikeAction.java
package spike;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SpikeAction extends Action {
public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm,
HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
throws Exception {
final SpikeForm form = (SpikeForm) actionForm;
httpServletRequest.setAttribute("editField", form.getEditField());
final String pageName = httpServletRequest.getParameter("pageName");
if (pageName == null) {
httpServletRequest.setAttribute("pageName", "noch nicht gesetzt");
} else {
httpServletRequest.setAttribute("pageName", pageName);
}
return actionMapping.findForward("success");
}
}
SpikeForm.java
package spike;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
public class SpikeForm extends ActionForm {
private String editField;
private String save;
public String getEditField() {
return editField;
}
public void setEditField(final String editField) {
this.editField = editField;
}
public String getSave() {
return save;
}
public void setSave(final String save) {
this.save = save;
}
public void reset(ActionMapping mapping, HttpServletRequest request) {
try {
request.setCharacterEncoding("utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
editField = "";
save = null;
}
}