SEO-friendly URLs with Slugify
If you want to use it directly in your JSP, take a look into jstl
If you want to use Slugify in your Java code you only need the library itself. Here's the dependency information for Maven:
<dependency> <groupId>com.github.slugify</groupId> <artifactId>slugify</artifactId> <version>2.3</version> </dependency>Now you're able to use it:
Slugify slg = new Slugify(); String result = slg.slugify("Hello, world!"); // result: hello-worldYou can set custom replacements for Slugify:
Slugify slg = slg.withCustomReplacement("hello", "world").withCustomReplacement("foo", "bar"); // or multiple at once slg = slg.withCustomReplacements(new HashMap<String, String>() {{ put("hello", "world"); put("foo", "bar"); }}); String result = slg.slugify("hello foo"); // result: world-barOr if you want case sensitivity:
Slugify slg = new Slugify().withLowerCase(false); String result = slg.slugify("Hello, World!"); // result: Hello-World