HTTP状态404 - 类型状态报告消息说明请求的资源不可用。 Apache Tomcat / 8.0.33

时间:2018-04-01 06:26:52

标签: java spring spring-mvc

这是我在浏览器中收到的错误 对于请求:

http://localhost:8090/FirstSpringMVCProject/welcome.html

HTTP状态404 - 类型状态报告
消息
description所请求的资源不可用 Apache Tomcat / 8.0.33

这是我的HelloController.java

package com.amarnath.spring.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class HelloController extends AbstractController{

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest req, 
HttpServletResponse res) throws Exception {
        ModelAndView modelAndView = new ModelAndView("HelloPage");
        modelAndView.addObject("welcomeMessage", "welcome to the first spring mvc 
application");
        return null;

        //welcome to.. will be display at welcomeMessage variable on Hellopage
    }

}

这是我的spring-dispatcher-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">


    <bean id="HandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />


    <bean name="/welcome.html" class="com.amarnath.spring.controller.HelloController" />


    <bean id="viewResolver" class="org.springframework.spring.view.InternalResourceViewResolver">

        <property name="prefix">
            <value>/WEB-INF/</value>
        </property>
        <property name="suffix">
            <value>.jps</value>
        </property>

     </bean>
</beans>

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" 
version="3.1">

    <display-name>FirstSpringMVCProject</display-name>
    <servlet>
        <servlet-name>spring-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    </servlet>
    <servlet-mapping>
        <servlet-name>spring-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>

    </servlet-mapping>
</web-app>

HelloPage.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>First spring MVC application demo</h1>
        <h2>$(welcomeMessage)</h2>
    </body>
</html>

在构建路径中,我设置了spring 4.3和commons-logging Build path

这是文件夹结构 folder structure

2 个答案:

答案 0 :(得分:0)

我对所有弹簧配置都不是很熟悉,但会像这样设置servlet.xml:

    <beans:beans
        xmlns:beans="http://www.springframework.org/schema/beans"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd">
        <context:component-scan base-package="com.amarnath.spring.controller"
                                annotation-config="true" />
        <mvc:annotation-driven />
        <beans:bean id="viewResolver"
                            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <beans:property name="prefix"
                            value="/WEB-INF/" />
            <beans:property name="suffix"
                            value=".jsp" />
        </beans:bean>
        <mvc:default-servlet-handler />
    </beans:beans>

并将HelloController更改为:

    package com.amarnath.spring.controller;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.mvc.AbstractController;

    @Controller
    public class HelloController {

    @GetMapping("/")
    public ModelAndView handleRequestInternal() {
    ModelAndView modelAndView = new ModelAndView("HelloPage");
    modelAndView.addObject("welcomeMessage", "welcome to the first spring mvc application");
        return modelAndView;

        //welcome to.. will be display at welcomeMessage variable on Hellopage
    }

    }

之后你应该通过http://localhost:8090/FirstSpringMVCProject/

到达HelloPage.jsp

答案 1 :(得分:0)

你的方法handleRequestInternal返回null,改变代码如下: -

@Override
protected ModelAndView handleRequestInternal(HttpServletRequest req, 
HttpServletResponse res) throws Exception {
    ModelAndView modelAndView = new ModelAndView("HelloPage");
    modelAndView.addObject("welcomeMessage", "welcome to the first spring mvc 
application");
    return modelAndView;

    //welcome to.. will be display at welcomeMessage variable on Hellopage
}