Struts 2重定向到另一个操作期间的参数编码问题

发布于 2024-10-14 17:35:46 字数 2402 浏览 4 评论 0原文

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

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

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

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

private String message;

Struts.xml 包含这两个操作的定义

<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 上显示消息,则一切正常。编码是正确的。 如果我重定向到另一个操作,消息字段的设置器将设置错误的格式字符串“?”。 我找不到解决方案。有人可以帮我吗?

自己的过滤器:

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

然后我尝试了这个过滤器:

<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 造成的。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

谜兔 2024-10-21 17:35:46

问题是tomcat的问题而不是struts的问题。
解决方案是在server.xml中将Connector的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

寄离 2024-10-21 17:35:46

也许问题在于 url(进行操作调用)的生成方式。例如,如果 url 生成如下所示,请注意 属性转义< /a>:

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

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>
ゝ偶尔ゞ 2024-10-21 17:35:46

这个问题看起来与您遇到的问题相同:

Struts2请求字符编码

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

Struts2 request character encoding

書生途 2024-10-21 17:35:46

我想你可以尝试在会话中保存消息变量。此外,据我所知,一般来说,在Web应用程序中,将信息从一个动作传递到另一个动作的推荐方式是使用会话。

如果其他方法失败了,我想有一个 hackie 方法。您可以将结果发布到普通网页(jsp)。您将该消息值放入输入元素中。然后你编写一个 javascript,它会自动提交该输入元素内的值。不过不推荐。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文