我正在尝试spring-mvc和json。
我的项目拱门是
src
--main
----java
----resource
----webapp
------WEB-INF
--------web.xml
--------jodoCmsDispatcher-servlet.xml
我有一个普通的控制器
public class CategoryController {
@RequestMapping(value = "/kfc", method = RequestMethod.GET)
public @ResponseBody Shop addCategory(HttpServletResponse response, @PathVariable("parentid") String parentCategoryId, @PathVariable("newcategorytitle") String newcategorytitle )
{
Shop shop = new Shop();
shop.setName("Testing");
shop.setStaffName(new String[]{"11", "22"});
return shop;
}
}
的web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB_INF/jodoCmsDispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
</listener>
<servlet>
<servlet-name>jodoCmsDispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jodoCmsDispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
jodoCmsDispatcher-servlet.xml中
.
.
<context:component-scan base-package="com.jodo.cms.controllers" />
<mvc:annotation-driven />
我尝试过以下网址(每次都有404)
localhost - - [05/Apr/2014:11:43:41 +0530] "GET /jodocms/kfc/ HTTP/1.1" 404 949
localhost - - [05/Apr/2014:11:44:20 +0530] "GET /jodocms/kfc HTTP/1.1" 404 949
localhost - - [05/Apr/2014:11:44:24 +0530] "GET /jodocms/ HTTP/1.1" 404 949
Project已在tomcat中成功部署,如localhost:8080 / manager所示
我正在尝试发送json数据并关注&#34; http://www.mkyong.com/spring-mvc/spring-3-mvc-and-json-example/&#34;
我错过了什么?
感谢
答案 0 :(得分:1)
这不是真的需要
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB_INF/jodoCmsDispatcher-servlet.xml</param-value>
</context-param>
AspatpatcherServlet默认加载jodoCmsDispatcher-servlet.xml,所以删除那些行,该配置仅用于添加另一个applicationContext文件
并且必须使用@Controller
标记CategoryController进行扫描
还要考虑改变
<context:component-scan base-package="com.jodo.cms.controllers" />
到
<context:component-scan base-package="com.jodo.cms" />
尝试删除..
@PathVariable("parentid") String parentCategoryId,
@PathVariable("newcategorytitle") String newcategorytitle
由于您的网址不正确,导致冲突,因此请保留此方法。
@RequestMapping(value = "/kfc", method = RequestMethod.GET)
public @ResponseBody Shop addCategory(HttpServletResponse response)
{
Shop shop = new Shop();
shop.setName("Testing");
shop.setStaffName(new String[]{"11", "22"});
return shop;
}