2012-08-06 74 views
0

我遇到了麻煩,希望得到您的幫助。我是Spring MVC的初學者(和Spring)。我遵循http://www.mkyong.com/spring3/spring-3-mvc-hello-world-example/但它沒有工作。我添加了一個歡迎文件(index.jsp)。當我輸入(http:// localhost:8080/SpringMVC)沒事。但是當我添加控制器模式(http:// localhost:8080/SpringMVC/welcome)時,它不起作用(HTTP狀態404)。這裏我CONFIGS:Spring MVC示例不能正常工作

的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> 
    <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</param-value> 
    </context-param> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
    <welcome-file-list> 
     <welcome-file>/index.jsp</welcome-file> 
    </welcome-file-list> 
</web-app> 

MVC-調度-servlet.xml中

<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation=" 
    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"> 
    <context:component-scan base-package="com.mkyong.common.controller" /> 
    <bean 
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix"> 
      <value>/WEB-INF/pages/</value> 
     </property> 
     <property name="suffix"> 
      <value>.jsp</value> 
     </property> 
    </bean> 
</beans> 

和我的文件夾結構是:

-> src 
    -> main 
     -> java 
     -> com 
      -> mkyong 
       -> common 
        -> controller 
        -> HelloController.java 
     -> resources 
     -> webapp 
     -> index.jsp 
     -> WEB-INF 
      -> mvc-dispatcher-servlet.xml 
      -> web.xml 
      -> pages 
       -> hello.jsp 

有人可以幫我?

+0

我可以推薦將日誌級別轉換爲DEBUG並查看Spring打印出的內容 - 如果Spring DispatcherServlet獲取請求,則會在日誌中看到很多應明確指出錯誤的信息。 – 2012-08-06 19:01:27

+0

我該怎麼做? – 2012-08-06 19:07:21

+0

試着將這些內容在classpath中log4j.properties文件 - log4j.rootLogger =跟蹤,標準輸出 log4j.appender.stdout = org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout = org.apache .log4j.PatternLayout log4j.appender.stdout.layout。ConversionPattern =%d [%t]%-5p%c - %m%n – 2012-08-06 19:17:44

回答

1

404意味着無法找到請求的資源。確保您的控制器都被註解:

@Controller and @RequestMapping("/welcome") 

從鏈接:

@Controller 
@RequestMapping("/welcome") 
public class HelloController { 

    @RequestMapping(method = RequestMethod.GET) 
    public String printWelcome(ModelMap model) { 

     model.addAttribute("message", "Spring 3 MVC Hello World"); 
     return "hello"; 

    } 

} 
+0

類** HelloController **用** @ Controller,RequestMapping **標記,因爲它必須是。 – 2012-08-06 18:24:00

0

你有你的servlet映射到「/」,和你想打控制器被接受爲「/歡迎請求'

您需要製作的請求應該是(http:// localhost:8080/welcome)。我不知道他爲什麼在他的例子中有額外的'SpringMVC'。

此外,將<mvc:annotation-driven/>添加到您的mvc-dispatcher-servlet.xml,以確保它在您的控制器上使用註釋。並且還添加mvc XML命名空間。我命名空間是這樣的:

<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:security="http://www.springframework.org/schema/security" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.1.xsd 
    http://www.springframework.org/schema/security 
    http://www.springframework.org/schema/security/spring-security-3.1.xsd"> 

我建議擺脫該歡迎文件,直到你有你的控制器正常工作,讓事情變得簡單

+0

發出這樣的請求** http:// localhost:8080/welcome **仍然返回HTTP狀態404。我不知道發生了什麼。 – 2012-08-06 18:36:20

+0

你使用的是什麼版本的春天? – MStodd 2012-08-06 18:37:18

+0

** 3.0.5.RELEASE **根據** pom.xml **。 – 2012-08-06 18:41:41

0

您必須映射到「/」的控制器。

@Controller 
@RequestMapping("/") 
public class StartController { 

    @RequestMapping(method = RequestMethod.GET) 
    public String printWelcome(ModelMap model) { 

    model.addAttribute("message", "Spring 3 MVC Hello World"); 
    return "hello"; 

    } 

} 
0

包括對你的看法這個庫,使${}

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 

這將解決您的問題。