Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 408 Vote(s) - 3.42 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get correct current URL in JSP in Spring webapp

#1
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:

${pageContext.request.requestURL}

The issue is that the returned URL contains prefix and suffix defined by UrlBasedViewResolver. For example the correct URL is:

[To see links please register here]


But the returned one is:

[To see links please register here]

Reply

#2
Which Spring version are you using? I have tested this with Spring 3.1.1.RELEASE, using the following simple application:

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

[To see links please register here]

; 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

[To see links please register here]

[To see links please register here]

[To see links please register here]

[To see links please register here]

[To see links please register here]

[To see links please register here]

[To see links please register here]

;

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


On accessing

[To see links please register here]

, the URL is correctly displayed as

[To see links please register here]

.
Reply

#3
I just found the right answer for your question. The key is using Spring Tags.

<spring:url value="" />

If you put the value attribute empty, Spring will display the mapping URL set in your @RequestMapping.
Reply

#4
Maybe you are looking for something like:

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

This is not that elegant but solved my problem.
Reply

#5
In a `jsp` file:


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

#6
The best way would be to use [EL][1] like this:

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


[1]:

[To see links please register here]

Reply

#7
I used the following in a similar situation. ${currentUrl} can then be Used where needed. Needs core tag library

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

#8
You can make Interceptor and set request attribute e.g.

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

and in jsp

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

#9
*<% 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*
Reply

#10
Try this:

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

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through