Spring Boot:使用控制器调用JSP时出现404错误
使用Spring Tool Suite运行项目时出现以下错误,
I'm getting the following error when running my project using Spring Tool Suite,
但是如果我的问题是我已经将适当的依赖项添加到了pom.XML
文件中.那么可能是什么问题?
But in case my problem is I have already added the appropriate dependencies to pom.XML
file. So what could be the problem?
我的pom.XML
文件依赖性如下,
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
我的控制器ApplicationController.java
如下,
package com.example.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class ApplicationController {
@RequestMapping("/")
public String Welcome() {
return "welcomepage";
}
}
我的灵魂在src/main/webapp/WEB-INF/view/welcomepage.jsp
中,您可以在下面查看树状视图,
My vives are in src/main/webapp/WEB-INF/view/welcomepage.jsp
you can look the tree view below,
我也已经更改了application.properties
文件.但是,我还是不明白哪里出了问题.
And I have already changed the application.properties
file as well. But still, I can't understand what is wrong.
我的application.properties
文件如下,
spring.mvc.view.prefix=/WEB-INF/view/
spring.mvc.view.suffix=.jsp
我只是在我的welcomepage.jsp
中打个招呼,
I just print hello in My welcomepage.jsp
,
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
hello
</body>
</html>
看起来您与正在运行的应用程序非常接近.代码中的主要问题是Jasper依赖项在<scope>provided</scope>
中.
而且看起来您正在通过main
方法从Eclipse IDE运行代码.
Looks like you was very close to the working application. The main issue in your code is in <scope>provided</scope>
for your Jasper dependency.
And also looks like you are running your code from eclipse IDE through the main
method.
长话短说:
如果要通过MyApplication.java
中的main
方法运行应用程序,则只需删除Jasper的作用域provided
.
If you would like to run your application through the main
method in MyApplication.java
then just remove scope provided
for the Jasper.
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
或者您也可以像现在从控制台一样在那种状态下完全运行您的应用程序:
Or you can run your application exactly in that state like you have right now from the console:
mvn clean spring-boot:run
但是我建议删除此作用域,以便您可以从IDE和控制台运行代码.除此之外,spring-boot-starter-tomcat
依赖项似乎是多余的(必须在spring-boot-starter-web
中可用).简而言之,请尝试使用以下pom文件:
But I suggest to remove this scope so you could be able to run your code from IDE and from console. In addition to that looks like spring-boot-starter-tomcat
dependency is redundant (it must be available within spring-boot-starter-web
). In a nutshell please try to use following pom file:
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
</dependencies>
</project>
希望我的回答对您有所帮助.
Hope my answer will help you.