简体   繁体   中英

Java web app redirect to URL depending on where the request came from

I understand the title isn't really great, but I'm not really sure how to explain it so I will describe the scenario.

First of all, I'm using a MVC design.

There are two resources User and Topic . User info is found at /users/{id} , there is an option to browse topics created by that user which redirects to /users/{id}/topics . When the user deletes that a topic I want him to send him to /topics/{id}/delete , I think that this should be handled by my TopicController .

Now the problem: in my TopicController , how do I redirect the user back to /users/{id}/topics ? Normally, when browsing all topics it returns /topics . Basically if the request came from user topic list, I need to return him back there after the topic is deleted.

Basically I need a more elegant sollution for

if(requestURI.contains("/users") 
   return "redirect:/users/{id}/topics" 
else 
   return "redirect:/topics"

In your TopicController handler that maps to /topics/{id}/delete you need to do whatever you need to to remove the topic and then something like:

    ...
    redirectAttrs.addAttribute("id", account.getId()); 
    return "redirect:/users/{id}/topics";
}

Update:-

/users/{id} -> invokes 1 handler which returns user info
/users/{id}/topics -> invokes 1 handler which returns a users topics
/topics/{id}/delete -> invokes 1 handler which deletes a users topic

If a user deletes his/her topic then shouldn't you always redirect to /users/{id}/topics since that is where they were when they chose to remove a topic?

If a user is on /topics then they cannot delete a topic right?

Do you ever have a Use Case for directing a user from /topics/{id}/delete to /topics ? I don't think so.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM