0

Can I use EL to specify my application context path on my Spring Controller using something like @RequestMapping("${pageContext.request.contextPath}")? Actually, I get an 404 error. So my question is how to map controller on context root?

I presume @RequestMapping("/") cannot do the job when app is not alone on the server, and url looks like: http://localhost:8080/myapp/mypage

@Controller
@RequestMapping("${pageContext.request.contextPath}")
public class MainController {

    @RequestMapping("/")
    public ModelAndView index() {

    }
}

PS: I've already added the app path prefix into every single url on my JSPs, like <form class="form-inline" role="form" action="${pageContext.request.contextPath}/search" method="post">. Now I need to make the same trick for my Spring Controllers to make it work when... wheather app deployed as ROOT app or as "just another app on this server".

2
  • You want to add your context path as a prefix for every single URL? Commented Apr 29, 2016 at 17:39
  • This doesn't make any sense. The mappings are already relative to the context root. Spring's DispatcherServlet is mapped to /. The Servlet container will already have resolved that your app's Servlet should be invoked. Commented Apr 29, 2016 at 18:25

1 Answer 1

1

you can add in the application.properties config file the root context path. That root context path will be the prefix path to all the controller paths.

application.properties

server.contextPath=/myapp/mypage

controller

@RequestMapping("/test")

Result:

http://localhost:8080/myapp/mypage/test

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.