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 ;)

Why?
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
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">

Maven Dependencies:

edit the pom.xml to include the following dependencies
<dependency>
 <groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <version>3.8.1</version>
 <scope>test</scope>
</dependency>

<dependency>
 <groupId>log4j</groupId>
 <artifactId>log4j</artifactId>
 <version>1.2.17</version>
</dependency>

<!--Spring-->
<dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-orm</artifactId>
 <version>3.0.6.RELEASE</version>
</dependency>
<dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-web</artifactId>
 <version>3.0.6.RELEASE</version>
</dependency>
<dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-webmvc</artifactId>
 <version>3.0.6.RELEASE</version>
</dependency>
<!--End Spring-->

<!--Spring Security-->
<dependency>
 <groupId>org.springframework.security</groupId>
 <artifactId>spring-security-core</artifactId>
 <version>3.0.7.RELEASE</version>
</dependency> 
<dependency>
 <groupId>org.springframework.security</groupId>
 <artifactId>spring-security-web</artifactId>
 <version>3.0.7.RELEASE</version>
</dependency> 
<dependency>
 <groupId>org.springframework.security</groupId>
 <artifactId>spring-security-config</artifactId>
 <version>3.0.7.RELEASE</version>
</dependency>
<!--End Spring Security-->

<!-- MySQL database driver -->
<dependency>
 <groupId>mysql</groupId>
 <artifactId>mysql-connector-java</artifactId>
 <version>5.1.9</version>
</dependency>
<!-- End MySQL-->

<!--DWR-->
<dependency>
 <groupId>org.directwebremoting</groupId>
 <artifactId>dwr</artifactId>
 <version>3.0RC2</version>
 <type>jar</type>
</dependency>
<!--End DWR-->

<!-- Hibernate framework -->
<dependency>
 <groupId>org.hibernate</groupId>
 <artifactId>hibernate-core</artifactId>
 <version>3.6.10.Final</version>
</dependency>
<dependency>
 <groupId>org.hibernate</groupId>
 <artifactId>hibernate-ehcache</artifactId>
 <version>3.6.10.Final</version>
</dependency>
<dependency>
 <groupId>javassist</groupId>
 <artifactId>javassist</artifactId>
 <version>3.12.1.GA</version>
</dependency>
<!-- Hibernate library dependecy end -->

<!-- apache commons -->
<dependency>
 <groupId>commons-logging</groupId>
 <artifactId>commons-logging</artifactId>
 <version>1.1.1</version>
</dependency>
<!-- end apache commons -->
under the resources directory create two files.
log4j.properties
# Root logger option
log4j.rootLogger=DEBUG, stdout
 
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
logging.properties
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
  <context-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
 <servlet-name>spring</servlet-name>
 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 <init-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/spring-mvc.xml</param-value>
 </init-param>
 <load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
 <servlet-name>spring</servlet-name>
 <url-pattern>*.html</url-pattern>
</servlet-mapping>
<welcome-file-list>
	<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
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.

applicationContext.xml

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="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">


</beans>

spring-mvc.xml

create a file with that name under /WEB-INF with the following content
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

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

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />

</beans>



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.

HelloController.java

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;
 
@Controller
@RequestMapping("/welcome")
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

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.

redirect.jsp

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

56 comments:

Moutaz Salem said...

thanks a lot sarabjeet, it is my pleasure

de 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

Mahesh said...

This is an awesome post. Really very informative and creative contents. This concept is a good way to enhance the knowledge.Like it and help me to development very well. Thank you for this brief explanation and very nice information. Well got good knowledge.
Java Training in Gurgaon





Kamal said...

This is an awesome post. Really very informative and creative contents. This concept is a good way to enhance the knowledge.Like it and help me to development very well. Thank you for this brief explanation and very nice information. Well got good knowledge.
Education | English Labs | MBA Talks | Technology

Dipanwita said...

Loved your blog post. Keep it up. java training in chennai

VenuBharath2010@gmail.com said...

Nice Post. It is very informative Pls keep update us more.
Best Java Training in Chennai
Java Certification course in Chennai
Java Training center in Chennai
Java Institute in Chennai
Best Java Training Institute in Chennai with placement
Java coaching center in Chennai

SRI said...


This is most informative and also this post most user friendly and super navigation to all posts... Thank you so much for giving this information to me.. 

best rpa training in chennai |
rpa training in chennai | rpa online training |
rpa training in chennai |
rpa training in bangalore
rpa training in pune
rpa training in marathahalli
rpa training in btm

Unknown said...

Inspiring writings and I greatly admired what you have to say , I hope you continue to provide new ideas for us all and greetings success always for you..Keep update more information.


