简体   繁体   English

Netbeans Spring Boot Initilizr项目:无法在Web浏览器中看到jsp内容

[英]Netbeans Spring Boot Initilizr project: Unable to see jsp content in web browser

I am reading Spring in Action, 4th Edition by manning publications and Chapter 5 is about Spring Web App. 我正在阅读出版物中的Spring in Action,第4版,第5章是有关Spring Web App的。 I am trying to implement textbook example in Netbeans using Maven template Spring Boot Initilizr Project. 我正在尝试使用Maven模板Spring Boot Initilizr Project在Netbeans中实现教科书示例。 (Examples by author use Gradle. I want to use Maven Project Management tool) (示例使用Gradle。我想使用Maven项目管理工具) Maven => Spring Boot Initilizr项目

However when I am trying to see content in browser, I am getting "Whitelabel Error Page". 但是,当我尝试在浏览器中查看内容时,出现“ Whitelabel Error Page”。 Below is my project configuration. 以下是我的项目配置。 What should I do to get past the error in Web browser and see content present in home.jsp 我应该怎么做才能克服Web浏览器中的错误并看到home.jsp中存在的内容

我的项目配置

WebConfig.java content: WebConfig.java内容:

@Configuration
@EnableWebMvc
@ComponentScan("spitter.web")
public class WebConfig extends WebMvcConfigurerAdapter {

    @Bean
        public ViewResolver viewResolver() {
        InternalResourceViewResolver resolver
            = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/pages/");
        resolver.setSuffix(".jsp");
        resolver.setExposeContextBeansAsAttributes(true);
        return resolver;
    }

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

}

home.jsp content: home.jsp内容:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
    <head>
        <title>Spittr</title>
        <link rel="stylesheet"
              type="text/css"
              href="<c:url value="/resources/style.css" />" >
    </head>
    <body>
        <h1>Welcome to Spittr</h1>
        <a href="<c:url value="/spittles" />">Spittles</a> |
        <a href="<c:url value="/spitter/register" />">Register</a>
    </body>
</html>

I am able to properly test @Test scripts present in Test Packages. 我能够正确测试“测试包”中的@Test脚本。

I now realized that the issue id with how I setup the project. 现在,我意识到问题编号与我如何设置项目有关。 Book example (Spring Web MVC) is a web application and not a Spring Boot application. 书籍示例(Spring Web MVC)是一个Web应用程序,而不是Spring Boot应用程序。 Spring boot applications are standalone applications that require public static void main (String[] args) to be present. Spring Boot应用程序是独立的应用程序,需要存在public static void main (String[] args) However Spring Web MVC application which displays .jsp pages is expected to run on server. 但是,显示.jsp页的Spring Web MVC应用程序应在服务器上运行。 Hence below template of Netbeans is more relevant 因此,下面的Netbeans模板更为相关

在此处输入图片说明

Once this template is chosen, rest of tasks are simple. 选择此模板后,其余任务就很简单。 Also, the book example incorrectly coded spittr as spitter in couple of places. 另外,这本书的示例在几个地方错误地将spittr编码为spitter That needs to be corrected as well as has been mentioned in book forum - https://forums.manning.com/posts/list/38046.page;jsessionid=8A9B66613EC5B4CAB91E1897C5631970 . 这需要纠正,以及在书论坛-https: //forums.manning.com/posts/list/38046.page ; jsessionid=8A9B66613EC5B4CAB91E1897C5631970中已提到。 Once these are corrected, application should run fine. 一旦更正了这些,应用程序应该可以正常运行。 Below is my project structure 下面是我的项目结构

在此处输入图片说明

WebConfig with corrected package name is 具有正确软件包名称的WebConfig为

@Configuration
@EnableWebMvc
@ComponentScan("spittr.web")
public class WebConfig
    extends WebMvcConfigurerAdapter {

    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver resolver
                = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
            resolver.setExposeContextBeansAsAttributes(true);
            return resolver;
    }

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

RootConfig with corrected package name: 具有更正的软件包名称的RootConfig:

@Configuration
@ComponentScan(basePackages = {"spittr"},
    excludeFilters = {
        @Filter(type = FilterType.ANNOTATION, value = EnableWebMvc.class)
    })
public class RootConfig {
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM