Struts 2参数编码问题在重定向到另一个动作期间 [英] Struts 2 parameter coding problem during redirect to another action

查看:150
本文介绍了Struts 2参数编码问题在重定向到另一个动作期间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试重定向到另一个动作并传输字符串参数。这没有问题,但如果我使用德语变音符号,我会遇到编码问题。

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.

这是我的代码:
第一个动作有一个带有getter的字段消息和二传手。在动作中我设置了字符串。

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;
}

第二个动作也有一个带有getter和setter的字段消息。

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

private String message;

带有两个动作定义的Struts.xml

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>

如果我不使用重定向并在jsp上显示消息,一切正常。编码是正确的。
如果我重定向到另一个动作,则消息字段的setter设置错误的formattet字符串ö。
我找不到解决方案。有人可以帮帮我吗?

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?

自己的过滤器:

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

过滤类

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>

这也不起作用。有人知道这个问题吗?也许它是由Spring Security引起的。

This does not work too. Do somebody know this problem? Maybe it is caused by Spring Security.

推荐答案

问题是tomcat问题而不是struts问题。
解决方案是在server.xml中将连接器的URIEncoding设置为utf-8。
请参阅 http://struts.apache.org/2.0.14/docs/how-to-support-utf-8-uriencoding-with-tomcat.html

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

这篇关于Struts 2参数编码问题在重定向到另一个动作期间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