Click here to Skip to main content
13,357,973 members (55,319 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


10 bookmarked
Posted 19 Sep 2013

Spring MVC, Maven, Twitter Bootstrap example

, 24 Sep 2013
Rate this:
Please Sign up or sign in to vote.
Spring MVC, Maven, Twitter Bootstrap example.


I was asked to, as an introduction example for a new job, to create a project which should:

  • be Java Spring based
  • use Maven and Twitter bootstrap 
  • run on Tomcat 
  • show a page to enter user name and email, both validated (in case of validation failure it should preserve user entered values in input boxes)
  • show a page of entered users
  • support non English characters 


I'm completely new to Java / Maven / Spring word, so I decided to go through the templates available on the internet. Each and every one of them was missing at least one required feature. So I decided to contribute to the overall mess with my own bit ;) 

Using the code 

You can find the result of my work in the attached file. I'm going to make a brief description of every important feature, which problems I encountered and how I've solved them. 

Problem 0:) 

On the internet there are too many of half cooked and rather wrongly named templates which do much less then they promise.  OK, finally I've found following template, which is clearly base of my work: and from that specifically springmvc subproject. The problem is, doc for it are in Czech language.  

Problem 1: Twitter Bootstrap) 

This example is not using Twitter Bootstrap. Adding it is not so easy. There are many Maven packages of Bootstrap. One of them is It promises taglib interface like: 

<%@ taglib uri="" prefix="bs" 

You can then use the tags as specified in the file. For example: 

<bs:button text="Go" />

Looks good, so I started messing with pom.xml of Maven. But Bootstrap in turn requires jQuery. There is efsavage version of jQuery for Maven. I was not able to make Maven happy regarding this jQuery dependency. I got feeling that Maven is not haven at all. Not to mention it is not clear how to get correct css / js into your resulting web page... Well, I'm missing something crucial with this project, should they improve their doc? Who knows, I need to solve my problem. 

So there's another bootstrap/jQuery Maven package on Maven is happy about both dependencies, but how to get css / js into web page? Easy, if you know how to do it. I found following solution: 

  1. add Maven dependency on webjars-locator along with dependency on jquery and bootstrap (pom.xml)  


2. add code to locate css / js filen into jsp pages (index.jsp)  

<link rel='stylesheet' href='<%= org.webjars.AssetLocator.getWebJarPath("css/bootstrap.min.css") %>'>
<link rel='stylesheet' href='<%= org.webjars.AssetLocator.getWebJarPath("css/bootstrap-theme.min.css") %>'>
<script type='text/javascript' src='<%= org.webjars.AssetLocator.getWebJarPath("jquery.min.js") %>'></script>
<script type='text/javascript' src='<%= org.webjars.AssetLocator.getWebJarPath("js/bootstrap.min.js") %>'></script>

Problem 2: Email Validation)

1. add yet another dependency to Maven (springmvc/pom.xml)  


2. Annotate model's class member by hybernate/email validator (  

private String email;   

Problem 3: Input text localisation 

I've put magic filter into web.xml and localised characters are handled correctly. Some people say that jsp and java files have to be saved in UTF-8, I do not know, this filter seems necessary anyway: 


Problem 4: Preserving user entered values in case of validation error)    

This had to be fixed by putting following code into editUser.jsp: 

<form:form method="post" modelAttribute="user">
     <td><form:input type="text" path="name" /></td>
     <td><form:errors path="name" cssClass="red" /></td>
     <td><form:input type="text" path="email"/></td>
     <td><form:errors path="email" cssClass="red" /></td>
<input type="submit" />
<input type="reset" />

There is crucial to use <form:form ... modelAttribure="user"> which must correspond to modelAttribute in code: 

@RequestMapping(method = RequestMethod.GET)
public String setupForm(ModelMap model) {
    model.addAttribute("user", new User());
    return "editUser"; //viewname

@RequestMapping(method = RequestMethod.POST)
public String processSubmit( @ModelAttribute("user") 
  @Valid User user, BindingResult result, SessionStatus status) {


By giving you my problems and their solution I'm also giving you enough information how whole thing work without loosing time on stating obvious. At least for somebody who has some extended experience with programming. And it gives you example which works correctly in mentioned areas and not just working somehow.

Points of Interest 

I've removed hibernate to store values into database. I've skipped how to configure Maven to directly store resulting war file into Tomcat by using MVN tomcat:deploy. There are plenty of tutorials on those subjects, and none of them work for me yet, so I'll update this article when they do. 


  • None so far. 


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior) Nexthink
Switzerland Switzerland
Please have a look at

You may also be interested in...

Comments and Discussions

QuestionNo resource files loaded. Pin
Member 43308512-Dec-14 22:45
memberMember 43308512-Dec-14 22:45 
QuestionBootstrap as a maven dependancy Pin
Stuart Paton23-Sep-13 12:15
memberStuart Paton23-Sep-13 12:15 
AnswerRe: Bootstrap as a maven dependancy Pin
jan.mach7124-Sep-13 13:12
memberjan.mach7124-Sep-13 13:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 24 Sep 2013
Article Copyright 2013 by jan.mach71
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid