views:

917

answers:

5

I am trying to split the ApplicationContext file in Spring.

For ex. the file is testproject-servlet.xml having all the entries. Now I want to split this single file into multiple files according to logical groups like : group1-services.xml, group2-services.xml

I have created following entries in web.xml :

<servlet>
    <servlet-name>testproject</servlet-name>
    <servlet-class>
     org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <init-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>
      /WEB-INF/group1-services.xml, /WEB-INF/group2-services.xml
     </param-value>
    </init-param>  
         <load-on-startup>1</load-on-startup>
</servlet>

I am using SimpleUrlHandlerMapping as:

RegisterController PayrollServicesController

I also have the controller defined as :

.. ..

The problem is that I have splitted the ApplicationContext file "testproject-servlet.xml" into two different files and I have kept the above entries in "group1-services.xml". Is it fine? I want to group things logically based on their use in seperate .xml files.

But I am getting the following error when I try to access a page inside the application :

org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping for [/TestProject/payroll_services.htm] in DispatcherServlet with name 'testproject'

Please tell me how to resolve it.

Thanks in Advance !

A: 

I don't think its a problem with your contextConfigLocation as such

I think its more that the dispatcher needs to know where to send payroll_servives.htm to, and can't find an appropriate handler knowing what to do with this pattern.

See reference documentation

Did you really want *.htm files to be matched to the dispatcher servlet?

If you are using annotation-based controllers (@Controller), then you need to have a line similar to:

<context:component-scan base-package="org.springframework.samples.petclinic.web"/>

This installs an appropriate annotation-based handler, that searchs for classes/methods annotated like:

@Controller
public class PayrollController {
     @RequestMapping("payroll_services.htm")
     public ModelAndView payrollServices() {
         ....
     }
}

Otherwise, if you are using more traditional handlers/controllers, you need to define this using XML. The reference documentation link earlier in this posting mentions two such handlers: SimpleUrlHandlerMapping or BeanNameUrlHandlerMapping

Perhaps if you updated your question with snippets of handler/controller XML, this may help?

toolkit
Thanks for your helpI have updated my question with more details.. please help on it.
A: 

Did you add the ContextLoaderListener to your web.xml? I don't see it:

   <listener>
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
   </listener>
duffymo
Yes I have this entry in web.xml. Still I am getting the error.I am not able to figure it out why this happens?
A: 

You need to include something like this in each of your files:

<context:component-scan base-package="com.example.dao" />

If it is missing from a file, it seems the annotated classes are not known in the context of that file.

You may want to revisit the documentation for details.

Tore A.
A: 

By the way, there's no need to list different Spring context configuration files inside of your <init-param> element. Nowadays, if you want to split your Spring context configuration into different files based on logical grouping, you simply have one main Spring configuration file and then import your other configuration files from inside of it like this:

<beans>
...
  <import resource="spring-security.xml" />
  <import resource="spring-webmvc.xml" />
...
</beans>

... and so on.

I think it's been possible to do this starting with Spring Framework 2.5 onward.

import has been available long before Spring 2.5
iwein
A: 

Either the handler mapping of your DispatcherServlet is incorrect or you're using the wrong url. If you fix the layout around your SimpleUrlHandlerMapping configuration I can tell you what to change.

iwein