当前位置: 首页 > 知识库问答 >
问题:

Whitelabel错误Spring Boot-项目结构

柴坚诚
2023-03-14

我知道到目前为止,关于这个特定主题的问题已经被问了很多次,但我找不到一个确切的解决方案,这真的很烦人。我正在编写一个Spring Boot应用程序,一切似乎都很好,但它没有提供我描述的jsp页面。我认为这与我的班级结构有关,我做过研究,但无法解决。请看一看。谢谢

这是我得到的结果

在此处输入图像描述

我想得到特定的jsp页面,这是我通常应该做的

这是我的类结构,请在此处输入图像描述

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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>CRM</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>CRM</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</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/htmlunit-driver -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>htmlunit-driver</artifactId>
        </dependency>

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


        <!-- 
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
         -->
        <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

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

        <!-- https://mvnrepository.com/artifact/org.apache.tomcat.embed/tomcat-embed-jasper -->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>   

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <!-- <scope>compileOnly</scope> -->
        </dependency>

        <!-- https://mvnrepository.com/artifact/oracle/ojdbc6 -->
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-hikaricp -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-hikaricp</artifactId>
        </dependency>

    </dependencies>

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

</project>

主应用类

package com.example.CRM;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan("com.example.CRM.entity")
@EntityScan(basePackages="com.example.CRM")
@SpringBootApplication
public class CrmApplication extends SpringBootServletInitializer  {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(CrmApplication.class);
    }  

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

}

配置:

package com.example.CRM;

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;

@Configuration
@EnableWebMvc
public class CrmApplicationConfig extends WebMvcConfigurerAdapter {

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

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

}

控制器:

package com.example.CRM.controllers;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.example.CRM.dao.CRMStaff;
import com.example.CRM.entity.CRM;

@Controller
@RequestMapping("/")
public class CRMController {

    @GetMapping
    public String getHome()
    {
        return "home";
    }

    @Autowired
    private CRMStaff crmStaff;

    @GetMapping("/staff")
    public String getCrmStaff(Model theModel)
    {
        List<CRM> list = crmStaff.getStaffMembers();
        theModel.addAttribute("staff", list);

        return "list";
    }

}

道:

package com.example.CRM.dao;

import java.util.List;

import com.example.CRM.entity.CRM;

public interface CRMStaff {

    public List<CRM> getStaffMembers();

}

DAO实施

package com.example.CRM.dao;

import java.util.List;

import javax.transaction.Transactional;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import com.example.CRM.entity.CRM;

@Component
@Repository
public class CRMStaffImpl implements CRMStaff {

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    @Transactional
    public List<CRM> getStaffMembers() {

        Session currentSession = sessionFactory.getCurrentSession();
        Query<CRM> theQuery = currentSession.createQuery("from CRM", CRM.class);
        List<CRM> list = theQuery.getResultList();

        return list;
    }

}

实体:

package com.example.CRM.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name="CRMMembers")
public class CRM {

    @Id
    //@GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="Member_id")
    private int id;

    @Column(name="firstName")
    private String firstName;

    @Column(name="lastName")
    private String lastName;

    @Column(name="phoneNum")
    private String phoneNum;

    @Column(name="email")
    private String email;

    @Column(name="address")
    private String address;

}

家jsp

<!DOCTYPE html>
<html>
    <head>
        <title>CRM project | Home</title>
    </head>
    <body>
        <h1>Customer Relation Management</h1>
        <a href="${pageContext.request.contextPath }/staff">Get List of Members</a>
    </body>
</html>

list.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8"/>
        <title>CRM | Staff Members</title>
    </head>
    <body>
        <h3>CRM Staff Members List</h3>
        <hr>
        <table border="1">
            <tr>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Phone</th>
                <th>Email</th>
                <th>Address</th>
            </tr>
            <c:forEach var="temp" items="${staff }">
                <tr>
                    <td>${temp.firstName }</td>
                    <td>${temp.lastName }</td>
                    <td>${temp.phoneNum }</td>
                    <td>${temp.email }</td>
                    <td>${temp.address }</td>
                </tr>
            </c:forEach>
        </table>
    </body>
</html>

共有1个答案

江阳冰
2023-03-14

当我将这些属性添加到应用程序时,您的项目对我有效。属性。

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

下面是一个供您尝试的存储库:https://github.com/a-p-o/stackoverflow-whitelabel-error-spring-boot-project-structure

 类似资料:
  • 我是新来的春靴。当我在端口8080上启动spring-boot应用程序时,我出现了一个错误。我得到白标签错误页面。 我在/src/main/resources/templates/中创建了一个非常简单的html页面。 下面是我的pom.xml。 下面是我的应用程序的控制器

  • 我正在处理一个Spring boot项目,我使用了JSP文件并提供了Whitelabel错误页面。有一个意外的错误(type=Not ville, status=404)。 我已经将JSP放在/src/main/webapp/pages/中,并且已经用应用程序属性给出了路径。 这有几个模块,主要组件不包括Spring Boot依赖性,它只包括所需的特定模块。从其他模块继承的代码已用于Spring

  • 问题内容: 我正在尝试删除白标签错误页面,所以我所做的是为“ / error”创建了一个控制器映射, 但是现在我得到了这个错误。 不知道我做错了什么。请指教。 编辑: 已经添加 到application.properties文件中,仍然出现相同的错误 问题答案: 你需要将代码更改为以下内容: 你的代码无法正常工作,因为当你未指定的实现时,Spring Boot会自动将其注册为。 要查看该事实,请导

  • 我使用下面的dockerfile生成一个图像调用userservice 然后我使用以下命令运行容器 所以我去检查编译的字节码版本,确实是52,我不知道为什么这个错误还是报告

  • 在此处输入图像描述 WEB内容: 在此处输入图像描述 我不知道这个项目有什么问题,我发誓在我来这里问这个问题之前,我真的试图搜索结果。希望你们帮帮我,谢谢!

  • 当默认的项目结构不适用时,可以自定义配置。查看 Gradle 文档中 Java plugin 部分以了解如何在纯 Java 项目中进行配置。 Android plugin 使用了类似的语法,但因为 Android 有自己的 sourceSets,所以需要配置到 android 块中。下面的例子使用了旧的项目结构(Eclipse),并把 androidTest 的 sourceSet 映射到 tes