Alex Rivera | Logout

How to get correct current URL in JSP in Spring webapp

Asked 2012-03-14T23:29:23.073
44

I'm trying to get a correct current URL in JSP in Spring webapp. I'm trying to use the following fragment in the JSP file:

${pageContext.request.requestURL}

The issue is that the returned URL contains prefix and suffix defined by UrlBasedViewResolver. For example the correct URL is:

http://localhost:8080/page

But the returned one is:

http://localhost:8080/WEB-INF/jsp/page.jsp

Edit
Report

1 Answer

0

Which Spring version are you using? I have tested this with Spring 3.1.1.RELEASE, using the following simple application:

Folder structure
-----------------------------------------------------------------------------------

spring-web
    |
     --- src
          |
           --- main
                 |
                  --- webapp
                         |
                          --- page
                         |     |
                         |      --- home.jsp
                         |
                          --- WEB-INF
                               |
                                --- web.xml
                               |
                                --- applicationContext.xml

home.jsp
-----------------------------------------------------------------------------------

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html dir="ltr" lang="en" xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Welcome to Spring Web!</title>
    </head>
    <body>
        Page URL: ${pageContext.request.requestURL}
    </body>
</html>

web.xml
-----------------------------------------------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee" metadata-complete="true" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<display-name>org.example.web</display-name>

<servlet>
    <servlet-name>spring-mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/webContext.xml</param-value>
 
answered 2012-04-12T05:02:49.447

Your Answer