我是spring的新手。虽然我的Tomcat日志显示我的URL已映射,但我的映射无法正常工作。
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'
我在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>
我正在获取MovieController中定义的输出。 http://localhost:8080/SpringMVC/test/movies
但是当我提供URL来获取employeeelist(另一个控制器)时。不工作.Below是控制器类。: -
@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;
}
}
有人请求帮助我从第二个控制器获得响应。 我使用的网址是: - http://localhost:8080/SpringMVC/list/employeelist
答案 0 :(得分:3)
更改应用程序中的片段代码。
在您的情况下,您已将两个控制器分开以进行多项操作。因此,根据您的案例场景,在web.xml中替换此代码段,如此
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
并更改控制器类中的控制器请求映射, 对于电影控制器添加像这样,
@Controller
@RequestMapping("/movieController")
public class MovieController {
用于userList控制器添加如下,
@Controller
@RequestMapping("/userListController")
public class UserList {
检查dispatcher-servlet.xml中正确提到的基于组件的包属性。
答案 1 :(得分:0)
尝试将produce =“application / json”添加到您的@RequestMapping中,如下所示。
@RequestMapping(value =“/ employeelist”,method = RequestMethod.GET,produce =“application / json”)
答案 2 :(得分:0)
您是否尝试集成spring MVC和CXF服务? list-dispatcher的servlet类是web.xml中的CXFServlet。 org.apache.cxf.transport.servlet.CXFServlet 如果不是 - 您实际上不需要两个不同的servlet配置。 只需要下面的
<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>