【问题标题】:Spring project in Eclipse - 404Eclipse 中的 Spring 项目 - 404
【发布时间】:2011-12-18 21:01:08
【问题描述】:

我尝试在 Tomcat 上运行该应用程序,但不断收到此错误消息“请求的资源 (/testapp/) 不可用。” - 可能有什么问题?我猜我的 XML 设置不正确,但不知道如何修复它。

调度程序-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:mvc="http://www.springframework.org/schema/mvc"
    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/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-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="controller" />

    <mvc:view-controller path="/" view-name="HelloWorldPage" />

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

</beans>

web.xml

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

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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" version="2.5">

    <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>*.htm</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

</web-app>

HelloWorldController.java - 使用 Spring 注释。

package controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

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

    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView helloWorld() {

        ModelAndView model = new ModelAndView("HelloWorldPage");
        model.addObject("msg", "hello world");

        return model;
    }
}

【问题讨论】:

  • 部署时的错误/异常是什么?
  • 这是你得到的运行时错误页面,但我问的是部署时是否有任何异常
  • tomcat目录下没有JSP文件很奇怪
  • 查看tomcat启动日志。我认为您的应用程序无法启动似乎是合理的; Tomcat 启动了,但是 "testapp" 启动失败
  • 在 localhost_access_log - 最新行有“HTTP/1.1” 404 985"

标签: java eclipse spring jsp tomcat


【解决方案1】:

在您的 web.xml 中,您将 spring 映射到 *.htm,但您的控制器被映射到

@RequestMapping("/welcome")

你应该映射到

@RequestMapping("/welcome.htm")

【讨论】:

    【解决方案2】:

    如果您在 Eclipse 控制台上没有遇到任何异常,我认为您可能没有找到项目的正确 contextRoot,主要是您的应用程序的确切名称,但有时会有所不同......

    如果您没有输入错误的应用名称,请尝试此操作。

    点击项目鼠标右键点击属性。

    然后搜索上下文。

    您将找到一个名为 ContextRoot 的部分

    使用该框中的字符串像这样挂载您的 Contextroot

    localhost:8080/stringFound/

    然后在浏览器中按回车键。

    【讨论】:

    • 上下文根与项目名称相同。
    猜你喜欢
    • 2018-11-12
    • 2012-08-08
    • 1970-01-01
    • 2019-12-13
    • 2012-11-12
    • 1970-01-01
    • 2013-04-05
    • 1970-01-01
    • 2018-09-29
    相关资源
    最近更新 更多