MyArchiBook

Simple Form Handling and Performing Dependency Injection in Spring MVC Framework using NetBeans

with 6 comments

  I thought  working on Spring MVC Framework– to handle xml, configurations etc.,was a geeky kindaa job.  But I also recently learnt that , working on it using an IDE like NetBeans which provides the Spring MVC Template(pom.xml, MVC Configurations etc.,) and also the Maven build  has made me focus mainly on the design and coding part of my project , thus saving my time and leaving me unstrained .

Below I have created Simple Form Handling Application using Spring MVC Framwork(3.X).In this sample,I have also implemented Dependency Injection – annotation based .

IDE used:NetBeans 7.3

Build used:Maven 2.2

JspConsider 2 Jsp’s

Jsp 1  : MusicForm.jsp contains the text boxes to be entered.

Jsp 2 : Output.jsp conatins the TEXTS of,

  •          Object transferred from Jsp 1
  •          Object got through DEPENDENCY INJECTION

FLOW:

1.Bean : SongDetails Bean with setters and getters is created

2.Interface : IMusicService Interface is created. It has three unimplemented methods play(), pause(), stop();

3.Service Implementation class :MusicServiceImpl class that implements the above interface and its unimplemented methods is created.

@Service
public class MusicServiceImpl implements IMusicService{
@Override
 public String play() {
 return "Music is played on!!!";
 }

@Override
 public String pause() {
 return "Music has been PAUSED !";
 }

@Override
 public String stop() {
 return "Music Stopped";
 }
}

Note  that there is @Service annotation before the class name… Please have it in mind.Will tell you why, when it’s time of usage has come.

4. Jsp and Controller:

4.a) HomePage(MusicForm) Jsp and Controller :

MusicForm

Here in MusicForm.jsp, the details to be transferred is enclosed in a form.Form has the following attributes,

  • Method type – GET/POST is specified here.
  • commandName -Here we can specify name of the Object , that carries the SongDetails (above commandName is songDetForm )
  • action- Here OutCome jsp name, where the details are to be transferred is specified.

For the above Jsp , controller method has to be written.The flow goes like this,

  • ModelAndView object is created
  • ViewName is set
  • SongDetails Object is initialised and set as songDetForm (must be same as commandName in jsp).

4.b)Output jsp and Controller:

Output

Here in this Controller,

  • Output jsp name is given as the Request mapping value
  • Request method is POST as specified in First Jsp
  • View Name is set.
  • Then Two Objects are set—->1.songDetForm, 2.object got through dependency injection
  • To set songDetForm obj,
  1. Object is got from HomePageController method via @ModelAttribute(“songDetForm”)SongDetails songDetails. This means , object or attribute in the name “songDetForm” is assigned to songDetails object.
  2. This object is then added in a HashMap
  • To set Dependency injection object,
  1. IMusicService private instance is created .It is annotated with @Autowired.
  2. Apart from this, @Service annotation has to be specified on the ServiceImplementation class . This means, MusicServiceImpl class is at Service which can be injected ,when @Autowired annotation is used over its Interface instance.
  3.  In MVC Configuration, the package be scanned is specified and Component Scan is done.
  4. then dependencyObject is  added in the Controller method map.
  • Both these objects along with viewName are set in mv and mv is returned.

The controller sourcecode will look like,

@Controller
public class HomeController {

 @Autowired
 private IMusicService iMusicService;

 @RequestMapping(value="/")
 public ModelAndView homePage(){
 //Method 1:
 ModelAndView mv=new ModelAndView();
 mv.setViewName("MusicForm");
 mv.addObject("songDetForm",new SongDetails());
 return mv;

 //Method 2:                  viewName      Obj Key        Obj Value
 // return new ModelAndView("MusicForm","songDetForm",new SongDetails());
 }

 @RequestMapping(value ="/Output", method = RequestMethod.POST)
 public ModelAndView processSongDetails (@ModelAttribute("songDetForm") SongDetails songDetails){
 Map<String,Object> obj=new HashMap<String,Object>();
 obj.put("songDetails",songDetails);
 obj.put("iMusicService", iMusicService);
//                                            viewName  Obj
 ModelAndView modelAndView = new ModelAndView("Output", obj);
 return modelAndView;
 }
}
  • In the respective Output.jsp , the respective objects are got.

5. Clean the project.

6.Build it using Maven… The most advantageous part about Maven is , I needn’t have to strain myself to provide any dependency jars, specify its classpath etc.,  everytime. Maven took care of that part by itself, thus making my code-writing easy and simple.

7.Run it.

The output will be as follows…

WebPageResult

That’s it about Form Handling and Dependecy Injection in Spring MVC Framework.

You can find and download the above source code from this link.  https://github.com/AishwaryaThangavelu/Spring-Repository

Advertisements

Written by thangaveluaishwarya

June 22, 2013 at 3:49 AM

6 Responses

Subscribe to comments with RSS.

  1. If you are talking about using spring only for injection then I suggest you to have a look at java enterprise edition 5 and then 6.
    Edition 5 introduced resource injection using annotation. You should be also knowing about ejb but in 6 cdi is there. A lot of things have been made simpler. Also your application would be lighter. Right now we have tomcat and glass fish that supports cdi.
    nice blog and the other series too.

    Isotope

    June 22, 2013 at 7:03 AM

  2. Spring is good no doubt but try having a read through below

    http://docs.oracle.com/javaee/6/tutorial/doc/

    http://www.jboss.org/jdf/migrations/get-started/

    Suggest above

    Santhosh

    June 22, 2013 at 7:25 AM

    • @Santhosh: Thanks for adding on. Will have a look at it

      thangaveluaishwarya

      June 22, 2013 at 10:46 AM

    • Try spring 4 beta. Had introduced lot of features. We must accept that spring addressed the strutting comings of j2ee 1.4

      java beats

      June 22, 2013 at 1:19 PM

  3. very nice tat this time u put also the source also gave me a chance to try git

    newbee

    June 28, 2013 at 10:37 PM


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: