Вопрос

I want to display articles in a website in the following manner

example.com/articles/23/thetitle

ie  example.com/articles/{id}/{title}

For each request, what actually matters is the id. I will display the article corresponding to the id, the title is shown for readability. Now the problem is when the user edits the url in the browser, say from example.com/articles/23/thetitle to example.com/articles/23/xyz and reloads, the same content is loaded with the new url.

How do i get around this? How can I get my URL to be displayed as example.com/articles/23/thetitle for a an entry example.com/articles/23/{anything}

I had a look into URLrewriting. but it doesn't seem to be the solution for me.

The only solution i can think about is doing it from javascript, but that IMHO is dirty.

I am using Spring MVC. Thanks in advance

PS: To put it in one sentence, I want it to work the way it works in SO. If we edit the question number and reload, the new title is loaded.

Here is a code sample of how I am currently fetching an article

@RequestMapping(value = "/{id}/{title}", method = RequestMethod.GET)
public 
String loadArticle(HttpServletRequest request,
        @PathVariable Long id, Model model) {
    //get the article
    model.addAttribute("article", article);
    return "article";
    }
Это было полезно?

Решение

I would use a servlet that checks the URL and sends an HTTP redirect response code (method sendRedirect it HttpServletResponse)

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top