Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Some beginner question about annotation configuration in Spring 3.1

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello to everybody,

I am quite new in Spring world.

I am now following the Spring MVC showcase example that you can download directly from the dashboard of your STS, but this is not important...it is a very simple example but I have some doubt.

My doubts concern the annotation configuration in Spring 3.1

In my example I have 2 configuration file:

The first one is the web.xml configuration file:


In this configuration there is the definition of my servlet named appServlet that have to manage all the http request to "/" pattern.

This servlet is configured in the second xml configuration file that is my servlet-context.xml, this is its code:



In this file I have configured the component-scan that allows Spring to call all components from the package com.springsourceMVC.myapp, and in particular the only class that I have at the moment in this package that is HomeController class that implement the controller that generates the data to be included in the view



Ok...but in my code I have also the following line of code:


The comments (written by the developer of the example) say that this line of code enable Spring MVC @Controller programming model, what means? This feature should not be enabled from the previous line of code?

I also try to delete the <annotation-driven /> tag from my servlet-context.xml file and my project still work well...I do not understand what it is used for...can you help me?

yhank you very much
Andrea
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both tags provide different classes to work on different annotations and also give you different set of bean classes that is needed.

So for instance component-scan provides things like scanning for @Component, @Controller, @Service, @Repository, @Autowired, @Inject, @PostConstruct etc, it adds BeanPostProcessors and such that scans for those and some more annotations, it does not for example, act on/search for @RequestMapping, @SessionAttribute, @RequestBody, @ResponseBody, not provide an HttpMessageConverter class. No, because those classes and act/on are provided with the mvc:annotation-driven tag. Now it also doesn't mean that xml configuration can overwrite some of that stuff too.

Hope that helps. Basically they provide difference stuff, those two configurations.

Mark
 
Andrea Nobili
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mark Spritzler wrote:Both tags provide different classes to work on different annotations and also give you different set of bean classes that is needed.

So for instance component-scan provides things like scanning for @Component, @Controller, @Service, @Repository, @Autowired, @Inject, @PostConstruct etc, it adds BeanPostProcessors and such that scans for those and some more annotations, it does not for example, act on/search for @RequestMapping, @SessionAttribute, @RequestBody, @ResponseBody, not provide an

class. No, because those classes and act/on are provided with the mvc:annotation-driven tag. Now it also doesn't mean that xml configuration can overwrite some of that stuff too.

Hope that helps. Basically they provide difference stuff, those two configurations.

Mark



Hello Mark,

thank you very much for your clear explanation...I have now understand that component-scan and mvc:annotation driven tag provide difference stuff but my doubt is the following one:

Why in the Spring showcase MVC base example use both?

If I delete the <annotation-driven /> tag from my servlet-context.xml configuration file...the projext still work well...so I'm thinking that, in this example, this annotation-driven tag is useless...but then why was it inserted?

I have also another question about the previus base Spring MVC example. Inside my spring-servlet.xml file I have also the following line of code:



the developer comment say that this tag is used to handling the HTTP GET request for the static resurces inside all the folder inside my /resources/ folder (I think something like properties file, image, css, etcetc), is it right?


My doubt is generated by the fact that I have nothing in the /resources folder of my project and if I delete this tag from my servlet-context.xml file, the project still work well...

So I am thinking that both the tag (<annotation-driven /> and <resources mapping="/resources/**" location="/resources/" />) are useless in this project and the developer of this example has placed them just because I could use them if I can further develop the project by adding new features...

but I'm not sure that this reasoning makes sense, wwhat do you think about?

I am searching to understand very well the base od Spring MVC framework to avoid problems in the future

Thank you very much
Andrea
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Why in the Spring showcase MVC base example use both? "

Because they do different things and the app needs both of those things. It is possible to write code that doesn't use the @RequestMapping and such.

Mark
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes the resources mapping makes putting links to css and javascript, well imports, so much easier to type and not have to do things like {context.getPath} or the similar in your html, it can now just be "resources/js/myjs.js"

But it doesn't mean you have to use "resources/js/myjs.js" in your html, you can still do {context.getPath} in your html.

In this showcase you'll see the following in action:

The simplest possible @Controller
Mapping Requests
Obtaining Request Data
Generating Responses
Message Converters
Rendering Views
Type Conversion
Validation
Forms
File Upload
Exception Handling



So things like MessageConverters, TypeConversion, Validation those all require the mvn:annotation-driven to automatically get all of those. You can also get all those by defining each and every one as its own bean in the config file, but that would probably be a couple hundred lines of xml that you get with just one line mvc:annotation-driven.

I would say the application doesn't completely work without mvc:annotation-driven.

But you probably can run it and just get to

<mvc:view-controller path="/" view-name="home"/>

the root path because of this that is in the config file
spring-mvc-showcase / src / main / webapp / WEB-INF / spring / appServlet / controllers.xml


Hmm, I also saw in the servlet-context.xml



Which doesn't look what you posted. I am looking at the current up to date version of the app on github at https://github.com/SpringSource/spring-mvc-showcase

Mark
 
Andrea Nobili
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mark Spritzler wrote:Yes the resources mapping makes putting links to css and javascript, well imports, so much easier to type and not have to do things like {context.getPath} or the similar in your html, it can now just be "resources/js/myjs.js"

But it doesn't mean you have to use "resources/js/myjs.js" in your html, you can still do {context.getPath} in your html.

In this showcase you'll see the following in action:

The simplest possible @Controller
Mapping Requests
Obtaining Request Data
Generating Responses
Message Converters
Rendering Views
Type Conversion
Validation
Forms
File Upload
Exception Handling



So things like MessageConverters, TypeConversion, Validation those all require the mvn:annotation-driven to automatically get all of those. You can also get all those by defining each and every one as its own bean in the config file, but that would probably be a couple hundred lines of xml that you get with just one line mvc:annotation-driven.

I would say the application doesn't completely work without mvc:annotation-driven.

But you probably can run it and just get to

<mvc:view-controller path="/" view-name="home"/>

the root path because of this that is in the config file
spring-mvc-showcase / src / main / webapp / WEB-INF / spring / appServlet / controllers.xml


Hmm, I also saw in the servlet-context.xml



Which doesn't look what you posted. I am looking at the current up to date version of the app on github at https://github.com/SpringSource/spring-mvc-showcase

Mark



mmm...OMG...but at what example you're referring to? In my MVC example there are not all these stuff !!! Maybe is it a different example?

I dowloaded my example directly from STS dashboard doing the following opertation: click on example project tab in the dashboard ---> Spring Template Project

I have only a homepage that show an "Hello World" message and the current date and time...

And in this example I can delete these 2 lice from my servlet-context.xml configuration file and the project work completly well...

I have not all the stuff of which you speak...like form, type conversion, validation, etcetc...
 
Andrea Nobili
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok,
I think that now I have understand what is the problem...the project that I have created is not the showcase MVC example...now I have dowload the right showcase example and yes...there are all the stuff you taliking about...

Tnx...now I will begin to study on this...
Andrea
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, you just built an app based on a template that doesn't have anything written in it yet that takes advantage or uses mvc:annotation-driven because the only "mapping is directly in the xml with mvc:controller pointing to a simple html page with just Hello World

Glad we got it all straightened out.

Good Luck and post away here for any questions you have, we are glad to be of assistance.

Mark
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic