JSP 2.0 SEO友好链接编码 [英] JSP 2.0 SEO friendly links encoding
问题描述
目前我的JSP中有这样的东西
Currently I have something like this in my JSP
<c:url value="/teams/${contact.id}/${contact.name}" />
我的网址的重要部分是ID,我只是为了SEO目的而把它放在上面(就像stackoverflow.com那样。)
The important part of my URL is the ID, I just put the name on it for SEO purposes (just like stackoverflow.com does).
我只是想知道是否有一种快速而干净的编码名称的方法(更改每个空格) +,拉丁字符删除等)。我希望它是这样的:
I was just wondering if there is a quick and clean way to encode the name (change spaces per +, latin chars removal, etc). I'd like it to be like this:
<c:url value="/teams/${contact.id}/${supercool(contact.name)}" />
那里有这样的功能还是应该自己动手?
Is there a function like that out there or should I make my own?
推荐答案
JSTL函数。你需要创建自己的。顺便说一句,我用 -
代替空格。
Nothing like that is available in JSTL functions. You'll need to create your own. I'd by the way rather replace spaces by -
.
到目前为止,你要执行以下操作步骤:
To the point, you want to perform the following steps:
-
小写字符串。
Lowercase the string.
string = string.toLowerCase();
规范化所有字符并摆脱所有变音标记。
string = Normalizer.normalize(string, Form.NFD).replaceAll("\\p{InCombiningDiacriticalMarks}+", "");
用替换所有剩余的非字母数字字符 -
并在必要时崩溃。
string = string.replaceAll("[^\\p{Alnum}]+", "-");
您可以将其包装在EL函数中:
You can wrap this in an EL function:
package com.example;
import java.text.Normalizer;
import java.text.Normalizer.Form;
public final class Functions {
private Functions() {}
public static String prettyURL(String string) {
return Normalizer.normalize(string.toLowerCase(), Form.NFD)
.replaceAll("\\p{InCombiningDiacriticalMarks}+", "")
.replaceAll("[^\\p{Alnum}]+", "-");
}
}
您在中注册/WEB-INF/functions.tld
如下所示:
<?xml version="1.0" encoding="UTF-8" ?>
<taglib
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
version="2.1">
<display-name>Custom Functions</display-name>
<tlib-version>1.0</tlib-version>
<uri>http://example.com/functions</uri>
<function>
<name>prettyURL</name>
<function-class>com.example.Functions</function-class>
<function-signature>java.lang.String prettyURL(java.lang.String)</function-signature>
</function>
</taglib>
您可以在JSP中使用以下内容:
Which you can use in JSP as follows:
<%@taglib uri="http://example.com/functions" prefix="f" %>
...
<a href="teams/${contact.id}/${f:prettyURL(contact.name)}">Permalink</a>
这篇关于JSP 2.0 SEO友好链接编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!