Avoid URL Encoding

大兔子大兔子 提交于 2019-12-25 01:28:47

问题


I'm having an issue related to url encoding in my struts 2 application. I did some research but could not find the correct way to do this.

My application prepares the url dynamically and gets the variable by querying the database. The valid URL that I'm expecting is
http://www.test.com/language=english&numbers=X,Y,Z

However, when on UI because of URL encoding i get the URL as below
http://www.test.com/language=english&numbers=X%2CY%2CZ

numbers is a single variable and database returns the value as X,Y,Z.

I'm preparing the URL in jsp as below

<s:url id="testUrl" escapeAmp="false" value="http://www.test.com">
  <s:param name="language" value="%{'english'}" />
  <s:param name="numbers" value="%{Num}" />
</s:url>

I did try encode="false" in the s:url tag but to no avail.

The testUrl is is hyperlinked as below

<a href="<s:property value="#testUrl" />" target="_blank">
  <s:property value="#Num" /> 
</a>

I understand that comma(,) is a reserved character and should be encoded. However, in my URL i need that comma so as to execute the url successfully when clicked. I'm using char set iso-8859-1.


回答1:


I understand that comma(,) is a reserved character and should be encoded. However, in my URL i need that comma

Then you don't... this is a perfect example of the XY problem:

The XY problem is asking about your attempted solution rather than your actual problem.

That is, you are trying to solve problem X, and you think solution Y would work, but instead of asking about X when you run into trouble, you ask about Y.

Something wrong has been fixed in the specs of the project, and now it shall need something even more wrong to workaround the first problem.

You shouldn't use unescaped commas in URLs; traverse backward the chain of specs to find where they could be branched to a newer, safer direction.

If the requirement is, for example, to have readable URLs (usually called Pretty URLs or RESTful URLs), you can use Advanced Wildcard Mappings; let's say you always have three numbers, like for cartesian coordinates; you could do like this:

<action name="/coordinates/{x}/{y}/{z}" class="foo.bar.CartesianAction">

callable with the URL:

http://www.test.com/coordinates/1/2/3

, way more readable than your original URL. But there are solutions (not involving commas in URLs) for every kind of need you may have, this is just an example.

Sidenote:

From the URL you posted (probably totally unrelated with your real use-case), it seems that you have mounted your webapp on the root context, and that you are an Action composed by just the variables (without the action name); if that is the case, you may want to read this answer.

The language=english part is also probably totally forged, but in the remote case you are really handling the language on your own, be aware that the framework would be happy to help you on that.




回答2:


This may not be best approach for everyone but i was able to get this working using javascript. I tested the functionality thoroughly and it is currently serving my purpose.
I did not have control to modify the URL as it was a third party URL.

So when a User clicks on a hyperlink on my website, a java script is called. The URL gets decoded and opens up in a new window.

<a href="#" onclick="decodeURL('<s:property value="#testUrl"/>')">
   <s:property value="#Num" /> 
</a>

Javascript

function decodeURL(url) {
var url_dec = decodeURIComponent(url);
window.open(url_dec);
}


来源:https://stackoverflow.com/questions/27279491/avoid-url-encoding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!