2015-09-15 149 views
2

我正試圖訪問我機器上的http://localhost:8080/EventTracker/greeting。但是,我收到了404錯誤。我正在關注Spring MVC4教程的PluralSight簡介,並且好像我的代碼與視頻中的代碼相匹配。我正在使用兩個Java文件WebConfig和WebAppInitializer來配置我的應用程序。我錯過了什麼?我認爲我已經逐行復制,但仍然無法正常工作。HTTP狀態404 -/EventTracker/greeting

HelloController.java

@Controller 
public class HelloController { 

    @RequestMapping(value="/greeting") 
    public String sayHello(Model model) { 
     model.addAttribute("greeting", "Hello World"); 

     return "hello.jsp"; 
    } 
} 

WebAppInitializer.java

public class WebAppInitializer implements WebApplicationInitializer{ 

    @Override 
    public void onStartup(ServletContext servletContext) throws ServletException { 
     WebApplicationContext context = getContext(); 
     servletContext.addListener(new ContextLoaderListener(context)); 
     ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet", new DispatcherServlet(context)); 
     dispatcher.setLoadOnStartup(1); 
     dispatcher.addMapping("*.html"); 

    } 

    private WebApplicationContext getContext() { 
     AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); 
     context.setConfigLocation("com.pluralsight.WebConfig"); 
     return context; 
    } 
} 

WebConfig.java

@Configuration 
@EnableWebMvc 
@ComponentScan(basePackages = "com.pluralsight") 
public class WebConfig { 

} 

的hello.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Insert title here</title> 
</head> 
<body> 
    <h1>${greeting}</h1> 
</body> 
</html> 

EDITED 9/15 4:25 PM PST 當使用http://localhost:8080/EventTracker/greeting.html,我仍然得到同樣的錯誤,錯誤的存在:

16:24:41.925 [http-nio-8080-exec-3] DEBUG o.s.web.servlet.DispatcherServlet - DispatcherServlet with name 'DispatcherServlet' processing GET request for [/EventTracker/greeting.html] 
16:24:41.931 [http-nio-8080-exec-3] WARN o.s.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/EventTracker/greeting.html] in DispatcherServlet with name 'DispatcherServlet' 
16:24:41.931 [http-nio-8080-exec-3] DEBUG o.s.web.servlet.DispatcherServlet - Successfully completed request 
+0

你能發佈你的DispatcherServlet.xml代碼嗎?看起來您的錯誤與您的應用沒有在您的DispatcherServlet文件中找到正確的映射有關。 – jeffkempf

+0

我沒有任何用於配置的xml文件。所有配置都由java文件完成 – LinhSaysHi

+0

如果您決定使用xml文件,我發佈了一個答案,可以幫助您解決404錯誤。我不確定只使用java文件。 – jeffkempf

回答

-1

您的網址http://localhost:8080/EventTracker/greeting不符合您的調度映射:dispatcher.addMapping("*.html");。嘗試http://localhost:8080/EventTracker/greeting.html

0

我從來沒有創建過沒有XML配置文件的Spring MVC應用程序。我相信這是可能的,但我不得不看看它。如果你不介意使用XML文件進行配置,你可以做類似於以下的事情(這是我之前做的一個小方案,以便更好地熟悉Spring MVC):

web.xml文件

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
id="WebApp_ID" version="2.5"> 
<display-name>Spring3MVC</display-name> 
<welcome-file-list> 
    <welcome-file>index.jsp</welcome-file> 
</welcome-file-list> 

<servlet> 
    <servlet-name>spring</servlet-name> 
    <servlet-class> 
     org.springframework.web.servlet.DispatcherServlet 
    </servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping> 
    <servlet-name>spring</servlet-name> 
    <url-pattern>*.html</url-pattern> 
</servlet-mapping> 

<!--if not using jsp, can omit this --> 
<jsp-config> <!-- if taglib not inside jsp-config, will cause deployment errors --> 
<taglib> 
    <taglib-uri>http://java.sun.com/jsp/jstl/core</taglib-uri> 
    <taglib-location>/WEB-INF/taglib/c.tld</taglib-location> 
</taglib> 
</jsp-config> 

web.xml中是配置的頂級。從上面,要記住的重要一點是,文件(它也將是一個XML文件)的名稱是調度程序servlet文件。無論你在標籤中包含了什麼,它都會附加-servlet.xml,所以在這種情況下,我的調度程序servlet將是一個名爲spring-servlet.xml的文件。該標籤告訴應用程序哪種類型的url模式與調度程序servlet相關聯。所以在這個例子中,任何以.html結尾的URL都會被spring-servlet.xml處理。

如果您使用的是JSP,請確保您的所有標籤都在標籤內,否則將無法正常工作。

爲spring-servlet.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:p="http://www.springframework.org/schema/p" 
xmlns:context="http://www.springframework.org/schema/context" 
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="net.viralpatel.spring3.controller" /> 
    <!-- declares package where controller(s) stored. Don't need to declare indvd controllers --> 

<bean id="viewResolver" 
    class="org.springframework.web.servlet.view.UrlBasedViewResolver"> 
    <property name="viewClass" 
     value="org.springframework.web.servlet.view.JstlView" /> 
    <property name="prefix" value="/WEB-INF/jsp/" /> <!-- hello.jsp must be located in this directory for link to work --> 
    <property name="suffix" value=".jsp" /> 
</bean> 

這是調度的servlet文件的樣子。您可以忽略頂部的代碼。根據你發佈的錯誤,我的猜測是你的Java代碼遺漏了或錯誤地映射了你的視圖解析器。視圖解析器是將你傳入你的控制器的字符串(即:hello)轉換成你的相對URL路徑(即:/WEB-INF/jsp/hello.jsp)。爲了正確工作,確保所有jsp文件都在同一個目錄中,並將該目錄列爲前綴值的一部分。在這個例子中,我將所有的jsp文件存儲在我的WEB-INF目錄下的一個名爲jsp的目錄中。本例中的後綴只是文件擴展名。任何未存儲在此目錄中的文件都會導致您的應用在嘗試加載缺少的文件時引發404錯誤。

我知道這不完全是你設定的,但如果你選擇使用XML文件,我希望這有助於。如果您有任何問題,請告訴我。

6

因爲這是我在谷歌找到的第一個問題,在這裏沒有正確的答案,這是什麼幫助我。

你應該WebAppInitializer.java添加下一個

context.register(com.pluralsight.WebConfig.class); 

所以,你的文件應該是這樣的:

public class WebAppInitializer implements WebApplicationInitializer{ 

    @Override 
    public void onStartup(ServletContext servletContext) throws ServletException { 
     WebApplicationContext context = getContext(); 
     servletContext.addListener(new ContextLoaderListener(context)); 
     ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet", new DispatcherServlet(context)); 
     dispatcher.setLoadOnStartup(1); 
     dispatcher.addMapping("*.html"); 

    } 

    private WebApplicationContext getContext() { 
     AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); 
     context.setConfigLocation("com.pluralsight.WebConfig"); 
     context.register(com.pluralsight.WebConfig.class); 
     return context; 
    } 
} 
+4

這必須被標記爲正確的答案!這項工作對我來說 – angel

0

我有同樣的問題。 問題是我的Tomcat安裝文件夾中沒有「本地庫」。 我解決與:

命令和apt-get安裝libtcnative-1

然後,我有這個問題我的本地庫版本太老了,我解決與升級:

sudo易於得到升級libtcnative-1

我希望這將有助於:)