user962206
user962206

Reputation: 16147

Stackoverflow Like URLS with Struts2 or JSP

I have noticed that SO generates its url very nicely, let's say when creating a question, the url is like this http://stackoverflow.com/questions/ask or when viewing a question http://stackoverflow.com/questions/QUESTIONNUM/some-question-title

How does Stackoverflow implements such elegant urls, it does not have any query strings in its url, even though without query strings it can still identify what page, or what question number is being accessed. I want to implement such url in my application.

My question how do you implement this in an application? I am using Apache Tomcat and Struts2.

Upvotes: 2

Views: 270

Answers (2)

Lukasz Lenart
Lukasz Lenart

Reputation: 1017

You can always write your own ActionMapper and used instead of default mapper as follow:

<bean type="org.apache.struts2.dispatcher.mapper.ActionMapper" name="myMapper" class="com.company.MyActionMapper" />
<constant name="struts.mapper.class" value="myMapper" />

Upvotes: 2

Darin Dimitrov
Darin Dimitrov

Reputation: 1039438

StackOverflow is built with ASP.NET MVC. It uses ASP.NET routing. The important part of this url is QUESTIONNUM. That's what's used to query the database and retrieve the question number. The question title is completely arbitrary. For example both those urls point to exactly the same location:

So basically when a link to a given question is generated, the question id is used to retrieve the question details from the database (such as the title of the question) and the proper url is built using HTML helpers in ASP.NET MVC. Since the title of the question could contain arbitrary characters, this title is filtered through a regular expression to remove dangerous characters and replace them with their safe equivalents.

Upvotes: 2

Related Questions