• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

Confusion over beanNameUrlMpping

 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following is an except from Mannings "Spring in action"
..........
BeanNameUrlHandlerMapping�Maps controllers to URLs that are based on
the controllers� bean name

For example, suppose that you want the ListCoursesController bean to handle
requests to URLs of the form �http://server-name/training/listCourses.htm�. To
set up bean name mapping, you must declare a BeanNameUrlHandlerMapping bean
in your context configuration file like this:
<bean id="beanNameUrlMapping" class="org.springframework.web.
servlet.handler.BeanNameUrlHandlerMapping"/>
Then you�ll need to name your controller beans with the URL pattern that they are
to handle. The URL pattern for the ListCoursesController is �listCourses.htm�,
so you�ll need to declare the controller in the context configuration file as follows:
<bean name="/listCourses.htm"
class="com.springinaction.training.mvc.ListCoursesController">
<property name="courseService">
<ref bean="courseService"/>
</property>
</bean>
Whenever BeanNameUrlHandlerMapping is asked to resolve a mapping to �/list-
CoursesController.htm�, it will scour the application context for a bean whose
name matches the URL pattern, finding ListCoursesController.

.........
My config file contains follwoing entry:

<bean name="/priceincrease.htm" class="springapp.web.PriceIncreasController">

I changed the class name to springapp.web.xyzContoller, but still my code is working!!!
Why and how??? Am i getting the concepts incorrectly? Pls help.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I changed the class name to springapp.web.xyzContoller, but still my code is working!!!


What did you change exactly ? Both the class name (PriceIncreasController.class into xyzContoller.class ) and the bean definition ? If so, it will work fine. The name of the class does not need to be the same as the url you're mapping it to. In Manning's example, if ListCoursesController was actually called MyController, the following would still work :
 
Shyam kumar
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply Christophre,
Yes, I changed both the class name and bean defination. and it is working.

Now I have 2 questions:
1. Do you mean to say that it is incorrectly mentioned in Manning.( Hope you have already read the excerpt)
2. I changed the bean name from "/priceincrease.htm" to "/price.htm"

<bean name="/price.htm" class="springapp.web.PriceIncreasController">
<property name="sessionForm" value="true"/>
<property name="commandName" value="priceIncrease"/>
<property name="commandClass" value="springapp.domain.PriceIncrease"/>
<property name="validator">
<bean class="springapp.domain.PriceIncreaseValidator"/>
</property>
<property name="successView" value="index.htm"/>
<property name="productManager" ref="SimpleProductManager"/>

</bean>

Now when i try to run the application i get the following error at the server side
that resource /WEB-INF/jsp/price.jsp is not found.

Does that mean that url name is coupled with the jsp name???
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

1. Do you mean to say that it is incorrectly mentioned in Manning


/listCoursesController.htm should be replaced by /listCourses.htm

resource /WEB-INF/jsp/price.jsp is not found


? What URL did you you use ? A bean named "/price.htm" will map to an URL ending in /price.htm, not anything else.
 
Shyam kumar
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

/listCoursesController.htm should be replaced by /listCourses.htm



I think I'm not able to make myself clear to you here. Actually what i wanted to know
that from the excerpt of manning book i have undustood that:
if url pattern is /abc.htm then the controller class should be AbcController.
Is this what the book saying? Please correct me


? What URL did you you use ? A bean named "/price.htm" will map to an URL ending in /price.htm, not anything else.


Initially the spring config had the following entry, when it was working fine:
<bean name="/priceincrease.htm" class="springapp.web.PriceIncreasController">
<property name="sessionForm" value="true"/>
<property name="commandName" value="priceIncrease"/>
<property name="commandClass" value="springapp.domain.PriceIncrease"/>
<property name="validator">
<bean class="springapp.domain.PriceIncreaseValidator"/>
</property>
<property name="successView" value="index.htm"/>
<property name="productManager" ref="SimpleProductManager"/>

</bean>

I changed the spring cofig to the following entry, then I'm getting the exception
<bean name="/price.htm" class="springapp.web.PriceIncreasController">
<property name="sessionForm" value="true"/>
<property name="commandName" value="priceIncrease"/>
<property name="commandClass" value="springapp.domain.PriceIncrease"/>
<property name="validator">
<bean class="springapp.domain.PriceIncreaseValidator"/>
</property>
<property name="successView" value="index.htm"/>
<property name="productManager" ref="SimpleProductManager"/>

</bean>

I want to know why I'm getting the exception resource /WEB-INF/jsp/price.jsp is not found.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

if url pattern is /abc.htm then the controller class should be AbcController. Is this what the book saying? Please correct me


No, that's not what it means. It means that the url will be matched to the name of the bean ("/abc.htm"). The class name can be anything you want. For example :

http://localhost:8080/myapp/abc.htm will redirect the request to MyController.
 
Shyam kumar
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Christopher, I misread the statement completely...
 
I love a good mentalist. And so does this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!