【问题标题】:How to get correct current URL in JSP in Spring webapp如何在 Spring webapp 中的 JSP 中获取正确的当前 URL
【发布时间】:2012-03-31 11:57:57
【问题描述】:

我正在尝试在 Spring webapp 中的 JSP 中获取正确的当前 URL。我正在尝试在 JSP 文件中使用以下片段:

${pageContext.request.requestURL}

问题是返回的 URL 包含 UrlBasedViewResolver 定义的前缀和后缀。例如正确的 URL 是:

http://localhost:8080/page

但返回的是:

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

【问题讨论】:

  • 您需要绝对网址吗?或者您只是想在您的应用程序中建立一个指向另一个页面的内部链接?
  • 我需要绝对 URL。

标签: spring jsp url web-applications request


【解决方案1】:

您使用的是哪个 Spring 版本?我已经用 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时,URL正确显示为http://localhost:8080/spring-web/page/home.jsp

【讨论】:

  • 我也在使用 Spring 3.1.1。我需要的是一个由控制器映射的 URL。例如,如果您有一个带有 @RequestMapping("/home") 的控制器,它将使用 page/home.jsp 作为视图,那么在该 jsp 中使用 ${pageContext.request.requestURL} 将返回 localhost:8080/spring-web/page/home.jsp 但在浏览器中应该是localhost:8080/spring-web/home
【解决方案2】:

我刚刚为您的问题找到了正确答案。关键是使用 Spring Tags。

<spring:url value="" />

如果你将 value 属性设置为空,Spring 将显示在你的@RequestMapping 中设置的映射 URL。

【讨论】:

  • 在我的情况下它没有显示任何内容:/
  • 我相信当target为空的时候浏览器会使用当前的URL。服务器/webapp 也一样吗?
【解决方案3】:

也许你正在寻找类似的东西:

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

这不是那么优雅,但解决了我的问题。

【讨论】:

    【解决方案4】:

    jsp 文件中:

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

    【讨论】:

    • 不返回问题的 cmets 中 OP 请求的绝对 URL。
    【解决方案5】:

    最好的方法是像这样使用EL

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

    【讨论】:

    • 这非常有效。它返回没有基本 URL 和查询字符串的当前操作 URL。
    • 这不起作用。它不会按照 cmets 中 OP 对问题的要求返回绝对 URL。
    【解决方案6】:

    我在类似的情况下使用了以下内容。然后可以在需要的地方使用 ${currentUrl}。需要核心标签库

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

    【讨论】:

      【解决方案7】:

      您可以制作拦截器并设置请求属性,例如

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

      在jsp中

        <form action="${__SELF}" ></form>   
      

      【讨论】:

        【解决方案8】:
        *<% 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*
        

        【讨论】:

          【解决方案9】:

          任何想知道除了reuqest URI之外的东西,例如一个查询字符串,你可以在RequestDispatcher(Servlet API 3.1+)接口的代码中查看所有变量的名称。

          可以这样获取查询字符串:

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

          【讨论】:

            【解决方案10】:

            试试这个:

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

            【讨论】:

            • 谢谢,赞成,请使用 nondepercated 选项进行编辑。
            • @MUYBelgium 你试过打开一个单独的 Stack-Overflow 问题吗?我希望我有更多的时间来帮助你。
            猜你喜欢
            • 2011-10-12
            • 1970-01-01
            • 2014-06-21
            • 1970-01-01
            • 2013-01-13
            • 1970-01-01
            • 2019-09-23
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多