Sunday, October 7, 2012

Spring MVC, Security + Hibernate + DWR - Page 1

Spring MVC, Security + Hibernate + DWR, Let's PLay

OK, that is a lot, but it was fun getting those guys working together in harmony ;)

Spring has proven to be a solid playground and a unique platform, here how the parts will act:
So, first things first, Maven, all the artifacts(shiny name for a jar/war files) that are required in in this example relies on Maven, all but dwr, by the time of writing this blog, "At the time of our last development build (3.0 RC2) we did not have a process in place to upload artifacts to Maven Central", the dwr site says.
This issue is easy to fix, we will install dwr.jar manually to our local repository, even better, if you are using archiva; upload the artifact.
mvn install:install-file -Dfile=path/to/dwr.jar -DgroupId=org.directwebremoting -DartifactId=dwr -Dversion=3.0RC2 -Dpackaging=jar
By now we can go ahead and create a new project using maven archetype.
mvn archetype:generate
we will start with a plain java web archetype, choose maven-archetype-webapp or number 224 as per my Maven, i ended up with those parameters
groupId: com.mtz.spring
artifactId: SpringHibernateDWR
version: 1.0.0
package: com.mtz.spring

Enable Java web 2.5 specifications:

edit /WEB-INF/web.xml
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "" >

<web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" version="2.5">

Maven Dependencies:

edit the pom.xml to include the following dependencies


<!--End Spring-->

<!--Spring Security-->
<!--End Spring Security-->

<!-- MySQL database driver -->
<!-- End MySQL-->

<!--End DWR-->

<!-- Hibernate framework -->
<!-- Hibernate library dependecy end -->

<!-- apache commons -->
<!-- end apache commons -->
under the resources directory create two files.
# Root logger option
log4j.rootLogger=DEBUG, stdout
# Direct log messages to stdout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
org.apache.catalina.core.ContainerBase.[Catalina].level = DEBUG
org.apache.catalina.core.ContainerBase.[Catalina].handlers = java.util.logging.ConsoleHandler
i wanted to see a lot of loggings, so i set both to DEBUG
now build your project with maven and make sure everything is OK.
mvn clean package

Spring MVC:

in your web.xml add
this is basically,
1- initializing the the Spring context using ContextLoaderListener which will look for a file named applicationContext.xml under WEB-INF.
2- loads the Spring MVC dispatcher servlet, the dispatcher will look for a file named spring-mvc.xml under WEB-INF and it also associate *.html requests to it.
3- the container will use redirect.jsp as the default landing page.


we will just create a bean-less file that will be used later on, create the file under /WEB-INF
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""



create a file with that name under /WEB-INF with the following content
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

    <context:component-scan base-package="com.mtz.spring.mvc.controller"/>

    <bean id="viewResolver"
          p:suffix=".jsp" />


this file configures Spring MVC to
1-Look for classes annotated with @controller in any class under com.mtz.spring.mvc.controller package.
2-add a basic view resolver that will map strings returned from the controller to a jsp file.

under the package com.mtz.spring.mvc.controller create a new class called HelloController with the following content
package com.mtz.spring.mvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
public class HelloController {
 @RequestMapping(method = RequestMethod.GET)
 public String printWelcome(ModelMap model) {
  model.addAttribute("message", "Spring + Hibernate + DWR, Hello World");
  return "hello";
here, Spring MVC will bind /welcome.html requests to be handled by this controller, the controller will return its view name which is "hello" then the view resolver will map it to /WEB-INF/jsp/hello.jsp


under /WEB-INF create a directory named jsp and then create a jsp file called hello.jsp with the following content
<%@page contentType="text/html" pageEncoding="UTF-8"%>

Hello! This is the default welcome page for a Spring Web MVC project.

Message: ${message}

this jsp will print the message that was sent from the controller.


create a new jsp under webapp directory with the following content
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<% response.sendRedirect("welcome.html"); %>
the redirect.jsp will redirect the requests from / to /welcome.html which will trigger the Spring MVC controller to work.

Test it

now package your project and deploy it, navigate to http://localhost:8080/SpringHibernateDWR/, if everything is correct, you should see this page.

Next - Configuring Hibernate


sarabjeet said...

i really like that you are giving information on core and advance java concepts. Being enrolled at i found your information very helpful indeed.thanks for it.

moutaz salem said...

thanks a lot sarabjeet, it is my pleasure

prathap kumar said...

Great Article

Hibernate Training
Hibernate Online Training
Hibernate Training Courses
Java J2EE Training Institutes in Chennai
Java Training in Chennai

Java Training

Hibernate Dialect
Hibernate Interview Questions

John Alert said...

Java Spring Hibernate Training Institutes in Chennai J2EE Training Institutes in Chennai J2EE Training Institutes in Chennai Java Spring Hibernate Training Institutes in Chennai | Hibernate Training Institutes in Chennai Core Java Training Institutes in Chennai Core Java Training Institutes in Chennai

Hibernate Online Training Hibernate Online Training Hibernate Training in Chennai Hibernate Training in Chennai Java Online Training Java Online Training Hibernate Training Institutes in ChennaiHibernate Training Institutes in Chennai