Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JSP呈现在Spring 4和Tomcat 8.0.20中不起作用_Java_Spring_Jsp_Spring Mvc_Tomcat - Fatal编程技术网

Java JSP呈现在Spring 4和Tomcat 8.0.20中不起作用

Java JSP呈现在Spring 4和Tomcat 8.0.20中不起作用,java,spring,jsp,spring-mvc,tomcat,Java,Spring,Jsp,Spring Mvc,Tomcat,当我试图构建一个简单的SpringWeb应用程序示例时,我发现了一些问题。当我试图显示jsp文件时,我获得了未呈现的jsp文件,如txt文件 提前谢谢你 我使用的是Spring4和Tomcat8.0.20的最新版本。我的Java SDK和JRE是8.0.40。这是我的文件夹结构: src/main/java: demo ---DemoApplication.java demo.config ----MvcConfig.java demo.controllers ----RootControlle

当我试图构建一个简单的SpringWeb应用程序示例时,我发现了一些问题。当我试图显示jsp文件时,我获得了未呈现的jsp文件,如txt文件

提前谢谢你

我使用的是Spring4和Tomcat8.0.20的最新版本。我的Java SDK和JRE是8.0.40。这是我的文件夹结构:

src/main/java:
demo
---DemoApplication.java
demo.config
----MvcConfig.java
demo.controllers
----RootController.java


src
   main
      java
      resources
      webapp
         WEB-INF
             jsp
               test.jsp
application.properties文件中没有任何内容

现在,我将向您展示我的代码:

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>org.test</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <start-class>demo.DemoApplication</start-class>
        <tomcat.version>8.0.20</tomcat.version>
        <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>
        </dependency>

        <!-- JSP -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
MvcConfig.java

package demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);

      }
}
package demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

@EnableWebMvc
@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Bean
    public InternalResourceViewResolver getViewResolver(){
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/jsp/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
   }

}
package demo.controllers;

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

@Controller
public class RootController {

    @RequestMapping("/")
    public String home (){
        return "test";

    }

}
RootController.java

package demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);

      }
}
package demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

@EnableWebMvc
@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Bean
    public InternalResourceViewResolver getViewResolver(){
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/jsp/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
   }

}
package demo.controllers;

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

@Controller
public class RootController {

    @RequestMapping("/")
    public String home (){
        return "test";

    }

}
test.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Inferno</title>
</head>
<body>
<h1>It's working!! Oh my god! I can't believe it!</h1>

</body>
</html>

无法控制的大火
它起作用了!!哦,我的上帝!真不敢相信!

我认为您需要在pom.xml中使用以下内容:


org.apache.tomcat.embed
(在该页面上搜索JSP)

干杯,
Marcus

我改变了方法,使用了以下URL中提供的说明


我也有同样的问题,但在我的例子中,我有一个servlet映射: /* 这也将拦截对jsp的请求

因此,我删除了web.xml中的内容,一切正常

<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name> 
  <url-pattern>/</url-pattern>
  <!--   <url-pattern>/*</url-pattern> -->  <<<<
</servlet-mapping>

调度员服务
/

你好,马库斯,非常感谢你的快速回复。当我使用它时,我遇到了一个maven依赖问题:ArtifactDescriptorException:无法读取org.apache.tomcat.embed:tomcat-embed的工件描述符jasper:jar:8.0.20。连接超时这很奇怪。也许我拼错了什么的,你可以直接从这里复制链接,看看它是否有效:同样的问题。。我将测试手动下载并将其添加到我的raven存储库中。我将发布结果。好的,我将其粘贴到maven存储库中,没有错误。然而,当我启动应用程序时,我再次收到类似txt的文件。。。谢谢你的帮助!好吧我建议您检查一下:这里有一个示例项目,您可以尝试一下。:)我对web应用程序使用SpringBoot,但使用Thymeleaf而不是JSP。所以说Spring Boot只适用于独立应用程序是不正确的。Spring Boot肯定可以用于web应用程序-您已经将答案标记为正确,但实际上它并不能解决您原来的问题-您只是尝试了一种完全不同的方法。@MystyxMac我纠正了关于Spring Boot的错误,“我不知道。”福拉夫迈斯特将军对我来说,我现在想做的就是尽我所能解决它。不管怎样,我都可以获得jsp网页而不是txt文件。有时,最好找到其他方法来做一些艰苦的工作并获得相同的结果,特别是在商业环境中。无论如何,感谢您的贡献。如果您在MvcConfig中省略这两种方法,会发生什么?Spring引导应该根据在类路径上找到的内容来“配置”自己。在本例中,它可能会为您自动配置JSLTView,因为JSTL位于类路径上