Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Struts 2 parameter coding problem during redirect to another action

Tags:

java

struts2

I try to redirect to another action and to transmit a string parameter. This works without problems, but I have a coding problem if I am using german umlauts.

Here is my code: First action has a field message with getter and setter. In the action I set the String.

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

Second action has a field message with getter and setter too.

private String message;

Struts.xml with the definition of the 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 don´t using redirection and show the message on a jsp, all works fine. The coding is correct. If I redirect to another action the setter of the message field set the wrong formattet string "ö". I cannot found the solution. Can somebody help me please?

Own 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>

Filter-class

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 too. Do somebody know this problem? Maybe it is caused by Spring Security.

like image 970
Rafael Avatar asked Jan 25 '11 08:01

Rafael


People also ask

How do I redirect from one action to another action in struts2?

The redirect result type calls the standard response. sendRedirect() method, causing the browser to create a new request to the given location.

What is ActionSupport in struts2?

ActionSupport class implements a no. of interfaces like Action, Validateable, LocaleProvider and Serializable etc. It is more commonly used instead of Action interface.

What is action redirect?

You can use post-login Actions to redirect users before an authentication transaction is complete. This lets you implement custom authentication flows that require additional user interaction beyond the standard login form.


2 Answers

The problem was a tomcat issue and not a struts issue. The solution is to set the URIEncoding of the Connector to "utf-8" in server.xml. See http://struts.apache.org/2.0.14/docs/how-to-support-utf-8-uriencoding-with-tomcat.html

like image 103
Rafael Avatar answered Sep 28 '22 18:09

Rafael


Maybe the problem is in the way the url (which makes action invocation) is generated. For instance, if the url generations is something like below, be aware of the attribute escape :

 <s:url namespace="/" action="recursoNuevo" var="recursoNuevo" >
   <s:param name="contextPath">
      <s:property value="contextPath" escape="false"/>
   </s:param>
  </s:url>
like image 41
togomez Avatar answered Sep 28 '22 19:09

togomez