我正在尝试在Spring webapp中的JSP中获取正确的当前URL。我正在尝试在JSP文件中使用以下片段:
${pageContext.request.requestURL}
问题是返回的URL包含UrlBasedViewResolver定义的前缀和后缀。例如,正确的URL是:
但返回的是:
答案 0 :(得分:46)
最好的方法是像这样使用EL:
${requestScope['javax.servlet.forward.request_uri']}
答案 1 :(得分:5)
也许你正在寻找类似的东西:
<%= new UrlPathHelper().getOriginatingRequestUri(request) %>
这不是那么优雅,但解决了我的问题。
答案 2 :(得分:5)
在jsp
文件中:
request.getAttribute("javax.servlet.forward.request_uri")
答案 3 :(得分:3)
我刚刚找到了你问题的正确答案。关键是使用Spring Tags。
<spring:url value="" />
如果将value属性设置为空,Spring将显示在@RequestMapping中设置的映射URL。
答案 4 :(得分:3)
您可以制作拦截器并设置请求属性,例如
request.setAttribute("__SELF",request.getRequestURI);
和jsp
<form action="${__SELF}" ></form>
答案 5 :(得分:3)
任何想要了解reuqest URI之外的人,例如查询字符串,都可以检查RequestDispatcher
(Servlet API 3.1+)接口代码中变量的所有名称。
您可以像这样获取查询字符串:
${requestScope['javax.servlet.forward.query_string']}
答案 6 :(得分:3)
试试这个:
<%@ page import="javax.servlet.http.HttpUtils.*" %>
<%= javax.servlet.http.HttpUtils.getRequestURL(request) %>
答案 7 :(得分:0)
您使用的是哪个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时,网址正确显示为http://localhost:8080/spring-web/page/home.jsp。
答案 8 :(得分:-1)
我在类似的情况下使用了以下内容。然后可以在需要时使用$ {currentUrl}。需要核心标签库
<c:url value = "" var = "currentUrl" ></c:url>
答案 9 :(得分:-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*