簡體   English   中英

本地主機 Spring Boot 中的錯誤 404

[英]Error 404 in local host Spring Boot

在 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

結構

我的控制器類

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 和其他頁面

索引.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 看到它?

您需要將視圖添加到您的映射中。

有兩種不同的方法可以做到。

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/

默認情況下,spring boot 上的 viewresolver 將在 /src/main/resources/templates/ 中查找視圖。

我現在看不到您的結構,但嘗試將您的 html 視圖移動到該文件夾​​。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM