spring 如何在Spring webapp的JSP中获取正确的当前URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9711896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get correct current URL in JSP in Spring webapp
提问by user405935
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:
我正在尝试在 Spring webapp 的 JSP 中获取正确的当前 URL。我正在尝试在 JSP 文件中使用以下片段:
${pageContext.request.requestURL}
The issue is that the returned URL contains prefix and suffix defined by UrlBasedViewResolver. For example the correct URL is:
问题是返回的 URL 包含由 UrlBasedViewResolver 定义的前缀和后缀。例如,正确的 URL 是:
But the returned one is:
但返回的是:
回答by Paulius Matulionis
回答by izilotti
Maybe you are looking for something like:
也许您正在寻找类似的东西:
<%= new UrlPathHelper().getOriginatingRequestUri(request) %>
This is not that elegant but solved my problem.
这不是那么优雅,但解决了我的问题。
回答by andy
In a jspfile:
在一个jsp文件中:
request.getAttribute("javax.servlet.forward.request_uri")
回答by Vicky
You can make Interceptor and set request attribute e.g.
您可以制作拦截器并设置请求属性,例如
request.setAttribute("__SELF",request.getRequestURI);
and in jsp
并在jsp中
<form action="${__SELF}" ></form>
回答by Sanghyun Lee
Anyone who wants to know about other than the reuqest URI, for example a query string, you can check all the names of the variables in the code of RequestDispatcher(of Servlet API 3.1+) interface.
任何人想知道reuqest URI以外的其他信息,例如查询字符串,您可以在RequestDispatcher(Servlet API 3.1+)接口的代码中检查所有变量的名称。
You can get the query string like this:
您可以像这样获取查询字符串:
${requestScope['javax.servlet.forward.query_string']}
回答by Damir Olejar
Try this:
尝试这个:
<%@ page import="javax.servlet.http.HttpUtils.*" %>
<%= javax.servlet.http.HttpUtils.getRequestURL(request) %>
回答by Rubens Mariuzzo
I just found the right answer for your question. The key is using Spring Tags.
我刚刚找到了您问题的正确答案。关键是使用 Spring 标签。
<spring:url value="" />
If you put the value attribute empty, Spring will display the mapping URL set in your @RequestMapping.
如果您将 value 属性设置为空,Spring 将显示您在 @RequestMapping 中设置的映射 URL。
回答by manish
Which Spring version are you using? I have tested this with Spring 3.1.1.RELEASE, using the following simple application:
您使用的是哪个 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 />
On accessing http://localhost:8080/spring-web/page/home.jsp, the URL is correctly displayed as http://localhost:8080/spring-web/page/home.jsp.
在访问http://localhost:8080/spring-web/page/home.jsp 时,URL 正确显示为http://localhost:8080/spring-web/page/home.jsp。
回答by Kaur Kase
I used the following in a similar situation. ${currentUrl} can then be Used where needed. Needs core tag library
我在类似的情况下使用了以下内容。${currentUrl} 然后可以在需要的地方使用。需要核心标签库
<c:url value = "" var = "currentUrl" ></c:url>
回答by Thomson Fernandez
*<% 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*

