我试图在Spring webapp中的JSP中获取正确的当前URL。我尝试使用下面的代码片段在JSP文件:如何在JSP中获取正确的当前URL在webapp中
${pageContext.request.requestURL}
的问题是,返回的URL中包含的前缀和后缀由作为UrlBasedViewResolver定义。例如,正确的网址是:
但返回的一个是:
http://localhost:8080/WEB-INF/jsp/page.jsp
我试图在Spring webapp中的JSP中获取正确的当前URL。我尝试使用下面的代码片段在JSP文件:如何在JSP中获取正确的当前URL在webapp中
${pageContext.request.requestURL}
的问题是,返回的URL中包含的前缀和后缀由作为UrlBasedViewResolver定义。例如,正确的网址是:
但返回的一个是:
http://localhost:8080/WEB-INF/jsp/page.jsp
哪个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。
我也使用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
我刚刚为你的问题找到了正确的答案。关键是使用Spring标签。
<spring:url value="" />
如果您将value属性设置为空,Spring将在您的@RequestMapping中显示设置的映射URL。
它不会在我的情况下显示任何内容:/ – user405935 2012-08-02 19:44:09
Wierd,你试过了什么? – 2012-08-03 14:52:46
我相信当目标为空时,浏览器将使用当前的URL。服务器/ webapp是否也是? – DerMike 2014-05-26 09:32:20
也许你正在寻找的东西,如:
<%= new UrlPathHelper().getOriginatingRequestUri(request) %>
这不是优雅,但解决我的问题。
在jsp
文件:
request.getAttribute("javax.servlet.forward.request_uri")
谢谢。 你的代码真的帮了我 – 2013-05-29 09:23:09
我花了一小时寻找这个,它的工作 – 2014-07-18 13:25:28
返回一个null。 – 2016-06-20 19:53:41
,最好的办法是使用EL这样的:
${requestScope['javax.servlet.forward.request_uri']}
这工作完美。它返回没有基本URL和查询字符串的当前操作URL。 – 2016-12-15 15:44:17
这不起作用。它不会返回OP在请求的注释中请求的绝对URL。 – 2017-09-21 09:52:16
我曾经在类似的情况下。然后可以在需要时使用$ {currentUrl}。需要核心标签库
<c:url value = "" var = "currentUrl" ></c:url>
您可以制作拦截器并设置请求属性,例如
request.setAttribute("__SELF",request.getRequestURI);
和JSP
<form action="${__SELF}" ></form>
*<% 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*
谁想要了解比reuqest URI等,例如查询字符串,你可以检查变量的所有名称代码RequestDispatcher
(Servlet API 3.1+)接口。
你可以得到查询字符串是这样的:
${requestScope['javax.servlet.forward.query_string']}
试试这个:
<%@ page import="javax.servlet.http.HttpUtils.*" %>
<%= javax.servlet.http.HttpUtils.getRequestURL(request) %>
除getRequestURL已被弃用... – 2017-04-03 13:48:23
谢谢,upvoted,请编辑与nonabrocated选项。 – 2017-04-03 16:43:46
我不知道替换这个旧命令是什么。 – 2017-04-05 12:38:07
你需要绝对的网址是什么?或者你只是想建立一个内部链接到你的应用程序中的另一个页面? – jddsantaella 2012-03-15 09:11:26
我需要绝对URL。 – user405935 2012-03-17 13:12:02