Question

Hi everyone and thanks a lot for helping and excuse me for my rusty english :). I was trying to create a mvc website with spring 3.x , spring-security ,and .jsp pages, i can paste a lot of code here but i think it will be useless because it is a kind of logic problem on how the URI of the action and the uri of the controller works:

1)Jsp pages: login: avalaible to all guests and return homeAdmin for admins with action "home" or
homeUser : for users with the action
homeAdmin : provides a menu with different actions mapped with " admin/action"
HomeUser : provides a menu with different actions mapped with " user/action"
UserLIst: a page that list every users and their information in a table

2)Controller : guest controller: no class controller ,there are only two methods. First for mapping value={ "/, home"} and latter for "login". So everyone that try to do a request will be redirect to the login page for be able to login

@Controller
   public class LoginController {

   @RequestMapping(value={"/home","/"}
   (code)//if admin homeAdmin else homeUser
   return new ModelAndView("home");

   @RequestMapping(value="/login", method = RequestMethod.GET)
   (code)
   return new ModelAndView("login");
....

There is a requestmapping for the class with the value "/admin/" for map the first part of the Uri-action;and there are different methods with the request mapping for a specificated action , like list User. AdminController

@controller
@RequestMapping("/admin")
public class AdminController {

@RequestMapping(value={"/list", "/adminFilm"})
(code)
return new ModelAndView("adminFilm");
...

Now i do login as admin and my URL became
"localhost:8080/name_project/homeAdmin.jsp"
which is URI corresponding to URL
"/webapp/metainf/views/homeAdmin.jsp"
(generated by method InternalResuorceViewResolver into its xml page).
Pressing the button "List User" I perform an action="admin/list", now my URI is
"localhost:8080/name_project/admin/listUser.jsp"
At this point any of my action don't need to generate "admin/" because is alredy in the path but if try to logout (j_springsecurity_Logout) my URL in the top becames from
"localhost:8080/name_project/admin/listUser",
to
"localhost:8080/name_project/admin/j_spring_Logout"
so i get (naturally) an error for a mapping not found.
Same problem appears for homepage request because it is mapped by LoginController and not in AdminController, so when i request it from any page that have path "/admin/home", this is not found and can't call the controller in loginController. I could solve it forcing paths of jsp pages, but it's bad programming (and generates errors). How can i solve this problem of paths?

If problem isn't understandable, I'll try to re-write it.

Thanks.

Was it helpful?

Solution

As I understand it the current URL in the browsers address bar is localhost:8080/name_project/admin/listUser. Now you click a link (or button, doesn't matter) that has the URL j_springsecurity_Logout. This is relative to the current path so you get the expected behavior: The resolved URL is localhost:8080/name_project/admin/j_spring_Logout.

What you need is the link's URL to be relative to the application path. That's what <c:url /> is for. So, in case of a link, instead of

<a href="j_spring_Logout">

you write

<a href="<c:url value="j_spring_Logout" />">
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top