简体   繁体   中英

Issues with mapping multiple controllers

I am new to spring.My mappings not working correctly though my Tomcat logs show that my URL is mapped.

INFO: Mapped URL path [/movie/{name}] onto handler 'movieController'
Jun 21, 2015 9:03:24 PM org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
INFO: Mapped URL path [/movie/{name}.*] onto handler 'movieController'
Jun 21, 2015 9:03:24 PM org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
INFO: Mapped URL path [/movie/{name}/] onto handler 'movieController'
Jun 21, 2015 9:03:24 PM org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
INFO: Mapped URL path [/movies] onto handler 'movieController'
Jun 21, 2015 9:03:24 PM org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
INFO: Mapped URL path [/movies.*] onto handler 'movieController'
Jun 21, 2015 9:03:24 PM org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
INFO: Mapped URL path [/movies/] onto handler 'movieController'
Jun 21, 2015 9:03:24 PM **org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
INFO: Mapped URL path [/employeelist] onto handler 'userList'
Jun 21, 2015 9:03:24 PM org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
INFO: Mapped URL path [/employeelist.*] onto handler 'userList'
Jun 21, 2015 9:03:24 PM org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
INFO: Mapped URL path [/employeelist/] onto handler 'userList'

I have two controllers defined in web.xml.

<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Spring Web MVC Application</display-name>


    <servlet>       <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>    </servlet>

    <!-- for ListDispatcher -->

    <servlet>       <servlet-name>list-dispatcher</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>1</load-on-startup>    </servlet>


    <servlet-mapping>       <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/test/*</url-pattern>  </servlet-mapping>


    <servlet-mapping>       <servlet-name>list-dispatcher</servlet-name>
        <url-pattern>/list/*</url-pattern>  </servlet-mapping>


<context-param>   <param-name>contextConfigLocation</param-name>  
<param-value>
    /WEB-INF/list-dispatcher-servlet.xml
    /WEB-INF/mvc-dispatcher-servlet.xml   </param-value> </context-param>

    <!-- <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener> --> 
</web-app>

I am getting output for the ones defined in MovieController. http://localhost:8080/SpringMVC/test/movies

But when i giving the URL to fetch the employeelist(another controller).its not working.Below is the controller class.:-

@Controller

public class UserList {


    public ModelAndView getdata() {
        System.out.println("Data");

        ArrayList<Employee> list = getEmpList();

        //return back to index.jsp
        ModelAndView model = new ModelAndView("index");
        model.addObject("lists", list);

        return model;

    }

    @RequestMapping(value="/employeelist", method = RequestMethod.GET) 
    public @ResponseBody  ArrayList<Employee> getEmpList(){
        System.out.println("inside The ArrayList");
        ArrayList<Employee> emp=new ArrayList<Employee>();
        emp.add(new Employee("sougata",25));
        emp.add(new Employee("sahil",30));
        return emp;

    }
}

Can someone pleas help me out to get the response from the 2nd controller. URL i am using is:- http://localhost:8080/SpringMVC/list/employeelist

Change the piece of snippet code in your application.

In your case, You have separated two controller's for multiple action. So according to your case scenario, replace this snippet code in web.xml, like this

<servlet-mapping>
     <servlet-name>mvc-dispatcher</servlet-name>
     <url-pattern>/</url-pattern>
</servlet-mapping>

And change the controller request mapping in the controller class, for movie Controller add like this,

@Controller
@RequestMapping("/movieController")
public class MovieController {

for userList controller add like this,

@Controller
@RequestMapping("/userListController")
public class UserList {

Check your component-base package property correctly mentioned in dispatcher-servlet.xml.

Try adding produces="application/json" to your @RequestMapping as shown below.

@RequestMapping(value="/employeelist", method = RequestMethod.GET, produces="application/json")

Are you trying to integrate spring MVC and CXF service? the servlet-class for list-dispatcher is CXFServlet in your web.xml. org.apache.cxf.transport.servlet.CXFServlet If not-You do not actually need two different servlet configurations. simply have the below

<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">
    <servlet><servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM