Posts

Showing posts with the label Spring Mvc

Add Context Path To Spring Boot Application

Answer : Why are you trying to roll your own solution. Spring-boot already supports that. If you don't already have one, add an application.properties file to src\main\resources . In that properties file, add 2 properties: server.contextPath=/mainstay server.port=12378 UPDATE (Spring Boot 2.0) As of Spring Boot 2.0 (due to the support of both Spring MVC and Spring WebFlux) the contextPath has been changed to the following: server.servlet.contextPath=/mainstay You can then remove your configuration for the custom servlet container. If you need to do some post processing on the container you can add a EmbeddedServletContainerCustomizer implementation to your configuration (for instance to add the error pages). Basically the properties inside the application.properties serve as a default you can always override them by using another application.properties next to the artifact you deliver or by adding JVM parameters ( -Dserver.port=6666 ). See also The Reference...

Can Spring Security Use @PreAuthorize On Spring Controllers Methods?

Answer : Yes, it works fine. You need <security:global-method-security pre-post-annotations="enabled" /> in ...-servlet.xml . It also requires CGLIB proxies, so either your controllers shouldn't have interfaces, or you should use proxy-target-class = true . See Spring Security FAQ (emphasis mine). In a Spring web application, the application context which holds the Spring MVC beans for the dispatcher servlet is often separate from the main application context. It is often defined in a file called myapp-servlet.xml, where “myapp” is the name assigned to the Spring DispatcherServlet in web.xml. An application can have multiple DispatcherServlets, each with its own isolated application context. The beans in these “child” contexts are not visible to the rest of the application. The “parent” application context is loaded by the ContextLoaderListener you define in your web.xml and is visible to all the child contexts. This parent context is u...

Alternatives To JSP For Spring MVC View Layer

Answer : I recently discovered Thymeleaf. It looks to be a complete replacement for JSPs and has integration with Spring MVC. The template approach looks more like HTML and may be more palatable to your UI designers. They have a small write-up that compares the two solutions side-by-side. In the standard Java EE API, the only alternative to JSP is Facelets. As far now (2010) JSF is the only MVC framework which natively supports Facelets. Spring MVC supports out of the box only JSP, but it has a configurable view resolver which allows you to use Facelets anyway. Other candiates are 3rd party templating frameworks such as Velocity, Freemarker, and Thymeleaf which can be configured as a view technology for Spring MVC. Spring documentation has integration examples with Velocity and Freemarker. I recently started going with plain HTML and jQuery for presentation with Spring MVC only creating a JSON view. So far it's going quite well and even though I have to do the javascri...