2013-05-01 131 views
0

我是Java Spring MVC的新手。 我创建了一个示例项目,但我似乎得到资源未找到错误Htto 404错误:资源未找到 - STS

的src /主/ web应用/ WEB-INF /春/ appServlet/servlet的context.xml中:

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns:mvc="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 --> 
<mvc:annotation-driven /> 

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --> 
<mvc: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"> 
    <property name="prefix" value="/WEB-INF/views/" /> 
    <property name="suffix" value=".jsp" /> 
</beans:bean> 

<context:component-scan base-package="com.shr.myapp" /> 

包融为一体。 shr.myapp.Controllers.TrampsController.java

package com.shr.myapp.Controllers; 

import java.util.*; 
import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 

@Controller 
public class TrampsController { 

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

private static List<String> tramps ; 

    public TrampsController() { 
     tramps= new ArrayList<String>(); 
     populate(tramps); 
    } 

    @RequestMapping(value = "/tramps", method = RequestMethod.GET) 
    public String list(Model model) { 
    logger.info("Listing Walks"); 
    model.addAttribute("tramps",tramps); 
    return "tramps/list"; 
    } 

private void populate(List<String> tramps) { 
    tramps.add("Lake Waikaremoana Great Walk");  
    tramps.add("Kepler Track"); 
    tramps.add("Milford Track"); 
    tramps.add("Routeburn Track"); 
    tramps.add("Rakiura Track"); 
    } 
} 

WEB-INF/views/tramps/list.jsp;

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<%@ page session="false" %> 
<html> 
<head> 
<link href="<c:url value="/resources/css/tramps.css" />" rel="stylesheet" type="text/css" media="screen" /> 
<title>Listing Tramps</title> 
</head> 
<body> 
<div class="container"> 
<h1>Tramps of New Zealand</h1> 


<table class="normal-table"> 
    <thead> 
     <tr> 
      <th>Key</th> 
     </tr> 
    </thead> 
    <tbody> 
    <c:forEach var="tramp" items="${tramps}"> 
     <tr> 
      <td><c:out value="${tramp}"/></td> 

     </tr> 
    </c:forEach> 
    </tbody> 
</table> 


</div> 
</body> 
</html> 

的web.xml

<?xml version="1.0" encoding="UTF-8"?> 

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> 

我不理解,我应该如何解决this.Please帮我

+0

请添加要使用 – 2013-05-01 15:36:14

+0

网址请加太多 – 2013-05-02 00:00:25

+0

@ Lilith2k3嗨,我还提供了web.xml file.please有web.xml中看看 – user1274646 2013-05-02 16:02:45

回答

0

我不确定这是你问题的根源,但是你的代码至少有两个错误。

你需要指定“豆”字头在你的servlet-context.xml的bean的条目。

<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> 

您的list.jsp中的tramp变量没有“key”和“value”属性,因为它的类型是String。

<c:forEach var="tramp" items="${tramps}"> 
    <tr> 
     <td>${tramp}</td> 
    </tr> 
</c:forEach> 

希望这会有所帮助。

+0

的List.jsp嘿感谢提mistakes.but IM仍然得到同样的error.is它DAT IM做一些错误,而重新运行project.i转到list.jsp页面 - >右键单击 - >运行 - >在服务器上运行。 这是wat我做的。 – user1274646 2013-05-02 16:28:28

1

基于要共享的网址,你正在使用错误的URL,你必须使用

localhost:8080/myapp/tramps 

localhost:8080/myapp/WEB-INF/views/tramps/list.jsp 

调用控制器的方法,如果你在Spring MVC中的新请我YT channel

+1

万一他的应用程序响应localhost:8080;) – 2013-05-02 18:20:21

+0

@Bobo Zohdy如何设置此网址? – user1274646 2013-05-03 12:07:14

+0

你正在使用@RequestMapping(value =“/ tramps”,method = RequestMethod.GET)这是映射“/ tramps”到这个控制器的方法,如果你是新的春天MVC检查我的YT频道这里http://www.youtube .COM /播放列表?列表= PL7350712B99EFC5DD – 2013-05-03 13:02:39