2011-09-02 56 views
4

當林在SpringSource工具創建的Spring MVC模板項目,並嘗試在Tomcat服務器上運行我有這樣的錯誤:爲什麼SpringSource Tool Suite中的「Spring MVC項目」模板不適用於Tomcat?

WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/test/] in DispatcherServlet with name 'appServlet'. 

這是默認:/測試/ src目錄/主/ JAVA/RU /檢驗/試驗/HomeController.java

@Controller 
public class HomeController { 

    private static final Logger logger = LoggerFactory 
      .getLogger(HomeController.class); 

    /** 
    * Simply selects the home view to render by returning its name. 
    */ 
    @RequestMapping(value = "/", method = RequestMethod.GET) 
    public String home(Locale locale, Model model) { 
     logger.info("Welcome home! the client locale is " + locale.toString()); 

     Date date = new Date(); 
     DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, 
       DateFormat.LONG, locale); 

     String formattedDate = dateFormat.format(date); 

     model.addAttribute("serverTime", formattedDate); 

     return "home"; 
    } 

} 

這是默認:/test/src/main/webapp/WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters --> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/spring/root-context.xml</param-value> 
    </context-param> 

    <!-- Creates the Spring Container shared by all Servlets and Filters --> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

    <!-- Processes application requests --> 
    <servlet> 
     <servlet-name>appServlet</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>appServlet</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 

</web-app> 

這是默認/測試/ SRC /主/ web應用/ WEB-INF/SPR ING/appServlet/servlet的context.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:beans="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --> 

    <!-- Enables the Spring MVC @Controller programming model --> 
    <annotation-driven /> 

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --> 
    <resources mapping="/resources/**" location="/resources/" /> 

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --> 
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <beans:property name="prefix" value="/WEB-INF/views/" /> 
     <beans:property name="suffix" value=".jsp" /> 
    </beans:bean> 

    <context:component-scan base-package="ru.test.test" /> 



</beans:beans> 

默認/test/src/main/webapp/WEB-INF/spring/root-context.xml是空

回答

19

我得到確切同樣的問題,我解決了它。基本上,當創建Spring MVC項目時,默認情況下,Eclipse不會將src/main/webapp配置爲源目錄。可能需要作爲源目錄,因爲Eclipse會在構建它時對其進行不同的處理。

右鍵單擊'webapp'文件夾並單擊'構建路徑 - >用作源文件夾'爲我解決了這個問題。這裏的其他評論是錯誤的:我沒有改變我的RequestMapping或servlet url模式。 '/'都爲我的'localhost:8080/test /'工作。

0

在我看來像網址你試圖命中「/ test /」,web.xml只映射「/」。如果你想春天處理所有的URL,你可以把它改成「/ *」,然後你也必須把你的主控制器改爲「/ test」。

或者你可以只打你已經映射到家庭控制器的根URL的URL「http:// localhost:8080 /」。

+0

在錯誤我們看到「 appServlet「是指Servlet接受請求。 – Selector

+0

是的,你沒有說你要從瀏覽器中點擊什麼URL,但它看起來像你在做:http:// localhost:8080/test /它是未映射的。 – Kevin

+0

瀏覽器運行http:// localhost:8080/test /後打開默認網址。我嘗試將RequestMapping的值更改爲「/ test」或「/ test /」或「test /」,但這對我不起作用。 – Selector

0

在Eclipse中創建Spring模板MVC項目之後,必須在運行服務器之前手動構建它。

0

找到解決方案!

當你開始創建項目時,你必須定義它。 (注意第三級superappenter image description here

爲了獲得到應用程序,URL是 http://localhost:8080/superapp

這是爲我工作

相關問題