2012-03-14 61 views
30

我試圖在Spring webapp中的JSP中獲取正確的當前URL。我嘗試使用下面的代碼片段在JSP文件:如何在JSP中獲取正確的當前URL在webapp中

${pageContext.request.requestURL} 

的問題是,返回的URL中包含的前綴和後綴由作爲UrlBasedViewResolver定義。例如,正確的網址是:

http://localhost:8080/page

但返回的一個是:

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

+0

你需要絕對的網址是什麼?或者你只是想建立一個內部鏈接到你的應用程序中的另一個頁面? – jddsantaella 2012-03-15 09:11:26

+1

我需要絕對URL。 – user405935 2012-03-17 13:12:02

回答

0

哪個Spring版本您使用的?我和春天有個3.1.1.RELEASE測試此,使用下面的簡單應用:

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 /> 

在訪問http://localhost:8080/spring-web/page/home.jsp,正確的網址顯示爲http://localhost:8080/spring-web/page/home.jsp

+0

我也使用Spring 3.1.1。我需要的是一個由控制器映射的URL。例如,如果您有一個使用page/home.jsp作爲視圖的@RequestMapping(「/ home」)控制器,那麼在該jsp中使用$ {pageContext.request.requestURL}將返回http:// localhost:8080 /spring-web/page/home.jsp,但在瀏覽器中它將是http:// localhost:8080/spring-web/home。 – user405935 2012-04-21 20:25:43

3

我剛剛爲你的問題找到了正確的答案。關鍵是使用Spring標籤。

<spring:url value="" /> 

如果您將value屬性設置爲空,Spring將在您的@RequestMapping中顯示設置的映射URL。

+6

它不會在我的情況下顯示任何內容:/ – user405935 2012-08-02 19:44:09

+0

Wierd,你試過了什麼? – 2012-08-03 14:52:46

+0

我相信當目標爲空時,瀏覽器將使用當前的URL。服務器/ webapp是否也是? – DerMike 2014-05-26 09:32:20

5

也許你正在尋找的東西,如:

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

這不是優雅,但解決我的問題。

6

jsp文件:

request.getAttribute("javax.servlet.forward.request_uri") 
+0

謝謝。 你的代碼真的幫了我 – 2013-05-29 09:23:09

+0

我花了一小時尋找這個,它的工作 – 2014-07-18 13:25:28

+0

返回一個null。 – 2016-06-20 19:53:41

41

,最好的辦法是使用EL這樣的:

${requestScope['javax.servlet.forward.request_uri']} 
+1

這工作完美。它返回沒有基本URL和查詢字符串的當前操作URL。 – 2016-12-15 15:44:17

+0

這不起作用。它不會返回OP在請求的註釋中請求的絕對URL。 – 2017-09-21 09:52:16

-1

我曾經在類似的情況下。然後可以在需要時使用$ {currentUrl}。需要核心標籤庫

<c:url value = "" var = "currentUrl" ></c:url> 
2

您可以製作攔截器並設置請求屬性,例如

request.setAttribute("__SELF",request.getRequestURI); 

和JSP

<form action="${__SELF}" ></form> 
-2
*<% String myURI = request.getAttribute("javax.servlet.forward.request_uri").toString(); %> 
       <% String[] split = myURI.split("/"); %> 
       <% System.out.println("My url is-->"+ myURI 
         + " My url splitter length --->"+split.length 
         +"last value"+split[4]);%> 
<%--    <jsp:param name="split[4]" value="split[4]" /> --%> 
       <c:set var="orgIdForController" value="<%= split[4] %>" /> 
       <a type="button" class="btn btn-default btn-xs" 
        href="${pageContext.request.contextPath}/supplier/add/${orgIdForController}"> 
        <span class="glyphicon glyphicon-plus" aria-hidden="true"></span> 
        Add 
       </a> 
- List item* 
3

誰想要了解比reuqest URI等,例如查詢字符串,你可以檢查變量的所有名稱代碼RequestDispatcher(Servlet API 3.1+)接口。

你可以得到查詢字符串是這樣的:

${requestScope['javax.servlet.forward.query_string']} 
2

試試這個:

<%@ page import="javax.servlet.http.HttpUtils.*" %> 
<%= javax.servlet.http.HttpUtils.getRequestURL(request) %> 
+1

除getRequestURL已被棄用... – 2017-04-03 13:48:23

+0

謝謝,upvoted,請編輯與nonabrocated選項。 – 2017-04-03 16:43:46

+0

我不知道替換這個舊命令是什麼。 – 2017-04-05 12:38:07