2012-06-28 116 views
0

我已經在採用以下方式配置尋址使用Spring MVC的應用春3.1.1.RELEASE請求不被彈簧控制器

的web.xml

<servlet> 
    <servlet-name>springmvc</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/context/*-context.xml</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
    <servlet-name>springmvc</servlet-name> 
    <url-pattern>/cgi/*</url-pattern> 
    </servlet-mapping> 

用SpringMVC-servlet.xml中

<context:component-scan base-package="com.pokuri.mvc.controllers"/> 

UserController.java

package com.pokuri.mvc.controllers; 

@Controller 
@RequestMapping("/user") 
public class UserController { 

    @RequestMapping(method=RequestMethod.GET) 
    @ResponseBody 
    public String defaultRequest(){ 
     return "It's a default handler method"; 
    } 

} 

的index.jsp

<a class="action" href="cgi/user">Default Action</a> 

Sever的日誌上用SpringMVC初始化應用:

23:53:04,406 INFO [DispatcherServlet] FrameworkServlet 'springmvc': initialization started 
23:53:04,468 INFO [XmlWebApplicationContext] Refreshing WebApplicationContext for namespace 'springmvc-servlet': startup date [Thu Jun 28 23:53:04 IST 2012]; root of context hierarchy 
23:53:04,609 INFO [XmlBeanDefinitionReader] Loading XML bean definitions from ServletContext resource [/WEB-INF/context/services-context.xml] 
23:53:04,890 INFO [DefaultListableBeanFactory] Pre-instantiating singletons in org.s[email protected]6c4fe: defining beans [dummyService]; root of factory hierarchy 
23:53:05,156 INFO [DispatcherServlet] FrameworkServlet 'springmvc': initialization completed in 750 ms 
..... 
..... 
23:56:17,875 WARN [PageNotFound] No mapping found for HTTP request with URI [/springmvc/cgi/user] in DispatcherServlet with name 'springmvc' 

當我是點擊鏈接在index.jsp中,我得到404錯誤。我可能犯了一個愚蠢的錯誤。但是,無法跟蹤它。有人可以幫助我嗎?

+0

嘗試將'@RequestMapping(value =「/」,method = RequestMethod.GET)'on defaultRequest()' –

回答

1

看起來您的DispatcherServlet正在尋找以-context.xml結尾的配置文件。但是你的Spring配置文件叫做springmvc-servlet.xml

+0

DispacherServlet根據web.xml中給出的名稱構建它的WebApplicationContext(即springmvc-servlet.xml )。然後我使用contextConfigLocation爲服務,dao和其他bean配置應用程序上下文。這是我錯誤的看法? – Pokuri

+1

您是否嘗試將web.xml中的該行更改爲'/ WEB-INF/context/* - servlet.xml'(假設springmvc-servlet.xml位於上下文目錄中)? – nickdos

+0

是啊!通過消除它的工作正常 – Pokuri