2013-06-29 192 views
1

我使用Spring Tool Suite在eclipse中開發一個簡單的Spring 3。但是當我運行時,「HTTP狀態404所請求的資源(/ hxj /)不可用」是返回。Spring 3 HTTP狀態404

在Eclipse的控制檯:沒有錯誤,但有些信息看起來奇怪:

Jun 29, 2013 7:50:47 PM org.apache.catalina.loader.WebappClassLoader validateJarFile 
INFO: validateJarFile(H:\My Documents\eclipse\.metadata\.plugins\org.eclipse.wst.server.core 
\tmp1\wtpwebapps\Spring\WEB-INF\lib\jsp-api-2.1.jar) - jar not loaded. 

Jun 29, 2013 7:50:47 PM org.apache.catalina.loader.WebappClassLoader validateJarFile 
INFO: validateJarFile(H:\My Documents\eclipse\.metadata\.plugins\org.eclipse.wst.server.core 
\tmp1\wtpwebapps\Spring\WEB-INF\lib\servlet-api-2.5.jar) - jar not loaded. 

以下是我的web.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app 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/javaee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.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</listene-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>*.do</url-pattern> 
</servlet-mapping> 
<welcome-file-list> 
    <welcome-file>home.jsp</welcome-file> 
</welcome-file-list> 
</web-app> 

和Java類的主要部分:HomeController的的.java

package com.hxj.hxj; 


@Controller 
public class HomeController { 

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

@RequestMapping(value = "/home", method = RequestMethod.GET) 
public String home(Locale locale, Model model) { 
    logger.info("Welcome home! The client locale is {}.", locale); 

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

    String formattedDate = dateFormat.format(date); 

    model.addAttribute("serverTime", formattedDate); 

    return "home"; 
} 

} 

在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.xsd 
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans 
    /spring-beans.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema 
/context/spring-context.xsd"> 

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

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


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

<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="com.hxj.hxj" /> 

</beans:beans> 

我不知道爲什麼它不起作用,因爲它是一個spring springte項目的spring MVC項目。任何人都可以幫我解決這個問題嗎?

非常感謝! 夏娃

編輯: 此外,爲什麼網址是:本地主機:8080/HXJ/ 代替:本地主機:8080/HXJ /回到Home.jsp(或/home.do)?

感謝

+1

嘗試使,在web.xml:' appServlet /* 而不是'* .do'。 – acdcjunior

+0

我改變了它,但仍然收到錯誤警告:WARN:org.springframework.web.servlet.PageNotFound - 在名爲'appServlet'的DispatcherServlet中找不到具有URI [/ hxj /]的HTTP請求的映射。但我已經在web.xml中定義了appServlet。任何消化?謝謝@acdcjunior – Eve

回答

2

Acdcjunior是在他的評論中分辯:

嘗試使,在web.xml:

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

,而不是*。做。


你問:

此外,爲什麼網址是:本地主機:8080/HXJ /而不是:本地主機:8080/HXJ /回到Home.jsp(或/home.do)?

我想到的是正確的網址是:localhost:8080/hxj/home (僅localhost:8080/hxj/作品,因爲在web.xml歡迎URL映射)

那是因爲這是你的控制器方法指定的請求映射。它不是* .jsp,因爲在Spring中使用url來指定應該調用哪個控制器方法,而不是直接使用jsp。 (而且這還不是*。做,因爲這不是支柱)

+0

我修改了它,但我仍然得到這個錯誤警告:WARN:org.springframework.web.servlet.PageNotFound - 在名爲'appServlet'的DispatcherServlet中沒有找到具有URI [/ hxj /]的HTTP請求的映射。但是我在web.xml中定義了appServlet。 @Ralph – Eve

+0

向Home Controller方法添加一些println語句。當你請求網址時是否打印? – Ralph

0

文件HomeController.java嘗試

package com.hxj.hxj; 


@Controller 
@RequestMapping(value = "/home", method = RequestMethod.GET) 
public class HomeController { 

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

@RequestMapping(method = RequestMethod.GET) 
public String home(Locale locale, Model model) { 
logger.info("Welcome home! The client locale is {}.", locale); 

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

String formattedDate = dateFormat.format(date); 

model.addAttribute("serverTime", formattedDate); 

return "home"; 
} 
} 

在你的src \主\ web應用程序把redirect.jsp中

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<% response.sendRedirect("home"); %> 

在web.xml中添加歡迎文件列表

<welcome-file-list> 
    <welcome-file>redirect.jsp</welcome-file> 
</welcome-file-list> 

servlet映射

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

確保文件在家。JSP在/ WEB-INF /視圖/目錄