开发者

Struts 2 parameter coding problem during redirect to another action

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 S开发者_运维知识库tring 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.


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


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>


This question looks like it is the same issue you are having:

Struts2 request character encoding


i guess you can try saving your message variable in session. moreover, afaik, generally speaking, in web application, the recommended way of passing information from 1 action to another action is by using session.

and if anything else fails, i guess there is a hackie way. you can release the result to an ordinary web page (jsp). you put that message value in an input element. then you write a javascript which automatically submit the value inside that input element. not recommended, though.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