Java Ninja Chronicles By Norris Shelton

Things I learned in the pursuit of code

I have a Springframework project that had a property value that contained the host of the server that I was going to be deployed on. There was more to it, because there were profiles, etc, but that is the gist of it. There is a value in a property file that I need.

The properties file was declared as:

<util:properties id="commonProperties" location=""/>

The contents of the properties file contained:


To use the value in the jsp, pull in the Spring tags by:

<%@ taglib prefix="spring" uri="" %>

Tie it all together by

<spring:eval var="loginUrl" expression="@commonProperties.hostUrl"/>
<a href="http://${loginUrl}/hub/login.jsp">Return to login</a>

January 27th, 2014

Posted In: JSP, SpEl, Spring


The servlet 3.0 spec defines a way to share webapp JSP resources via a dependent jar (See Servlet 3.0 spec, section 10.5)

If you have a Maven dependency that is a jar, you can package resources and have them available to webapps that use them as a dependency.

To share, /css/test.css, you need to place it in:

Any webapp that depends on that jar can access /css/test.css as if it was in the root of it’s own webapp.

<link rel="stylesheet" type="text/css" href="<c:url value="/css/test.css" />">

Caveat, IntelliJ does not resolve /css/test.css as being within the current webapp. There was a ticket that was filed in 2011, but it doesn’t appear that it will ever be fixed.

Note, that if you are trying to use the included file as a tag file, it will need to have /WEB-INF/tags at the front of the included path.

January 24th, 2014

Posted In: IntelliJ, Maven, Servlet Spec

Leave a Comment

There will be sometimes in your code that you need access to the Springframework security principal object. Here is how you get it. You will need to create a Spring configuration class and annotate it with @EnableWebMvcSecurity

package com.cdi.igs.hub.spring;

import org.springframework.context.annotation.Configuration;

 * Spring Security configuration.
 * @author norris.shelton
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    // nothing needed here

The documentation then says to add a method parameter to a Spring MVC controller method with @AuthenticationPrincipal and you are done, like the following:

    @RequestMapping(value = "/{personId}", method = RequestMethod.GET)
    public ModelAndView getPersonAccount(@PathVariable int personId, @AuthenticationPrincipal User user) {
        ModelAndView modelAndView =  new ModelAndView("dashboard/account");
        modelAndView.addObject("person", personRepository.findOne(personId));
        return modelAndView;

What they don’t tell you is that you need to configure a AuthenticationPrincipalArgumentResolver. Nor they tell you how to do it. This is the missing piece.

            <bean class=""/>

January 14th, 2014

Posted In: Spring, Spring MVC, Spring Security

Tags: , , , , ,


WP to LinkedIn Auto Publish Powered By :