书单推荐:成为Java顶级程序员架构师 ,这20来本(高薪)必看点击获取
一、首先查看目录结构
二、出现500错误的原因
访问url:http://127.0.0.1:8080/hello
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Thu Jan 31 14:19:44 CST 2019
There was an unexpected error (type=Internal Server Error, status=500).
Circular view path [hello]: would dispatch back to the current handler URL [/hello] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)
由于application.properties文件位置错误导致出现了500错误。
正确路径是src/main/resources/application.properties
错误路径是src/test/resources/application.properties
更正文件路径之后,完美执行了。
三、出现404错误的原因
访问url:http://127.0.0.1:8080/hello
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Thu Jan 31 14:35:20 CST 2019
There was an unexpected error (type=Not Found, status=404).
No message available
出现404的错误经过排查是2个原因造成的
1、由于application.properties文件位置错误
正确路径是src/main/resources/application.properties
错误路径是src/test/resources/application.properties
2、由于application.properties文件位置错误,模板名称为helloccc.jsp时根本映射不到。
src/main/resources/application.properties修改为src/test/resources/application.properties之后测试完美执行
我为了测试写的是helloccc测试结束之后helloccc都修改成了hello
四、以下代码是修改错误之后能够正确执行的代码
1、Application.java
java;toolbar:false">package com.how2java.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
2、HelloController.java
package com.how2java.springboot.web; import java.text.DateFormat; import java.util.Date; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class HelloController { @RequestMapping("/hello") public String hello(Model m) { m.addAttribute("now", DateFormat.getDateTimeInstance().format(new Date())); return "helloccc"; } }
3、hello.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> Hi JSP. 现在时间是 ${now}
4、application.properties
spring.mvc.view.prefix=/WEB-INF/jsp/ spring.mvc.view.suffix=.jsp
5、pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.how2java</groupId> <artifactId>springboot</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot</name> <description>springboot</description> <packaging>war</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- servlet依赖. --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <!-- tomcat的支持.--> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> </dependencies> <properties> <java.version>1.8</java.version> </properties> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>