问题
I'm looking to encode filter parameters within a URL. I'd like a readable URL so I'd like to replace spaces with underscores.
Example aston martin encoded to aston_martin and not aston$0020martin. I would then need to be able to decode the URL back to aston martin.
My only concern is with search parameters naturally containing underscores. I'm not sure what you would do in this particular use case.
I'm a tapestry5 user, so I do have the option to override the URLEcoder, or could use the java URLEncoder. I just haven't figured out how to get it to encode things the way I'd like them.
Any help would be appreciated.
回答1:
You are confusing URL-encoding with java.net.URLEncoder. Not surprising, and not your fault. Despite its name, the latter process is for URL argument names and values, and the same in POST data. It is only the former that turns space into %20. The latter turns it into '+', which is (a) the correct thing to do for your filter parameters, (b) reversible, and (c) not as offensive as %20. Don't know where you got $0020 from.
来源:https://stackoverflow.com/questions/12944672/java-urlencoding-decoding-url-spaces-with-dashes