I am implementing the Spring Pet Clinic Sample project given here
Implementation here
In the home page all the urls are encoded by <spring:url /> tag. I just want to confirm that is it a good programming practice to encode the links, even the simple navigation urls by the spring:url tag or is it done for a specific reason?.
Yes, it's a good practice. That tag is a Spring JSP tag for creating URLs with enhancements for JSTL c:url.
<c:url> tag is used to create a url and it is helpful in the case when cookies is turned off by the client, and you would be required to rewrite URLs that will be returned from a jsp page.
The rewritten URL will encode the session ID if necessary to provide persistent sessions.
For example, your URL will be displayed as:
<c:url value="a.jsp">
/context/a.jsp // when cookies is enabled
/context/a.jsp;jsessionid=B01F432.... // when cookie is disabled
This way, the servlet container can track the user requests. Another important thing is that c:url will prefix the context root, so you don't need to write your root context prefix everywhere.
Like I said, <spring:url> contains some enhancements over JSTL like encoded URI template variables for example.
<spring:url value="/url/path/{variableName}">
<spring:param name="variableName" value="more than JSTL c:url" />
</spring:url>
Results in: /currentApplicationContext/url/path/more%20than%20JSTL%20c%3Aurl
See more details in here: http://docs.spring.io/spring/docs/3.1.4.RELEASE/javadoc-api/org/springframework/web/servlet/tags/UrlTag.html
Related
I'm creating some pages for a web application, I currently have some pages with a brief description of a company and a link to allow them to log in to access more information. When the user logs in, they should be redirected to the more detailed version of the page.
In order to do this, when the user clicks on the log in link, I save the company name (as it is used in the url) to the session and access it when the user is logging in to figure out where to redirect them to.
This works fine but the only problem is these pages are using simple file name controllers and I don't want to implement controllers for them just so I can set this attribute. I've found a fix by using scriptlets in my jsp:
<a href="<c:url value='/login.jsp' />"
onclick="<% session.setAttribute( "partner", "companyName" ); %>"
>
Click here to log in
</a>
Now after some reading on SO and other resources I know that use of these scriptlets is highly discouraged, but I'm at a loss as to how to fix this problem without having to implement a controller to handle this simple problem. Any suggestions would be greatly appreciated.
use jstl
<c:set var="partner" value="companyName" scope="session" />
I am in the process of changing a project which used a simple java HTTPServer on port 8080 to that which uses the Spring MVC servlet framework.
There are lots of links in the application like so:
Manage rules<br/>
Which worked fine using requests such as:
http://localhost:8080/send
However, these now don't work unless I put the servlet name in front like so:
"Manage rules<br/>"
Just wondering is there any way around this, or is it a matter of just changing all the href links to add the servlet name in front of them?
Note that I dont have to add the servlet name in my #RequestMapping calls at the start of methods, its only links that are the problem. E.g. this works fine without adding the servlet name in front
#RequestMapping(value = "/send", method = RequestMethod.GET)
If you use JSPs in your view layer, use a tag library that has a link tag (or write one yourself). Basically, the link tag has to create the url by doing
HttpServletResponse.encodeURL(originalUrl)
The <c:url> tag in JSTL does that, but it only generates the URL, not the link tag, but it can get you there like this:
<c:url value="your/relative/url" var="somevar" />
Link Text
I have an app that may run at http://serverA/m/ or http://serverA/mobile/. I have a shared header with a search form that needs to go to http://serverA/installationName/search.
However, if I use <form action="/search"> it goes to the root of the server, not the tomcat application.
If I use <form action="search"> it goes to a path relative to the current page. (i.e http://serverA/m/someOtherPage/search
I've tried <c:url value="search"> and <c:url value="/search"> but neither of them seem to work.
In intelliJ, <c:url value="/search"> gives me "Cannot resolve controller URL /search" even though I have a controller defined with #RequestMapping("/search")
<form action="<c:url value="/search" />" />
Using <c:url> is the way. Ignore what the IDE tells you. They are not good at that. Just try to run it.
Bozho is right. I have used HTML BASE tag too:
<base href="${pageContext.request.scheme}://${pageContext.request.serverName}:${pageContext.request.serverPort}${pageContext.request.contextPath}/" />
If you can put this tag in a few places (ideally in only one JSP) you can get your code cleaner.
You can (apart from other responders hints) also use Spring JSP tag (spring:url) which is modeled after the JSTL c:url tag (see Bozhos reply). The tld reference:
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/spring.tld.html#spring.tld.url
And the bottom of this mvc:resources block for an example use:
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-static-resources
you will not be able to imbed the c:url tag directly in the attribute, if your form tag is a jsp tag (perhaps, <sf:form>).
In that situation I do the following:
<c:url var="someName" value="some uri value"/>
<sf:form path="${someName}" ...>
I am using JSTL c:url tag to define my URL's in the application, something like:
<c:url value"/home" />
But the problem is that it appends the application context to the url, so the link becomes http://appName.come/appName/page while it should be http://appName.come/page.
The link had to be with slash, because it's not relative. I want to prevent the application context from being added or something like that, any ideas?
That's just the sole purpose of c:url: adding the context root and if necessary jsessionid whenever client doesn't support cookies. It also has support for URL-encoding the query parameters by c:param. If you don't want to have that, then just don't use c:url but use a plain HTML <a> element instead.
home
Update: as per the comment you seem to want to have the jsessionid in the URL (do you realize that the sessions are by default not shared between contexts and that you have to configure the serletcontainer accordingly?). In that case, manually set the context attribute.
<c:url context="/" value="/home" />
Is there a cleaner way to do this in a JSP/Struts1 setup ?
... some HTML here ...
EDIT: In admin mode I would like to have access to additional parameters from a form element,
e.g. from the form element:
input type="text" value="Test user" name="Owner"
EDIT 2: Actually, my problem is very similar to the question that was asked in : Conditionally Render In JSP By User
But I don't really get the "pseudo-code" from the likely answer
Is SessionConfig exposed as a bean in your JSP (as part of request / session / Struts Form)?
If it's not, you can expose it. And if it's a static class containing global settings (which, by the looks of it, is a possibility), you can create a small wrapper and put it in the servlet context which you'd then be able to access from Struts tags as scope="application".
Once that's done you can check your condition via Struts tags:
<logic:equal name="sessionConfig" property="adminMode" value="true">
... your HTML here
</logic:equal>
Or, if you're using EL / JSTL, same can be done via <core:if>.
Without more information, it's hard to answer this, but I'd think instead of separate views: one for admin mode, one for normal mode. Extracting the parts of your pages into tiles will help you do this without a lot of pain; see: http://tiles.apache.org/