in springboot after adding dependency file i have /newart.jsp throw where i do mistake

huangapple 未分类评论46阅读模式
英文:

in springboot after adding dependency file i have /newart.jsp throw where i do mistake

问题

Spring Boot这是我使用的但在运行时出现了错误
这是我的Java文件

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class MusicConcert {
    @RequestMapping(value = "/newart", method = RequestMethod.GET)
    public String newart() {
        System.out.println("sdf");
        return "newart.jsp";
    }
}

这是我的hello.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>
hi this is java
</body>
</html>

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 https://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.2.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.telusco</groupId>
    <artifactId>respect</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>respect</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.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper -->
        <dependency>

        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-jasper</artifactId>
        <version>9.0.33</version>
        </dependency>
    </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

错误信息:
白标签错误页面
该应用程序没有明确映射到/error,所以您看到这是一个回退。

Sun May 03 23:57:58 IST 2020
发生了意外错误(类型=未找到,状态=404)。
/newart.jsp


<details>
<summary>英文:</summary>

Spring boot this i used but i got error while runing
Its my Java File
   

        import org.springframework.stereotype.Controller;
        import org.springframework.web.bind.annotation.RequestMapping;
        import org.springframework.web.bind.annotation.RequestMethod;
        @Controller
        public class MusicConcert {
        @RequestMapping(value = &quot;/newart&quot;, method = RequestMethod.GET)
    	public String newart() {
        	    System.out.println(&quot;sdf&quot;);
    		    return &quot;newart.jsp&quot;;
    	    }
        }

It my hello.jsp File
    
   

     &lt;%@ page language=&quot;java&quot; contentType=&quot;text/html; charset=ISO-8859-1&quot;
        pageEncoding=&quot;ISO-8859-1&quot;%&gt;
        &lt;!DOCTYPE html&gt;
        &lt;html&gt;
        &lt;head&gt;
        &lt;meta charset=&quot;ISO-8859-1&quot;&gt;
        &lt;title&gt;Insert title here&lt;/title&gt;
        &lt;/head&gt;
        &lt;body&gt;
         hi this is java
        &lt;/body&gt;
        &lt;/html&gt;

pom.xml

    &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
    &lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema- 
    instance&quot;
    xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
    &lt;parent&gt;
    	&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    	&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
    	&lt;version&gt;2.2.6.RELEASE&lt;/version&gt;
    	&lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
    &lt;/parent&gt;
    &lt;groupId&gt;com.telusco&lt;/groupId&gt;
    &lt;artifactId&gt;respect&lt;/artifactId&gt;
    &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
    &lt;name&gt;respect&lt;/name&gt;
    &lt;description&gt;Demo project for Spring Boot&lt;/description&gt;

    &lt;properties&gt;
    	&lt;java.version&gt;1.8&lt;/java.version&gt;
    &lt;/properties&gt;

    &lt;dependencies&gt;
    	&lt;dependency&gt;
    		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    		&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
    	&lt;/dependency&gt;

	

    &lt;dependency&gt;
    		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    		&lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
    		&lt;scope&gt;test&lt;/scope&gt;
    		&lt;exclusions&gt;
    			&lt;exclusion&gt;
    				&lt;groupId&gt;org.junit.vintage&lt;/groupId&gt;
    				&lt;artifactId&gt;junit-vintage-engine&lt;/artifactId&gt;
    			&lt;/exclusion&gt;
    		&lt;/exclusions&gt;
    	&lt;/dependency&gt;
    	&lt;!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper --&gt;
    &lt;dependency&gt;

    &lt;groupId&gt;org.apache.tomcat&lt;/groupId&gt;
    &lt;artifactId&gt;tomcat-jasper&lt;/artifactId&gt;
    &lt;version&gt;9.0.33&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;/dependencies&gt;
    
    &lt;build&gt;
    	&lt;plugins&gt;
    		&lt;plugin&gt;
    			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    			&lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
    		&lt;/plugin&gt;
    	&lt;/plugins&gt;
    &lt;/build&gt;


    
Error is :
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sun May 03 23:57:58 IST 2020
There was an unexpected error (type=Not Found, status=404).
/newart.jsp




</details>


# 答案1
**得分**: 0

请在`pom.xml`中将依赖替换为:

```xml
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>org.apache.tomcat.embed</groupId>
      <artifactId>tomcat-embed-jasper</artifactId>
      <scope>provided</scope>
    </dependency>

另外,请确保你的application.properties文件包含以下内容:

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

同时,你的 JSP 文件应位于目录/src/main/webapp/WEB-INF/jsp中。在你的控制器中,只需执行return /newart,而不是return /newart.jsp

英文:

Please replace dependency

&lt;groupId&gt;org.apache.tomcat&lt;/groupId&gt;
&lt;artifactId&gt;tomcat-jasper&lt;/artifactId&gt;
&lt;version&gt;9.0.33&lt;/version&gt;
&lt;/dependency&gt;
&lt;/dependencies&gt;

in pom.xml with dependencies

    &lt;dependency&gt;
      &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
      &lt;artifactId&gt;spring-boot-starter-tomcat&lt;/artifactId&gt;
      &lt;scope&gt;provided&lt;/scope&gt;
    &lt;/dependency&gt;

    &lt;dependency&gt;
      &lt;groupId&gt;org.apache.tomcat.embed&lt;/groupId&gt;
      &lt;artifactId&gt;tomcat-embed-jasper&lt;/artifactId&gt;
      &lt;scope&gt;provided&lt;/scope&gt;
    &lt;/dependency&gt;

In addition please ensure that your application.properties contains

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

and that your jsp files are located in folder /src/main/webapp/WEB-INF/jsp.
In your controller it is sufficient to execute return /newart instead of return /newart.jsp.

huangapple
  • 本文由 发表于 2020年5月4日 02:56:49
  • 转载请务必保留本文链接:https://java.coder-hub.com/61580001.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定