2012-08-04 29 views
0

問題是:我以客戶爲回報。根據我的viewresolver是應該映射到WEB-INF/pages/customer.html。相反,它是通過調度程序servlet進行的,無法找到客戶的html。它給出的錯誤是:「警告:沒有映射找到具有URI [/SpringMVC/WEB-INF/pages/customer.html]中的名稱爲'mvc-dispatcher'的HTTP請求的映射'」Spring MVC:調度員映射到錯誤視圖

這是我的控制器

@Controller 
public class CustomerController implements BeanFactoryAware { 

    private Customers customers; 


    /*public String getCustomer(@RequestParam String name) { 

     //ApplicationContext context = new FileSystemXmlApplicationContext("/WEB-INF/springapp-servlet.xml"); 
     //Customers customers = get 
     System.out.println("In Controller"); 
     return "customer"; 
    }*/ 

    @RequestMapping(value="/form") 
    public String getCustomer(HttpServletRequest request, HttpServletResponse response) 
    throws ServletException, IOException { 


      System.out.println("In Customer Controller"); 
      return "customer"; 
    } 

    @Override 
    public void setBeanFactory(BeanFactory context) throws BeansException { 
     // TODO Auto-generated method stub 
     customers = (Customers)context.getBean("customers"); 

     //System.out.println(customers); 

    } 

} 

這是我的web.xml

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> 

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

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

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

這是我的dispatcher.xml


<bean 
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="prefix"> 
     <value>/WEB-INF/pages/</value> 
    </property> 
    <property name="suffix"> 
     <value>.html</value> 
    </property> 
</bean> 

回答

0

嘗試從改變你的servlet映射:

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

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

我試過改變/ *到/也還是同樣的問題。我有奇怪的行爲。每當我將所有htmls重命名爲jsps時,它工作正常,但將擴展名更改爲htmls會給我帶來問題。在轉換爲jsp時,我只是在internalviewresolver中將後綴屬性更改爲jsp。感謝Biju – Gaurav 2012-08-06 05:44:22

0

你的servlet將攔截的URL模式匹配所有的呼叫 '/ *',但它無法在任何彈簧控制器中找到指定的映射。

我曾經遇到類似的麻煩,以及我如何克服這一點是通過遵循所有春季呼叫的特定模式。例如,

<url-pattern>*.htm</url-pattern> 

希望這可以幫助你。

0

這是行不通的,因爲最終的通話將進入RequestDispatcher的是這樣的:

RequestDispatcher dispatcher = httpRequest.getRequestDispatcher("/WEB-INF/pages/test.html"); 
dispatcher.forward(httpRequest, httpRequest); 

此時容器將期望的DispatcherServlet再次處理請求(因爲/*路徑)。如果它是一個jsp頁面,容器有一個*.jsp的映射並知道如何處理它。

你就會把你的資源,對於你的web資源的地方(如果maven的結構,那麼src/main/webapp/resources/下,這個內容配置處理器的修復:

<mvc:resources location="/resources/" mapping="/resources/**" /> 

,現在從你的控制器,你可以返回:

return "forward:/resources/mypage.html"; 

另外,我看到你的「客戶」豆做的查找,你不必這樣做,而是希望春天在注入它:

@Controller 
public class CustomerController{ 

    @Autowired private Customers customers; 
+0

,它解決了我的問題! – Gaurav 2012-08-06 09:15:06