【问题标题】:Error 404 in local host Spring Boot本地主机 Spring Boot 中的错误 404
【发布时间】:2017-02-05 01:06:17
【问题描述】:

在 Spring Boot 中运行 localhost:8080 时出现错误 404

错误

 Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Tue Sep 27 14:44:36 EEST 2016
There was an unexpected error (type=Not Found, status=404).
No message available

Structure

我的控制器类

package chi.student.controller;


import chi.student.model.Student;
import chi.student.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class StudentController {
    private StudentRepository studentRepository;

    @Autowired
    @Qualifier(value = "studentRepository")
    public void setStudentRepository(StudentRepository studentRepository) {
        this.studentRepository = studentRepository;
    }


    @RequestMapping(value = "students", method = RequestMethod.GET)
    public String listStudents(Model model) {
        model.addAttribute("student", new Student());
        model.addAttribute("listStudents", this.studentRepository.findAll());
        return "students";
    }

    @RequestMapping(value = "/students/add", method = RequestMethod.POST)
    public String addStudent(@ModelAttribute("student") Student student) {
        if (student.getId() == 0) {
            this.studentRepository.save(student);
        } else {
            this.studentRepository.save(student);
        }
        return "redirect:/students";
              }

    @RequestMapping("/remove/{id}")
    public String removeStudent(@PathVariable("id") int id) {
        this.studentRepository.delete((long) id);
        return "redirect: /students";
    }

    @RequestMapping("edit/{id}")
    public String editStudent(@PathVariable("id") int id, Model model) {
        model.addAttribute("student", this.studentRepository.findOne((long) id));
        model.addAttribute("listStudents", this.studentRepository.findAll());
        return "students";

    }

    @RequestMapping("studentdata/{id}")
    public String studentData(@PathVariable("id") int id, Model model) {
        model.addAttribute("student", this.studentRepository.findOne((long) id));
        return "studentdata";

    }


    }

我的 build.gradle

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.1.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'

jar {
    baseName = 'gs-spring-boot'
    version =  '0.1.0'
}

repositories {
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
   }


dependencies {
    compile("org.springframework.boot:spring-boot-starter-actuator")
    compile("org.springframework.boot:spring-boot-starter-web")
    compile("org.springframework.boot:spring-boot-starter-thymeleaf")
    compile("org.springframework.boot:spring-boot-starter-data-jpa")
    compile 'org.springframework.boot:spring-boot-starter-tomcat'
    compile("org.springframework.boot:spring-boot-starter-security")
    compile("mysql:mysql-connector-java")

    testCompile('org.springframework.boot:spring-boot-starter-test')
    compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.0'
    compile group: 'org.apache.commons', name: 'commons-digester3', version: '3.2'
    compile group: 'commons-logging', name: 'commons-logging', version: '1.1.1'



}

我做错了什么?它找不到我的 index.jsp 和其他页面

index.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>StudentBook</title>
</head>
<body background="http://s1.hostingkartinok.com/uploads/images/2012/10/9b5f28f249b60412a4cf9cbc7cf8af4b.jpg">
<h1 align="center">Welcome!</h1>
</body>
</html>

我应该在哪里提供我的 index.jsp 以让 Controller 看到它?

【问题讨论】:

  • 你想访问什么网址?
  • 以上,您尝试访问的网址不存在....
  • 我会尝试访问 localhost:8080/index
  • 在您放置的控制器中,它不存在。我推荐你spring-boot-actuator,把它放在你的pom中然后转到localhost:8080/mappings:你可以看到你所有的映射,并确定/index是否存在。在您的情况下,我确定没有索引映射
  • 当我尝试获取 "localhost:8080/mappings" 时它会询问用户和密码,但我已经从我的 spring-boot 中禁用了 spring security

标签: java mysql gradle spring-boot spring-data-jpa


【解决方案1】:

您需要将视图添加到您的映射中。

有两种不同的方法来做到这一点。

1) 配置WebMvcConfigurerAdapter

@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/index").setViewName("index");
     }
}

2) 在控制器的请求映射中返回视图

@Controller
public class StudentController {
    ....
    @RequestMapping(value = "index", method = RequestMethod.GET)
    public String listStudents(Model model) {
        //your impl if is need
        return "index"; //or the view you want
    }
}

正如@wilsoncampusaon 所说,您的观点应该是/src/main/resources/templates/

【讨论】:

  • 您的第二种方式与我的控制器有何不同?我无法理解
  • 除了 index.jsp(学生数据和学生)我还有 2 个页面我需要在每个页面上添加视图控制器吗?
  • 抱歉这个问题,但我应该使用 .html 还是 .jsp 可以吗?
  • 这取决于你的需要,两者都是有效的。
【解决方案2】:

默认情况下,spring boot 上的 viewresolver 会在 /src/main/resources/templates/ 中查找视图。

我现在看不到您的结构,但尝试将您的 html 视图移动到该文件夹​​。

【讨论】:

  • 谢谢@pauchorro,我看不到他的结构......但他说他找不到他的页面。你觉得呢?
  • 我认为他忘记创建映射返回视图或添加视图控制器
猜你喜欢
  • 2013-08-14
  • 2018-06-27
  • 1970-01-01
  • 1970-01-01
  • 2019-03-12
  • 2020-04-03
  • 2015-09-27
  • 2019-07-13
  • 2018-10-06
相关资源
最近更新 更多