【问题标题】:Spring MVC 5 program returning an error 404Spring MVC 5 程序返回错误 404
【发布时间】:2018-12-08 12:07:48
【问题描述】:

这是我第一个使用 Spring MVC 5 的程序,它返回错误 404。 我正在使用 Tomcat 7,虽然我也尝试过 Tomcat 8

This is my directory structure

我使用了基于注解的配置,这里是我的程序的组件

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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>SpringMVCProject</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

    <servlet>
    <servlet-name>webapp-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-web-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

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

</web-app

弹簧配置文件::

        <?xml version="1.0" encoding="UTF-8"?>
    <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"
      xmlns:mvc="http://www.springframework.org/schema/mvc"
      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.pgx.java.web" />
      <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/view/jsp/" />
        <property name="suffix" value=".jsp" />
      </bean>
      <mvc:resources mapping="/resources/**" location="/resources/" />
      <mvc:annotation-driven />
    </beans>

我的控制器

    package com.pgx.java.web;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import com.pgx.java.bean.Person;
@Controller
public class MyController {
    @GetMapping("/Hello")
    public String helloView(Model model) {

        Person p = new Person();
        p.setTitle("Dr.");
        p.setFirstName("John");
        p.setLastName("Watson");

        model.addAttribute("person", p);

        return "HelloWorld";
    }
}

人物类

 package com.pgx.java.bean;
public class Person {

  private String title;
  private String firstName;
  private String lastName;

  public String getTitle() {
    return title;
  }

  public void setTitle(String title) {
    this.title = title;
  }

  public String getFirstName() {
    return firstName;
  }

  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }

  public String getLastName() {
    return lastName;
  }

  public void setLastName(String lastName) {
    this.lastName = lastName;
  }
}

HelloWorld.jsp

   <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>Hello World!</title>
    </head>
    <body>
        Welcome ${person.title} ${person.firstName} ${person.lastName}!
    </body>
</html>

【问题讨论】:

    标签: spring spring-mvc model-view-controller http-status-code-404


    【解决方案1】:

    您的 jsp 文件 HelloWorld.jsp 位于错误的位置。它应该在WEB-INF/view/jsp/ 文件夹中。在WEB-INFview 文件夹中创建一个文件夹view,将您的HelloWorld.jsp 放入您的HelloWorld.jsp

    【讨论】:

    • 我已经尝试过您的解决方案,但仍然遇到同样的错误。我正在用我的目录结构的新图像更新问题。
    猜你喜欢
    • 2021-04-06
    • 2019-03-28
    • 2021-09-06
    • 2019-10-22
    • 1970-01-01
    • 2015-07-09
    • 1970-01-01
    • 1970-01-01
    • 2011-08-03
    相关资源
    最近更新 更多