Links (href) not relative to application in Spring MVC? - java

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

Related

How to read form parameter data using JSP without using scriptlet

I want to read the form parameter data using scriptlet in #jsp. But i do not want to use any java code in JSP. Then do i need Expression Language or #JSTL or what?
Just noting this here in case anyone else has a similar issue.
If you're directing a request directly to a JSP, using Apache Tomcat web.xml configuration, then ${requestScope.attr} doesn't seem to work, instead ${param.attr} contains the request attribute attr.

Spring PetClinic <spring:url ....../> encoding for very url

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

Spring and dynamic inclusion of JSP files

I'm starting building web apps in Spring 3 (and in J2EE) in general.
Looking at the petclinic example I've seen that the programmer creates many JSP pieces, like header, includes, footer and then stitches them together using static inclusion. Anyway what I'd like is that I may have a base page, like Base.jsp and be able to include things like this:
<body>
<jsp:include page="${subpage}"></jsp:include>
</body>
the reason is that I'd like a main page, then being able to put in the ModelAndView returned by the controller which parts of the pages display in each situation (with the data attached to it). This works, but it gives no errors in case ${subpage} is not found, the jsp name is wrong or missing. I'd like more error checking...
Is this the best and recommended way to do this? And if this seems a good idea for what I've in mind, what's the correct way of doing it?
You might want to use Apache Tiles 2 integration for managing your JSP files. Spring has good integration support Apache Tiles. It also shows if there's an error in your page. I've put an example of it at http://krams915.blogspot.com/2010/12/spring-mvc-3-tiles-2-integration.html
It appears you have additional quotes in your subpage. Get rid of them. For example:
<c:set var="subpage" value="/jsp/index.jsp" />
If you have to set it in a controller or servlet - just use request.setAttribute("subpage", "/jsp/index.jsp")
For error checking you can use:
<c:catch var="myException">
<c:import url="${subpage}" />
</c:catch>
and later you can check it with:
<c:if test="${myException != null}">
...
</c:if>
Take a look at Sitemesh (http://www.opensymphony.com/sitemesh). It is a servlet filter-based page layout system that is easy to use. I have done a number of projects using it with Spring MVC and it worked very well.

How to hide application context from urls?

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

how can I integrate formBean to my Spring MVC?

My application is using Spring MVC.
On the way from the Controller to the view we are taking adventage of this framwork by creating a bean of the model that is used to display the relevant properties through the JSP.
On the way back however, meaning after submitting a form back to the controller, it is using a raw HTTPRequest instead of a structured formBean.
I think that this is a disadvantege so I am looking for way to insert this to our MVC model.
I saw in this link some way that Spring MVC handles it.
by adding to the JSP binding such as:
<spring:bind path="command"> <font color="red"> <b><c:out value="${status.errorMessage}"/></b>
</font>
</spring:bind>
and to the controller:
protected ModelAndView onSubmit(Object command) throws ServletException
{ Widget widget = (Widget) command;
...
}
But this solution is not good for our implementation - I don't want to add anything to the JSP and in addition . some of the parameters that are added to the httprequest are done in javascript code.
Therefore I am looking for a solution that can create formBean out of the form parameters while the mapping is not defined on the JSP but elsewhere (in some dedicated xml obviously).
Any ideas?
The simplest way to use a form (command) bean in Spring is to write a controller that extends SimpleFormController.
A quick Google shows a number of basic tutorials - for example:
http://www.vaannila.com/spring/spring-simple-form-controller-1.html

Categories