How to get correct current URL in JSP in Spring webapp

后端 未结 10 605
梦谈多话
梦谈多话 2021-02-03 18:53

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}


        
相关标签:
10条回答
  • 2021-02-03 19:41

    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>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>spring-mvc-dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>
    </web-app>
    
    applicationContext.xml
    -----------------------------------------------------------------------------------
    
    <?xml version="1.0" encoding="UTF-8"?>
    
    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd">
    
    <context:annotation-config />
    <context:component-scan base-package="org.example" />
    
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/page/" />
        <property name="suffix" value=".jsp" />
    </bean>
    
    <mvc:annotation-driven />
    

    On accessing http://localhost:8080/spring-web/page/home.jsp, the URL is correctly displayed as http://localhost:8080/spring-web/page/home.jsp.

    0 讨论(0)
  • 2021-02-03 19:44

    The best way would be to use EL like this:

    ${requestScope['javax.servlet.forward.request_uri']}
    
    0 讨论(0)
  • 2021-02-03 19:44

    Maybe you are looking for something like:

    <%= new UrlPathHelper().getOriginatingRequestUri(request) %>
    

    This is not that elegant but solved my problem.

    0 讨论(0)
  • 2021-02-03 19:49

    Try this:

    <%@ page import="javax.servlet.http.HttpUtils.*" %>
    <%= javax.servlet.http.HttpUtils.getRequestURL(request) %> 
    
    0 讨论(0)
提交回复
热议问题