I would like to use GSP views instead of JSP/JSTL views in a plain old Spring MVC application. I have added a groovy.servlet.TemplateServlet to web.xml like this:
<servlet>
<servlet-name>GroovyTemplate</servlet-name>
<servlet-class>groovy.servlet.TemplateServlet</servlet-class>
<init-param>
<param-name>template.engine</param-name>
<param-value>groovy.text.GStringTemplateEngine</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>GroovyTemplate</servlet-name>
<url-pattern>*.gsp</url-pattern>
</servlet-mapping>
And setup a Spring InternalResourceViewResolver to load the GPS files. Upto this point it all works fine, but to expose the values in the Model to the template I had to do some tricks (subclassing TemplateServlet and adding them to the ServletBinding).
Now my next obstacle is that JSTL by default escapes XML when using the c:out tag and Grails has the notion of codecs to automatically escape values used in a GSP. The template method described above does not escape by default, which requires the developers to be very careful to avoid XSS vulnerabilities.
Is there another (better) way to use GSP including automatic escaping in a plain Spring MVC application without using Grails?
Today GSP for Spring Boot was just released. This provides the ability to use GSP instead of JSP in a regular Spring web application. You can see an example here: https://github.com/grails/grails-boot/blob/master/sample-apps/gsp/script/templates/index.gsp
Instead of using a TemplateServlet, you could have also used a GroovyPagesServlet for that purpose (I haven't tested that, just looked at Grails' web.xml and the class' code).
The latter requires a Spring bean to be setup, named groovyPagesTemplateEngine, and typed GroovyPagesTemplateEngine (GStringTemplateEngine in this case).
Configuring the view layer with an InternalResourceViewResolver is correct. You'll have assigned a GroovyPageView.
GSPs, by default, are not configured to perform HTML output escaping. To configure that, change grails.views.default.codec from none to html in Config.groovy. See this article for more information.
We have extracted GSP from Grails, customized it for Spring MVC applications and improved configuration support. Please see our tool Rabbtor. We don't provide it open source, but usage is free of charge and we have been using it in our own applications. GSP for Spring Boot depends on Spring boot, it is not maintained and some tag libraries depend on Grails. We removed these dependencies, created our own custom tag libraries which better suit Spring MVC apps.Most tag libraries are supported and have similar implemantations to Spring JSP tags.A data-bound form tag library is provided and also you can register your tag libs or packages.
Related
I have followed the Spring example Serving Mobile Web Content with Spring MVC and got it working.
Now I would like to replace greeting.html with a simple static page (and replace Thymeleaf with a simpler view handler).
What is the easiest approach in this particular case?
I am new to Java, the probable solutions in web use web.xml, whereas this example does not use web.xml and it does not seem to be the recommended approach in Spring Boot documentation either. So, use #EnableAutoConfiguration with some overriding? How?
All you have to do is put the static HTML file in a place where Spring Boot will automatically look for static resources. This part of the documentation provides all the details.
So for example you can place your greeting.html under /src/main/resources/static/ and you will be able to access it at http://localhost:8080/greeting.html (that's if you have not configured a different port of the root path of the servlet context)
While reading the spring reference specification i hit upon the following: "When using the Java Standard Tag Library you must use a special view class, the JstlView, as JSTL needs some preparation before things such as the i18N features will work."
I am able to use JSTL in my spring application without configuring JSTLView as a view resolver. Given that, what is the disadvantage of not using the spring provided JSTLView resolver (apart from the i18N features the spec refers to)?
Could not find any explanation of the same in the spec in detail.
I am able to use JSTL in my spring application without configuring JSTLView as a view resolver
No, you application did use the JSTLView for view resolver if the InternalResourceViewResolver is set and JSTL library is present in classpath.
I want to use stripes tag library specifically layout features. My rest of the project is handled by spring mvc.
Is this possible to use stripes without adding any stripes filter?
In the web.xml you can configure the Stripes DispatcherServlet with an url-pattern so that Stripes front controller will only respond to certain URL's. Another option is, to use separate context-paths for Stripes and Spring MVC.
We've got some Pure Servlets (pure Java classes following the Servlet API, no framework) that we'd like to include in a project that relies heavily on the Spring Framework.
What's the best way of including these servlets in the project, when all the new code we're writing is making heavy use of Spring 3 features?
your servlet container can run multiple servlets, spring is just one of them. why not just include your servlets in the web.xml and see if it works? it should work. spring is not that intrusive, yet (but obviously it already intruded the minds of many developers)
If you declare servlets in the web.xml, alongside the Spring front controller, it most certainly will work.
You just have to be careful when you declare which URLs map to the servlets. If you send "/*" to the Spring front controller, none of your requests will reach your other servlets. Be specific about what you need to send to each one.
As you might know, servlets cannot be configured as Spring beans. If your question is about colloborating with spring beans from a servlet, do refer this thread and also this
Spring provides a couple of classes to make this bridging easier.
ServletForwardingController
Spring Controller implementation that
forwards to a named servlet, i.e. the
"servlet-name" in web.xml rather than
a URL path mapping. A target servlet
doesn't even need a "servlet-mapping"
in web.xml in the first place: A
"servlet" declaration is sufficient.
Useful to invoke an existing servlet
via Spring's dispatching
infrastructure, for example to apply
Spring HandlerInterceptors to its
requests.
ServletWrappingController
Spring Controller implementation that
wraps a servlet instance which it
manages internally. Such a wrapped
servlet is not known outside of this
controller; its entire lifecycle is
covered here (in contrast to
ServletForwardingController).
I'm developing a web application using Spring MVC 3.0 and looking for a ready-made solution, if any, or a "best practices" reference for a url/action mapping and routing system that can achieve the following:
REST-friendly controller / method name to view mapping. The current mapping implementation translates the request to a view name, which may be problematic when using several parameters and is incompatible with REST urls
A service that accepts the name of a controller, a method and arguments values and renders the URL that's represented by them
Integration with Spring Security that can allow me to check for a given URL whether the current user is allowed to access it, so that I can decide whether or not to render a URL
A menuing system based on the above that can define menues composed of these actions and render them to page
Basically what I need is the ability to define URLs in one centralized place, so that changing a URL (during development; I'm aware of the don't-change-live-urls idea :) ) does not mean looking up and changing that URL in a zillion pages.
Any directions to such an existing solution / tutorial / guide would be great.
Thanjs
This is a feature I really miss in Spring MVC.
That's why I created the springmcv-router project, basically a port of PlayFramework's Router implementation in Spring MVC (HandlerMapping + HandlerAdapter).
I'm heavily using it in several real-world projects and the Router implementation itself is reliable.
Try using Spring Roo. It utilizes many best practices for spring MVC, and it has a scaffolding feature that automatically maintains a menu, jsp's and all the CRUD methods of a controller.
Setting up Spring Security with Roo is as simple as typing "security setup".
Hope this is helpful.