How I can match the current page URL with a certain pattern. For example I want to make if statement that makes one region on the page appears or disappears depends on the URL pattern of the current JSP page.
What I know , I have to use the tag :
<c:if test="the conditional test">Region</c:if>
for example I want the region appears if the end of URL is matching /me/*
?
You can use
HttpServletRequest#getRequestURI()
to obtain the request URI. The
getServletPath()
as suggested by the other answer is not necessarily helpful as it represents the servlet path (the matching part in the JSP/Servlet URL pattern), not the request URI (as the enduser sees in the browser address bar). If the JSP was been forwarded by some front controller servlet, you would get the JSP's own path instead of the virtual path as in the browser address bar.
Assuming that you have a menu which is represented by a List in the application scope where the Page class has url and name properties
you can use this code to find current page url, and then do your task
<c:set var="active" value="${fn:endsWith(pageContext.request.requestURI, page.url)}" />
Something along the lines of:
<c:set var="url" value="${pageContext.request.requestURL}" />
<c:set var="pathinfo" value="${fn:split(url, '/')}" />
<c:set var="pathnode" value="${pathinfo[pathinfo.length - 1]}" />
<c:if test="${pathnode == 'me'}">
<p>Show this region</p>
</c:if>
Related
I have two jsp, let's say A.jsp and B.jsp. A.jsp has the following code:
<c:import url="B.jsp" >
<c:param name = "page_title" value = "Title" />
</c:import>
In B.jsp i need to check some conditions and do a redirection to Login.jsp. I achieve this by doing response.sendRedirect(Logn.jsp)
The problem is that the redirection is made on B.jsp so the result is that the browser displays the content of Login.jsp and A.jsp but i need to redirect the whole page to Login.jsp. That is, the browser should only show Login.jsp.
Consider that the redirection must be made on B.jsp unless there is a way that B.jsp can tell A.jsp the url to redirect to.
EDIT: A.jsp and B.jsp belong to different projects
In your B.jsp, instead of doing a redirect, set a flag on the request as
<c:set var="login" value="true" scope="request" />
Then in A.jsp check the flag and redirect if present.
<c:if test="${login == 'true'}">
<c:redirect url="/login.jsp" />
</c:if>
Try doing a jsp:include instead.
<jsp:include page="B.jsp" >
<jsp:param name="page_title" value="Title" />
</jsp:include>
There are two parts to solving this:
1) Making sure that you are not sending any HTML to the client in A until you have performed the logic in B. That means that you must call the B.jsp early (part of good MVC design).
2) Use the var parameter in your c:import:
<c:import url="B.jsp" var="output">
Now the output of the call to B will be in a buffer called "output" instead of being sent directly to the client. That allows you to make decisions in A (after the call to B). If you decide you want to redirect, you can, because you have dumped nothing (except some white space probably) to the browser. If you decide you want to send the output of B to the browser instead you can simply do this after the c:import:
${output}
There is even a trick to get rid of the white space being sent to the browser if that becomes necessary, but it rarely is (if you send too much white space the buffer will commit and then you cannot redirect, but that's rare and an entirely different question).
Make sense?
What is the best way of obtaining context-root on a jsp page. If I hardcode my css/image to "/myapp/images/foo.gif" then it will be a broken link if the context root / app name is changed. Using relative path is not always preferrable because a context root can be multi-path (eg: /mysite/myapp)
So far I've tried using <c:url var="root" value="/"/> which works alright (${root} will give the context-root /myapp/), however if this is the very first time user is visiting the site (or if cookie is cleaned on the browser), the value assigned to ${root} became something like /myapp/;jsessionid=019762g1hk3781kh98uihilho and it breaks the images/css reference. Is there any other better way than this?
So far I've tried using <c:url var="root" value="/"/> which works alright (${root} will give the context-root /myapp/)
This is not the right way. The <c:url> should be applied on every single URL individually.
You'd better use
<c:set var="root" value="${pageContext.request.contextPath}" />
See also:
Browser can't access/find relative resources like CSS, images and links when calling a Servlet which forwards to a JSP
I want to print a list of absolute URLs in a Spring JSP, for an internal user to pick from. However, the page is rendered with the current URL prepended.
For Example I want to a link to www.anothersite.com, but the links comes out as http://localhost:8080/myapp/www.anothersite.com on the page
What am I doing wrong? Both lines below have the same result.
<c:forEach items="${listAppURLForm}" var="nextURL">
<li>
</c:out>>${nextURL.link}
<a href=${nextURL.link}>${nextURL.link}</a>
</li>
</c:forEach>
There's a misconception. An URL like www.example.com is definitely not an absolute URL. The URI scheme is completely missing. You need to prepend the URL with the desired scheme to make it really an absolute URL, like so http://www.example.com.
If you can't edit the URLs directly in the list, then you'd need to prefix it in HTML instead.
${nextURL.link}
You might want to perform a ${fn:startsWith()} check beforehand to prevent duplicate schemes.
When jsp:include or jsp:forward is executed, the included page or forwarded
page will see the original request object, with the original parameters
augmented with the new parameters and new values taking precedence over
existing values when applicable.
For example, if the request has a parameter
Lname=abc and a parameter Lname=xyz is
specified for forward, the forwarded request
will have Lname=xyz, abc.
The new parameter has precedence
Please explain with an example.
Lets understand main difference between jsp:include and jsp:forward.
jsp:include, includes the page inside the current page. So the included page will appear exactly where u have added the jsp command inside the current page.
e.g.
...
<jsp:include page="inc/include.jsp" />
...
but jsp:forward will forward the current page to the forwarded page. Meaning when the current page is called the forwarded page is called immediately after.
e.g.
if (...) { forwarded to pageOne.jsp }
else if (...) { forwarded to pageTwo.jsp }
Now following is the way we can access parameters for both jsp:forward and jsp:include.
jsp:include : we can access parameters using param not by request object.
e.g.
${param.param1}
while in jsp:forward : we can access parameters using request object.
e.g.
request.getParameter("param1")
This is what the main difference between the parameters in jsp:inlcude and jsp:forward. And thus, we are getting new value in jsp:forward (say Lname=xyz).
example:
say we have a parameter Lname=xyz in request object. In current jsp it can be accessed using below code
<%= (String)request.getParameter("Lname"); %> <%-- This will print xyz on jsp --%>
Now, further the same parameter name can be used with jsp:forward, like...
<jsp:forward page="fwd/pageOne.jsp?Lname=abc" />
or
<jsp:forward page="fwd/pageOne.jsp">
<param name="Lname" value="abc"/>
</jsp:forward>
This will result Lname=abc in fwd/pageOne.jsp using request.getParameter() method.
Thanks
I have a controller bound the URL: "/ruleManagement".
Inside my JSP, I have a form that forwards (on submit) to "ruleManagement/save" url. When there are errors with the input fields, I want it to return back the original form View. This is where the problem starts...
Problem 1) Now that the URL is "/ruleManagement/save", my form submit now points to "/ruleManagement/ruleManagement/save".
Problem 2) I tried using spring:url tag to generate the absolute paths for me, which usually works great. But when I put a spring:url tag inside of a tag, the spring:url tag does not get parsed correctly.
<form:form action="<spring:url value='/ruleManagement/save' ...>" method="post">
When I analyze the DOM after the page loads, my form tag looks something like:
<form action='<spring:url value="/ruleManagement/save" />' ... >
If I don't use the spring:url tag, and instead use just "/ruleManagement/save", the url generated excludes my application name in the url, which is also wrong.
How do I generate a consistent URL pattern across all Views regardless of path? If the answer is "using spring:url", how do I get that content inside a form:form tag?
Custom tags in JSP can't be used in attributes of other custom tags, so you need to store intermediate result in a request attribute (using var to redirect output of the tag to the request attribute is a common idiom supported by many tags):
<spring:url var = "action" value='/ruleManagement/save' ... />
<form:form action="${action}" method="post">
I too would love to be able to generate a consistent URL path across all Views! Is this possible with <spring:url .../>.
To answer your second question & tacking on to axtavt's answer, embed the <spring:url ... /> into the form action after adding the property htmlEscape="true"
Example: <form:form action="<spring:url value="/ruleManagement/save" htmlEscape="true" .../>" method="post">