Whitelabel错误Spring Boot-项目结构

时间:2019-01-30 07:23:59

标签: spring spring-boot

我知道到目前为止,针对这个特定主题的问题已经问了很多遍了,但是我找不到解决我问题的确切方法,这真的很烦人。我正在编写一个Spring Boot应用程序,一切似乎都很好,但是没有提供我所描述的jsp页。我认为这与我的班级结构有关,我确实对其进行了研究,但无法解决。请看一下。谢谢。

这是我得到的结果

enter image description here

我想获得通常应该的特定jsp页面

这是我的课程结构 enter image description here

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";
    }

}

DAO:

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;

}

home.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 个答案:

答案 0 :(得分:0)

将这些属性添加到application.properties时,您的项目对我有用。

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