Encoding with parameters for struts2 redirection

Source: Internet
Author: User

Because redirection will lose all request parameters and value stacks, a request parameter MSG is added during redirection, and MSG is set in the action to be redirected.

The problem is that the msg parameter in this action becomes garbled during redirection !!! Chinese characters, English letters, and numbers are all garbled characters.


All redirection operations will lose all request parameters, request attributes, and so on. Of course, the processing results including actions will also be lost.
First, let's clarify the differences between redirect and redirectaction:
1. The redirect type struts2 is used to call the sendredirect (string) method of httpservletresponse to redirect to the specified resource. It can be a view result or other types of actions;
2. redirectaction is also a new request. However, inside struts2, actionmapper provided by actionmapperfactory is used for redirection. It can only jump to another action;

Because redirectaction uses actionmapper for redirection, it also uses the actionmapper encoding method for re-encoding, which leads to garbled characters when parameters are retrieved.Source codeBut it is a little troublesome to restore the code multiple times. Redirect uses httpservletresponse for redirection, so the above problem does not exist. Finally, change to redirect. The result is as follows: 1 < Result Name = "Myinfosuccess" Type = "Redirect" >
2 < Param Name = "Location" > Myapp_myinfo.action? MSG =$ {MSG} </ Param >
3 < Param Name = "Encode" > True </ Param >
4 </ Result >

Note: The action corresponding to myapp_myinfo.action must transcode the msg parameter once, because the previous action is encoded with URL base64. If it is sent directly to the browser, you will see a string of URL base64 encoded characters with % in the browser, so you need to add1String msg=Urldecoder. Decode (getmsg (),"UTF-8");
2Setmsg (MSG );

Set it back to Chinese to make the browser normal.

We also need to mention another redirection type chain, which is the action chain and can keep the current value stack unchanged. However, after redirection with it, although it jumps to other actions, the URL on the browser side will not change, so the problem mentioned at the beginning is still unsolvable!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.