Problem with Struts 2 parameter options when redirecting to another action

I am trying to redirect to another action and pass a string parameter. This works without problems, but I have a problem with the encoding if I use German umlauts.

Here is my code: The first action has a field message with a getter and a setter. In action, I set the line.

private String message; public String action1() { message = "ΓΆ"; return SUCCESS; } 

The second action has a field message with a getter and installer.

 private String message; 

Struts.xml defining both actions

 <action name="action" method="action1" class="de.samba.control.actions.Action1"> <result name="success" type="redirectAction"> <param name="actionName">action2</param> <param name="message">${message}</param> 

 <action name="action2" class="de.samba.control.actions.Action2"> <result name="success">/pages/showMessage.jsp</result> 

If I do not use redirection and do not show the message on jsp, everything works fine. The encoding is correct. If I redirect to another action, the message field installer sets the wrong line of the form "â". I can not find a solution. Can someone help me?

Custom filter:

 <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>de.samba.control.CharacterEncodingFilter</filter-class> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> 

Class filter

 public class CharacterEncodingFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain next) throws IOException, ServletException { String encoding = request.getCharacterEncoding(); if (encoding == null || encoding.length() == 0) { request.setCharacterEncoding("UTF-8"); } encoding = request.getCharacterEncoding(); next.doFilter(request, response); } } 

Then I tried this filter:

 <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> 

This does not work either. Does anyone know this problem? Perhaps this is caused by Spring Security.

+6
java struts2
source share
4 answers

The problem was a tomcat problem, not a problem. The solution is to install the URIEncoding connector on "utf-8" in server.xml. See http://struts.apache.org/2.0.14/docs/how-to-support-utf-8-uriencoding-with-tomcat.html

+5
source share

Perhaps the problem is how the URL is generated (which triggers the action). For example, if the URL generations are something like below, keep in mind the escape attribute :

  <s:url namespace="/" action="recursoNuevo" var="recursoNuevo" > <s:param name="contextPath"> <s:property value="contextPath" escape="false"/> </s:param> </s:url> 
+1
source share

This question looks the same as yours:

Character Encoding Struts2

0
source share

I think you can try to keep your message variable in the session. in addition, afaik, generally speaking, in a web application, the recommended way to transfer information from one action to another action is to use a session.

and if something else fails, I think there is a hacky way. You can publish the result on a regular web page (jsp). you put this message value in the input element. then you write javascript that automatically passes the value inside this input element. Not recommended.

0
source share

All Articles