A solution that does not match the 404 error when the parameter passed with @paramvariable in the SPRINGMVC contains a slash (/)

Source: Internet
Author: User

Today to do the site " tag " screening function, there has been such a wonderful problem.
I jump directly through the <a> tag in the href, the URL contains Chinese characters
<href= "/tags/label a"> label a</a>

The background code is this:

@RequestMapping (value = "/tags/{tagname}")publictagname) {
Iso-8859-1 ==> UTF-8 for encoding conversion
TagName = Encode_to_utf8 (tagname);
The rest is handled slightly
}

This is supposed to be the case, the major browsers are also normal execution.

However, not a heart to find, as long as the URL appears in the " charge " This character, directly reported 404 error

For example, this:

<href= "/tags/label charge a"> label charge a</a>

It's wonderful.

After a lengthy investigation, the reasons may be:

Fill This character in the URL directly submitted, after the browser transcoding, will become a string containing "/" of "garbled".

Later, a similar test found that, as long as the URL contains "/" parameters, can not be correctly matched by @PathVariable .

Some say it's better to change it:

Scenario 1:

On the server side, the kanji are UTF-8 encoded by UrlEncode and then thrown to the front.

veto : to do so, the URL will become this ugly kind, and garbled what is the difference? Really don't like it.

<href= "/tags/%d6%d0%b9%fa"> label charge a</a  >

Later, throughout the major sites, each has its own practice

Scenario 2:

<href= "/tags?tagname= label charge a"> label charge a</A  >

Then use the @RequestParam in the controller to receive the parameters, which is really possible.

veto : But SEO big God said that the URL contains the dynamic parameters, there may be spiders repeatedly crawl, not conducive to SEO.

Scenario 3: Converting Chinese notes to Pinyin

<href= "/tags/biaoqianchonga"> label charge a</a >

Veto: This can be, but also in a Chinese character to pinyin plug-in, and also look not intuitive, bad.

Scenario 4: Give the label an ID

<href= "/tags/t1"> label charge a</a>

veto : This can be, but I also want the table structure, egg pain.

Scenario 5: Use JS to block A's href to achieve post jump

veto : Now Baidu can already parse JS it?

Do you have any other plans?

There is no way to keep the URL format and Chinese characters are the same situation, to achieve this function?

Finally found that someone has done this!

Front:

<href= "/tags/label charge a"> label charge a</a>

Back end:

@RequestMapping (value = "/tags/**") PublicString tags (httpservletrequest request) {//iso-8859-1 ==> UTF-8 for encoding conversionString TagName =Extractpathfrompattern (Request); TagName=toolutils.encodestr (tagname); //The rest is handled slightly}//truncates the string after the specified URL as a parameter//This is done to prevent the URL from containing Chinese or special characters (/etc.), the problem cannot be matchedPrivate StaticString Extractpathfrompattern (FinalHttpServletRequest Request) {String path=(String) Request.getattribute (Handlermapping.path_within_handler_mapping_attribute); String Bestmatchpattern=(String) Request.getattribute (Handlermapping.best_matching_pattern_attribute); return NewAntpathmatcher (). Extractpathwithinpattern (Bestmatchpattern, path);}

After that, no matter what URL you enter, you can go to the specified method!

<a href= "/tags/label charge a" > label charging a</a>
<a href= "/tags/label charging A/ASD/ASD" > label charging a</a>
<a href= "/tags/label charging a/bb/cc.html" > label charging a</a>



Reference Original address: http://kamatama41.hatenablog.com/entry/20130411/1365668200

A solution that does not match the 404 error when the parameter passed with @paramvariable in the SPRINGMVC contains a slash (/)

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.