Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get requestURI by using jstl in spring mvc project?

I google a lot and get a answer:

<c:out value="${pageContext.request.requestURI}" />

But I get /myapp/WEB-INF/views/index.jsp

I want to get /myapp/index

How can I do that?

My project is using spring mvc. My config in spring-mvc.xml:

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
         <property name="prefix" value="/WEB-INF/views/"/>
         <property name="suffix" value=".jsp"/>
</bean>

In my /WEB-INF/views/, has a index.jsp

My Controler:

@RequestMapping("/index")
public String welcome() {
    return "index";
}

When I view localhost:8088/myapp/index, It shows.

like image 253
secondflying Avatar asked May 31 '13 08:05

secondflying


1 Answers

Try to use ${requestScope['javax.servlet.forward.servlet_path']}

javax.servlet.forward.* constants retrieve information based on URI passed to getRequestDispatcher() (DispatcherServlet sets this attribute while handling request in the case of Spring Web MVC). But it's independent of frameworks and web containers.

As documentation says FORWARD_SERVLET_PATH is:

The name of the request attribute under which the original servlet path is made available to the target of a forward

You should also remember that if the forward() works by calling getNamedDispatcher(), these attributes (there are 4 more similar attributes: request_uri, context_path, path_info and query_string) are not set because in this case the initial elements of the path does not change.

like image 174
Patison Avatar answered Nov 15 '22 06:11

Patison