rpa training in chennai |
best rpa training in chennai
rpa online training
rpa course in bangalore
rpa training in pune
rpa training in marathahalli
rpa training in btm

Mounika said...

Thanks for posting this info. I just want to let you know that I just check out your site and I find it very interesting and informative. I can't wait to read lots of your posts
python course in pune | python course in chennai | python course in Bangalore

sakthi said...


This is most informative and also this post most user friendly and super navigation to all posts... Thank you so much for giving this information to me.

best rpa training in chennai
rpa training in chennai |
rpa online training
rpa course in bangalore
rpa training in pune
rpa training in marathahalli
rpa training in btm

Unknown said...

After seeing your article I want to say that the presentation is very good and also a well-written article with some very good information which is very useful for the readers....thanks for sharing it and do share more posts like this.
Java training in Chennai | Java training in Annanagar | Java training in Chennai

Java training in Chennai | Java training in Bangalore | Java training in Electronic city

nivatha said...

Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging.
Data Science training in rajaji nagar | Data Science Training in Bangalore
Data Science with Python training in chennai
Data Science training in electronic city
Data Science training in USA
Data science training in pune

dwarakesh said...

Hello! This is my first visit to your blog! We are a team of volunteers and starting a new initiative in a community in the same niche. Your blog provided us useful information to work on. You have done an outstanding job.



AWS Training in Bangalore | Amazon Web Services Training in Bangalore

AWS Interview Questions And Answers

Learn Amazon Web Services Tutorial |AWS Tutorials For Beginners

Amazon Web Services Training in OMR , Chennai | Best AWS Training in OMR,Chennai


AWS Training in Chennai |Best Amazon Web Services Training in Chennai

sathya shri said...

Thanks for posting this info. I just want to let you know that I just check out your site and I find it very interesting and informative. I can't wait to read lots of your posts
angularjs Training in bangalore

angularjs Training in bangalore

angularjs Training in chennai

automation anywhere online Training

angularjs interview questions and answers

nikitha josh said...

Very nice blog, Thank you for providing good information.
Aviation Academy in Chennai
Air hostess training in Chennai
Airport management courses in Chennai
Ground staff training in Chennai
Medical coding training in Chennai
Fashion designing courses in Chennai
Interior design courses in Chennai

Vicky Ram said...

Excellent post! keep sharing such a post

Article submission sites
Technology

Priyanka said...

Attend The Python training in bangalore From ExcelR. Practical Python training in bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Python training in bangalore.
python training in bangalore

jvimala said...

Hey, would you mind if I share your blog with my twitter group? There’s a lot of folks that I think would enjoy your content. Please let me know. Thank you.
Java Training in Chennai | J2EE Training in Chennai | Advanced Java Training in Chennai | Core Java Training in Chennai | Java Training institute in Chennai

Priyanka said...

Attend The Data Science Course in Bangalore From ExcelR. Practical Data Science Course in Bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Data Science Course in Bangalore.
ExcelR Data Science Course in Bangalore

akalya said...
This comment has been removed by the author.
eTechno Soft Solutions said...

wonderful thanks for sharing an amazing idea. keep it...

Get Software Testing Training in Bangalore from Real Time Industry Experts with 100% Placement Assistance in MNC Companies. Book your Free Demo with eTechno Soft Solutions.

hari said...

good
nice........
inplant training in chennai
inplant training in chennai
online python internship
online web design
online machine learning internship
online internet of things internship
online cloud computing internship
online Robotics
online penetration testing

greatrockdev said...

You should be a part of a contest for one of the finest websites on the internet. I most certainly will recommend this website!

Mithun said...

The way of Explanation's about the content is ease to understand...Thanks for such a clear flow of content...Keep Doing this amazing works
Java training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery

Mithun said...
This comment has been removed by the author.
Gaurav said...

I really enjoy simply reading all of your weblogs. Simply wanted to inform you that you have people like me who appreciate your work. Definitely a great post. Hats off to you! The information that you have provided is very helpful.
data science course in raipur

yash said...

You actually make it look so easy with your performance but I find this matter to be actually something which I think I would never comprehend. It seems too complicated and extremely broad for me. I'm looking forward for your next post, I’ll try to get the hang of it!
arttificial intelligence course in varanasi

Unknown said...

I am really enjoying reading your well written articles. It looks like you spend a lot of effort and time on your blog. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work.
artificial intelligence course in mangalore

360digitmgas said...

Actually I read it yesterday but I had some thoughts about it and today I wanted to read it again because it is very well written.
data science course in kochi

360digitmgas said...

You might comment on the order system of the blog. You should chat it's splendid. Your blog audit would swell up your visitors. I was very pleased to find this site.I wanted to thank you for this great read!!
arttificial intelligence course in faridabad

sankeerthan said...

