1

当我从 Eclipse 运行它时,我得到一个 404 请求的资源()不可用,并且在地址栏中我有http://localhost:8080/SpringMVCTest/web.xmlhttp://localhost:8080/SpringMVCTest/springMVCTest-servlet.xml。为什么它试图打开 xml 文件?

这是 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>SpringMVCTest</display-name>

    <servlet>
        <servlet-name>springMVCTest</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

</web-app>

这是 springMVCTest-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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
    <mvc:annotation-driven/>
    <mvc:resources mapping="/resources/**" location="/resources" />
    <context:component-scan base-package="/"></context:component-scan>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/"></property>
    <property name="suffix" value=".jsp"></property>
</bean>

</beans>

这是控制器

import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;



@Controller
public class SpringMVCTest {

    @RequestMapping({"/","/home"})
    public String showHomePage(Map<String, Object> model) {
        model.put("name", "Jess");
        return "home";
    }
}
4

1 回答 1

0

我会对此投下阴影:
您右键单击xml文件并选择 RUN

它没有其他方法尝试打开xml文件。我对吗?

更新
将这些添加到您的web.xml

<welcome-file-list>
    <welcome-file>index.jsp</welcome-file> <!-- Your index page -->
</welcome-file-list>

然后再次运行。

于 2012-08-08T11:34:13.483 回答