我在Spring Boot中运行localhost:8080时遇到错误404
错误
keydown: function(ev) {
var self = $(this);
clearTimeout(self.data('timer'));
if (typeof self.data('xhr') === 'object' && 'abort' in self.data('xhr')) {
self.data('xhr').abort();
}
var timer = setTimeout(function() {
self.data('xhr', $.ajax({
url: '/files/tags_autocomplete.php',
dataType: 'JSON',
success: function(tags) {
$("ul").html(tags.output);
}
}));
}, 500);
self.data('timer', timer);
我的控制器类
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
我的build.gradle
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";
}
}
我做错了什么?它无法找到我的index.jsp和其他页面
的index.jsp
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以使Controller看到它?
答案 0 :(得分:1)
您需要将视图添加到映射中。
有两种不同的方法可以做到。
@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/index").setViewName("index");
}
}
@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/
。
答案 1 :(得分:0)
默认情况下,spring boot上的viewresolver将在/ src / main / resources / templates /中查找视图。
我现在无法看到您的结构,但尝试将您的html视图移动到该文件夹。