【发布时间】: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
我的控制器类
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