Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.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 Springboot:Localhost:8080导致白页错误,而不是显示内容_Java_Spring Boot_Intellij Idea_Localhost_Thymeleaf - Fatal编程技术网

Java Springboot:Localhost:8080导致白页错误,而不是显示内容

Java Springboot:Localhost:8080导致白页错误,而不是显示内容,java,spring-boot,intellij-idea,localhost,thymeleaf,Java,Spring Boot,Intellij Idea,Localhost,Thymeleaf,我试图用Springboot和Thymeleaf显示一个基因列表。数据库(基因、蛋白质等)和所有方法(如获取基因标识符)都存在并发挥作用。 项目结构为: 我的类/html文件是: 主要内容: 应用程序控制器: package gui.spring.controller; import db.admin.DatabaseQuery; import db.admin.local.DatabaseQueryLocal; import db.io.FileReader; import db.samp

我试图用Springboot和Thymeleaf显示一个基因列表。数据库(基因、蛋白质等)和所有方法(如获取基因标识符)都存在并发挥作用。 项目结构为:

我的类/html文件是:

主要内容:

应用程序控制器:

package gui.spring.controller;

import db.admin.DatabaseQuery;
import db.admin.local.DatabaseQueryLocal;
import db.io.FileReader;
import db.sample.Gene;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

@SpringBootApplication
@Controller
public class ApplicationController {
    @RequestMapping(value = "/", method=RequestMethod.GET)
    public String root(Model model){
        DatabaseQuery query = new DatabaseQueryLocal();
        new FileReader(query);

        List<Gene> genes = query.getGenes();
        model.addAttribute("genes", genes);
        return "root";
    }

}
包gui.spring.controller;
导入db.admin.DatabaseQuery;
导入db.admin.local.DatabaseQueryLocal;
导入db.io.FileReader;
导入db.sample.Gene;
导入org.springframework.boot.autoconfigure.springboot应用程序;
导入org.springframework.stereotype.Controller;
导入org.springframework.ui.Model;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RequestMethod;
导入java.util.List;
@SpringBoot应用程序
@控制器
公共类应用程序控制器{
@RequestMapping(value=“/”,method=RequestMethod.GET)
公共字符串根(模型){
DatabaseQuery query=新建DatabaseQueryLocal();
新文件阅读器(查询);
List genes=query.getGenes();
添加属性(“基因”,基因);
返回“根”;
}
}
root.html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <th:block th:each="gene:${genes} ">
        <p th:text="${gene.getIdentifier()}"></p>
    </th:block>


</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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>1.0.0</modelVersion>

    <groupId>SpringBoot</groupId>
    <artifactId>biosampledb</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
    </parent>

    <dependencies>

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

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <type>jar</type>
        </dependency>

      <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
            <version>1.4.6.RELEASE</version>
      </dependency>

      <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-api</artifactId>
          <version>1.7.7</version>
      </dependency>

    </dependencies>
</project>

1.0.0
弹簧靴

UPDATE2:DatabaseQuery和DatabaseQueryLocal 这是DatabaseQuery接口(已提供)

