【问题标题】:Spring MVC and Maven (404 error: Resource )Spring MVC 和 Maven(404 错误:资源)
【发布时间】:2020-07-30 15:14:25
【问题描述】:

这是我认为我配置正确的文件。我的 Tomcat 9 应用程序已启动并正常运行。我只是不知道这是什么问题。 Tomcat 部署应用程序,但我不确定问题是否在 Tomcat 或 Spring MVC 上出现

rcontroller.java:

package com.mycompany.myspring;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("/")
public class rcontroller {
    @ResponseBody
    public String getFoosBySimplePath() {
    return "Get some Foos";
}


    
}

dispatch-servlet.xml:

<beans:beans xmlns="https://www.springframework.org/schema/mvc"
    xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:beans="https://www.springframework.org/schema/beans"
    xmlns:context="https://www.springframework.org/schema/context"
    xsi:schemaLocation="https://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
        https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
        https://www.springframework.org/schema/context https://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 />
    <context:component-scan base-package="com.mycompany.myspring" />

    <!-- 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">
        <beans:property name="prefix" value="/WEB-INF/" />
        <beans:property name="suffix" value=".html" />
    </beans:bean>

</beans:beans>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://java.sun.com/xml/ns/javaee" xsi:schemaLocation="https://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>spring-mvc-example</display-name>

    <!-- Add Spring MVC DispatcherServlet as front controller -->
    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>
                org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>WEB-INF/dispatch-servlet.xml</param-value>
            </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
 
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern> 
    </servlet-mapping>
    
 </web-app>

pom.xml:

<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany.myspring</groupId>
    <artifactId>SpringMVC</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>SpringMVC</name>
    <description>Spring MVC Hello World Example</description>

    <!-- Add Spring Web and MVC dependencies -->
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.3.9.RELEASE</version>
        </dependency>
        <!-- Servlet -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.0.0</version>
                <configuration>
                    <warSourceDirectory>WebContent</warSourceDirectory>
                </configuration>
            </plugin>
        </plugins>
        <finalName>${project.artifactId}</finalName> <!-- added to remove Version from WAR file -->
    </build>
</project>

项目结构:

【问题讨论】:

    标签: java spring spring-mvc maven-3


    【解决方案1】:

    在您的文件夹结构中,index.html 文件位于 webapp 文件夹下。但是在 dispatch-servlet.xml 文件中,您将路径设置为 WEB-INF 文件夹。

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

    因此,将 index.html 文件移动到 WEB-INF 文件夹中

    然后,在 web.xml 文件中配置欢迎文件,如下所示,

    <display-name>spring-mvc-example</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
    

    【讨论】:

    • 但是如果你看到rcontroller.java 我返回一个字符串。我想返回那个字符串而不是index.html
    • @TOMAS 不用担心欢迎文件配置。上面的代码简单地返回Get some Foos。请尝试一下
    • 这是错误:HTTP 404 Status - Not Found Type Status report message / SpringMVC Description The required resource is not available. Apache Tomcat / 9.0.24
    【解决方案2】:

    你的问题不是很清楚。究竟是什么问题?

    反正你的dispatch-servlet.xml有问题

    1- 行

     <beans:bean>
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    

    应该是

     <beans:bean id="viewResolver" 
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    

    I.E.你不应该在类声明之前关闭标签bean,应该给它分配一个id

    2- 我相信您可能对命名空间 beans: 有一些问题。如果为真,最好的想法是删除 beans: 命名空间前缀。关注这个答案

    The prefix "beans" for element "beans:beans" is not bound

    3- contextConfigLocation 应该是/WEB-INF/dispatch-servlet.xml

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

    4- 在您的控制器中,您应该使用 @GetMapping 或 òRequestMapping(method=RequestMethod.GET) 映射您想要公开的方法

    @GetMapping
    @ResponseBody
    public String getFoosBySimplePath() {
    

    我也建议不要将控制器映射到 / 而是映射到某个子路径

    【讨论】:

    • 如果问题不清楚,我深表歉意。我按照你说的做了,但问题仍然存在我得到了404.The required resource is not available. 当我清理和构建时,我会在日志底部发送一条消息/home/educacion/NetBeansProjects/MYSPRING/target/SpringMVC/META-INF/context.xml (the file or directory does not exist)
    • 你好,现在我明白了。对于构建问题,请尝试我添加到答案中的第 3 点。对于 404 问题,这很正常:您返回一个不是视图名称的字符串。您只想返回一个字符串作为响应正文?
    • 是的,我只想返回一个字符串。但是我不确定将 dispatch-servlet 放在哪里,因为 Maven 将 web.xml 放在了 Other sources 地毯上,但我不确定将 dispatch-servlet.xml 放在哪里。
    • 我也编辑了我的问题。我改变了项目结构的形象。它显示了 Maven 为 Web 应用程序创建的结构。在该项目中,index.html 显示在浏览器上。但是当我点击浏览器上的地址时,我映射了@RequestMapping 似乎不起作用。我尝试将其更改为/hello,但问题仍然存在。
    • maven如何在target下构建文件夹结构?
    猜你喜欢
    • 1970-01-01
    • 2017-04-07
    • 2017-05-09
    • 2015-08-20
    • 1970-01-01
    • 2017-10-13
    • 2019-04-17
    • 1970-01-01
    • 2012-08-03
    相关资源
    最近更新 更多