Spring MVC: Relative URL problems - java

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">

Related

forward from spring jsp to spring jsp

in catalog
webapp/WEB-INF/views located my jsp pages
I want to forward from 1.jsp to 2.jsp
in 1.jsp I write
${candidate.name}
but it doesn't work.
How to fix it?
Use <c:url and then the value attribute of <c:url is set to the href link to the other JSP. So for example, it would be:
${candidate.name}
I had used it in the following manner in my webapps (where circuits is a folder under /WEB-INF/views):
Edit Circuit
A relative url should start with a / character
Change the anchor tag as below and try again
${candidate.name}
Reference Link

how to add <div> tag data to arraylist in java

i want pass data /innerHTML to the another jsp page ,so how could i pass it ?is it possible to use arraylist or any other way to pass this large data to another page?
Is it possible add tag data to the arraylist in javascript?or how can we pass div tag innerhtml to the next page in jsp?
i want pass data /innerHTML to the another jsp page
This seems wrong approach to me,because the main adavantage of Jsp is actually writing HTML on demand,where ever you need it.
Pass the essesntial data which decides the html to the jsp using the session or request but passing html is a bad idea.
Instead of passing the HTML view data, just pass what needs to be displayed and let the target JSP decide how to render it. For example, if you wanted the target JSP to print a welcome message for the logged-in user; then instead of passing the HTML
target.jsp?msg=<div><p>Welcome <b>John</b></p></div>
You would pass a request attribute named user as
request.setAttribute("user", "John");
which can now be accessed in your target JSP (using EL) as
<c:if test="${not empty user}">
<div><p>Welcome <b>${user}</b></p></div>
</c:if>
In your case, your Cart along with its List<CartItems>, should be stored in HttpSession. This way the shopping data remains accessible to any JSP/Servlet that needs to update the cart.
Wrap your div tag div tag. and get the html tags from ouside wrapper div tag.
<div id="wrapper">
<div id="what_you_want">
some contents...
</div>
</div>
$("#wrapper").html() - It contains whole html code in wrapper div.
Try some Javascript. If you really insist on using kinda abnormal methods (I do too sometimes), you can create an input field of type hidden and put the cart info into that input as a delimited string.

How to check the URL pattern inside JSP?

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>

c:url tag includes jsession id query string

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

Write Multiple Full HTML 'Files' to Single Output Stream?

I'm writing a testing utility- I want to show multiple finished HTML "pages" in a single browser window.
Is this possible? I'm using Java Servlets.
For example, normally the user goes to a utility screen, fills in a bunch of fields and POSTS this to my servlet, which builds up a full HTML stream based on their input and writes it to HttpServletResponse.getWriter(). When the user views source, they get a <html> ... </html>.
What I want to do is allow users to request multiple "screens" and get the results in a single web page where you'd scroll down to see the 2nd, 3rd, etc. screens, maybe there is some kind of divider in between. I thought of frames or iframes, but didn't have luck. I have seen where I can write my big html stream to a javascript variable, then use document.write to dump it into the iframe. But that seems pretty awkward, and I'd have to be really careful about escaping quotes and stuff.
You will have to use iframes or frames to do this. A single web page can only contain one set of html tags and thus one html page.
Another idea would be to render the page by your script and then capture a picture of it and then have a page containing images. You will of course loose all interaction with the page.
I'm not sure what you're trying with your frames, but I imagine frames should work OK for what you've described.
Instead of trying to post to more than one URL from your form, you just post to a servlet that returns a page with the frameset, and each frame has a source that points to one of the URLs you want to test. For example:
<form action="testServlet" method="post">
<input type="text" name="someValue" />
</form>
The testServlet then returns a page with this content:
<frameset rows="33%,33%,33%">
<frame src="testUrl1?someValue=value">
<frame src="testUrl2?someValue=value">
<frame src="testUrl3?someValue=value">
</frameset>
The only problem with this is that you're doing a GET instead of a POST, but that's easy to get around. All you would need do is to implement the doGet method within your servlets and just call doPost from within doGet.
Just leave out the <html>/</html> tags for each page and wrap the whole thing inside a single large ....
Like this maybe:
<html>
[page1Content]
<hr />
[page2Content]
<hr />
[page3Content]
<hr />
</html>

Categories

Resources