package db.admin;
导入db.sample.Assay;
导入db.sample.Gene;
导入db.sample.Protein;
导入org.apache.commons.lang3.*;
导入java.util.List;
导入java.util.Optional;
公共接口数据库查询{
列表
IntelliJ的执行日志。当

在浏览器中调用“localhost:8080”。之后使用停止按钮手动终止进程。

模板的默认thymeleaf前缀(它是属性:spring.thymeleaf.prefix)是:classpath:/templates/


如果您将root.html移动到src/main/resources/templates,如果我没有遗漏任何其他内容,它应该可以工作。

对于staters,请停止混合Spring启动版本(2.0.3和1.4.6)。其次,您的项目设置没有遵循所需的(和建议的)要求设置。首先,你的模板目录是错误的,
db
包中的所有内容都被Spring Boot忽略了(因此,如果你在那里使用自动布线,它将不起作用)。谢谢你,我不想制造混乱。我在哪里混合它?你的百里香叶启动器来自1.4.6(只需删除版本就可以修复它)此外,这不是混淆,而是将不属于同一个版本的内容混合在一起。
<?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>1.0.0</modelVersion>

    <groupId>SpringBoot</groupId>
    <artifactId>biosampledb</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
    </parent>

    <dependencies>

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

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <type>jar</type>
        </dependency>

      <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
            <version>1.4.6.RELEASE</version>
      </dependency>

      <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-api</artifactId>
          <version>1.7.7</version>
      </dependency>

    </dependencies>
</project>
package db.admin;

import db.sample.Assay;
import db.sample.Gene;
import db.sample.Protein;
import org.apache.commons.lang3.*;

import java.util.List;
import java.util.Optional;

public interface DatabaseQuery {
    List<Assay> getAssays();

    List<Gene> getGenes();

    List<Protein> getProteins();

    void addAssay(Assay assay);

    void addGene(Gene gene);

    void addProtein(Protein protein);

    /**
     * Returns all Assays that used a Protein that is associated with the given gene.
     *
     * @param gene - gene to get the proteins
     *
     * @return list of associated assays
     */
    List<Assay> getAssaysByGene(Gene gene);

    /**
     * Returns all Proteins that have a measurement above or equal to 'meas' in any Assay
     *
     * @param meas - threshold value
     * @return A list of tuples (Pairs) where the Protein is left and the measured score from the assay is right
     */
    List<Pair<Protein, Double>> getAssayResultsByMeas(Double meas);

    /**
     * Return all Scores and assoc. Assays for a given Protein
     *
     * @param protein - protein to look up
     * @return List of Pairs of assays and results for the given protein on that assay
     */
    List<Pair<Assay, Double>> getScores(Protein protein);

    /**
     * Returns a protein by given identifier. Returns Optinal.empty if there is no protein with this name
     *
     * @param identifier - id of the protein
     * @return Optional protein
     */
    Optional<Protein> getProteinByName(String identifier);

    /**
     * Returns a gene by given identifier. Returns Optinal.empty if there is no gene with this name
     *
     * @param name - id  of a gene
     * @return Optional protein
     */
    Optional<Gene> getGeneByName(String name);

    /**
     * Returns a assay by given identifier. Returns Optinal.empty if there is no assay with this name
     *
     * @param name - id  of a gene
     * @return Optional protein
     */
    Optional<Assay> getAssayByName(String name);
}
package db.admin.local;

import db.admin.DatabaseQuery;
import db.sample.Assay;
import db.sample.Gene;
import db.sample.Protein;
import org.apache.commons.lang3.*;
import org.apache.commons.lang3.tuple.ImmutablePair;
//import org.apache.commons.lang3.tuple.ImmutablePair;
//import org.apache.commons.lang3.tuple.Pair;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class DatabaseQueryLocal implements DatabaseQuery {
    private final DatabaseLocal db;

    public DatabaseQueryLocal() {
        this.db = DatabaseLocal.getInstance();
    }

    @Override
    public List<Assay> getAssays(){
        return db.assayList;
    }

    @Override
    public List<Gene> getGenes(){
        return db.geneList;
    }

    @Override
    public List<Protein> getProteins(){
        return db.proteinList;
    }


    @Override
    public void addAssay(Assay assay){
        db.assayList.add(assay);
    }

    @Override
    public void addGene(Gene gene){
        db.geneList.add(gene);
    }

    @Override
    public void addProtein(Protein protein){
        db.proteinList.add(protein);
    }


    /**
     * Returns all Assays that used a Protein that is associated with the given gene.
     *
     * @param gene - gene to get the proteins
     *
     * @return list of associated assays
     */
    @Override
    public List<Assay> getAssaysByGene(Gene gene){
        Protein protein  = gene.getProtein();

        return this.getAssays().stream()
                        .filter(assay -> assay.getUsedProteins().contains(protein))
                        .collect(Collectors.toList());
    }

    /**
     * Returns all Proteins that have a measurement above or equal to 'meas' in any Assay
     *
     * @param meas - threshold value
     * @return A list of pairs where the Protein is left and the measured score from the assay is right
     */
    @Override
    public List<Pair<Protein, Double>> getAssayResultsByMeas(Double meas){
       List<Pair<Protein, Double>> results = new ArrayList<>();

       for(Assay assay: getAssays()){
           List<Double> measurements = assay.getMeasurements();
           List<Protein> proteins = assay.getUsedProteins();

           for (int i = 0; i < measurements.size(); i++) {
               Double m = measurements.get(i);

               if(m >= meas){
                   //results.add(new ImmutablePair<Protein,Double>(proteins.get(i), m));
                   results.add(new Pair<>(proteins.get(i), m));
               }
           }
       }

       return results;
    }


    /**
     * Return all Scores and assoc. Assays for a given Protein
     *
     * @param protein - protein to look up
     * @return List of Pairs of assays and results for the given protein on that assay
     */
    @Override
    public List<Pair<Assay, Double>> getScores(Protein protein) {
        List<Pair<Assay, Double>> results = new ArrayList<>();

        for (Assay assay : getAssays()) {
            List<Double> measurements = assay.getMeasurements();
            List<Protein> proteins = assay.getUsedProteins();

            for (int i = 0; i < proteins.size(); i++) {
                if (proteins.get(i).equals(protein)) {
                    results.add(new Pair<>(assay, measurements.get(i)));
                    //results.add(new ImmutablePair<Assay,Double>(assay, measurements.get(i)));
                    // http://www.javased.com/index.php?api=org.apache.commons.lang3.tuple.Pair
                    // https://www.programcreek.com/java-api-examples/?api=org.apache.commons.lang3.tuple.ImmutablePair
                }
            }
        }

        return results;
    }

    /**
     * Returns a protein by given identifier. Returns Optional.empty if there is no protein with this name
     *
     * @param identifier - id of the protein
     * @return Optional protein
     */
    @Override
    public Optional<Protein> getProteinByName(String identifier){
        Optional<Protein> r = this.getProteins().stream().filter(p -> p.getIdentifier().equals(identifier)).findFirst();

        if(!r.isPresent()){
            System.err.println("DBQueryLocal: could not find protein by name: " + identifier);
        }
        return r;
    }


    /**
     * Returns a gene by given identifier. Returns Optional.empty if there is no gene with this name
     *
     * @param name - id  of a gene
     * @return Optional protein
     */
    @Override
    public Optional<Gene> getGeneByName(String name){
        return this.getGenes().stream().filter(p -> p.getIdentifier().equals(name)).findFirst();
    }



    /**
     * Returns a assay by given identifier. Returns Optional.empty if there is no assay with this name
     *
     * @param name - id  of a gene
     * @return Optional protein
     */
    @Override
    public Optional<Assay> getAssayByName(String name){
        return this.getAssays().stream().filter(p -> p.getName().equals(name)).findFirst();
    }
}