I like viewing web sites which comprehend the price of delivering the excellent useful resource free of charge. I truly adored reading your posting. Thank you!
arttificial intelligence course in aurangabad

sankeerthan said...
This comment has been removed by the author.
sankeerthan said...

it was a wonderful chance to visit this kind of site and I am happy to know. thank you so much for giving us a chance to have this opportunity.. data science training in pune

Data Science Course said...

You might comment on the order system of the blog. You should chat it's splendid. Your blog audit would swell up your visitors. I was very pleased to find this site.I wanted to thank you for this great read!!

Data Science Course

Data Science Training said...

Just saying thanks will not just be sufficient, for the fantasti c lucidity in your writing. I will instantly grab your rss feed to stay informed of any updates.

Data Science Training

Data Science Training said...
This comment has been removed by the author.
DataScience Specialist said...

I feel a lot more people need to read this, very good info! .
Data Science Course in Bangalore

DataScience Specialist said...

Much thanks for composing such an intriguing article on this point. This has truly made me think and I plan to peruse more
Data Science Training in Bangalore

JAINAND DIGITAL POINT said...

Led bullb repairing fast and lowest prices visit us : JAINAND DIGITAL POINT

360digitmgdelhi said...

Through this post, I realize that your great information in playing with all the pieces was exceptionally useful. I advise this is the primary spot where I discover issues I've been scanning for. You have a smart yet alluring method of composing.
data science courses in delhi

jenani said...

Really it is very useful for us..... the information that you have shared is really useful for everyone.Nice article i have ever read information's like this.it's really awesome the way you have delivered your ideas.i hope you will add more content in your blog
Java Training in Chennai

Java Training in Velachery

Java Training in Tambaram

Java Training in Porur

Java Training in Omr

Java Training in Annanagar

Digital Marketing Course - 360DigiTMG said...

Truly incredible blog found to be very impressive due to which the learners who ever go through it will try to explore themselves with the content to develop the skills to an extreme level. Eventually, thanking the blogger to come up with such an phenomenal content. Hope you arrive with the similar content in future as well.

Digital Marketing training in Bhilai

subathara said...
This comment has been removed by the author.
vanathi said...

Interesting blog, it gives lots of information to me. Thanks for sharing such a nice blog.
Software Testing Training in Chennai

Software Testing Training in Velachery

Software Testing Training in Tambaram

Software Testing Training in Porur

Software Testing Training in Omr

Software Testing Training in Annanagar

Digital Marketing training - 360DigiTMG said...

Honestly speaking this blog is absolutely amazing in learning the subject that is building up the knowledge of every individual and enlarging to develop the skills which can be applied in to practical one. Finally, thanking the blogger to launch more further too.

Digital Marketing Course

subathara said...

Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
Digital Marketing Training in Chennai

Digital Marketing Training in Velachery

Digital Marketing Training in Tambaram

Digital Marketing Training in Porur

Digital Marketing Training in Omr

Digital Marketing Training in Annanagar

Data Science training - 360DigiTMG said...

Wonderful blog found to be very impressive to come across such an awesome blog. I should really appreciate the blogger for the efforts they have put in to develop such an amazing content for all the curious readers who are very keen of being updated across every corner. Ultimately, this is an awesome experience for the readers. Anyways, thanks a lot and keep sharing the content in future too.

Data Science training

Digital Marketing Course - 360DigiTMG said...

Truly incredible blog found to be very impressive due to which the learners who ever go through it will try to explore themselves with the content to develop the skills to an extreme level. Eventually, thanking the blogger to come up with such an phenomenal content. Hope you arrive with the similar content in future as well.

Digital Marketing training

saketh321 said...


A debt of gratitude is in order for ExcelR Data Analytics Course the blog entry amigo! Keep them coming...

Oxygen Academy said...

Such a very useful article. Very interesting to read this article. I would like to thank you for the efforts you had made for writing this awesome article.Cyber Security in Visakhapatnam. Cyber Security near me

Oxygen Academy said...

I need to thank you for this particularly fantastic article. I definitely really liked every part of it.Educational Institute in Visakhapatnam.

Clubf7 said...

Wow! Such an amazing and helpful post this is. I really really love it. It's so good and so awesome. I am just amazed. I hope that you continue to do your work like this in the future also.
Best Gym in Visakhapatnam

Unknown said...

“Great share!”
Best Gym in Visakhapatnam

360DigiTMG said...

Wonderful blog post. This is absolute magic from you! I have never seen a more wonderful post than this one. You've really made my day today with this. I hope you keep this up!
data analytics course in hyderabad

Pema wangmo said...

Thank you for the great post., Appreciated it! Digital Marketing Institute in Gurgaon where you can learn everything from here.

iteducationcentre said...

Superb post.
Java training in Pune