代码之家  ›  专栏  ›  技术社区  ›  Shushiro

springboot/thymeleaf:无法分析HTML模板上的链接

  •  0
  • Shushiro  · 技术社区  · 6 年前

    我继续我的springboot/thymeleaf项目并解决了前面提到的一些问题( springboot:localhost:8080 results in whitepage error instead of showing content )。

    本地宿主:8080现在显示了基因和蛋白质的列表(见图)。遵循uri http://localhost:8080/protein?id=q6gzx3(例如)我现在可以显示一种蛋白质的条目。基因也是如此。

    现在,我尝试链接蛋白质和基因(在蛋白质页面上有一个链接,上面写着“基因”,点击后显示基因html页面)。仅限于蛋白质页)。执行时,我的蛋白质.html出现以下错误:

    <a th:href=“gene”?id=+$gene id“>基因</a>
    
    无法分析。
    < /代码> 
    
    

    为了进一步理解/如有必要: 在解决protein.html-page的链接错误之后, 我的最终目标是:

    使通过index.html显示的列表元素可单击,单击后显示基因/蛋白质的详细信息。此外,在每个基因位点上都有一个按钮,它会将你重定向到相应的蛋白质,反之亦然。

    背景信息:

    迄今为止我的代码: POM.XML

    <?xml version=“1.0”encoding=“utf-8”?gt;
    <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>com.mmueller</groupid>
    <artifactid>Springboot_v2</artifactid>
    <version>1.0-快照</version>
    
    父母亲;
    <groupid>org.springframework.boot</groupid>
    <artifactid>Spring引导启动程序父级</artifactid>
    <version>2.0.3.版本</version>
    &家长/家长;
    
    <相关性>
    <相关性>
    <groupid>org.springframework.boot</groupid>
    <artifactid>Spring Boot Starter Web</artifactid>
    </dependency>
    
    <相关性>
    <groupid>org.apache.commons</groupid>
    <artifactid>通用语言3</artifactid>
    <type>JAR</type>
    </dependency>
    
    <相关性>
    <groupid>组织.slf4j</groupid>
    <artifactid>slf4j api</artifactid>
    </dependency>
    
    <相关性>
    <groupid>org.springframework.boot</groupid>
    <artifactid>春季启动启动程序thymeleaf</artifactid>
    </dependency>
    
    <相关性>
    <groupid>org.springframework.boot</groupid>
    <artifactid>Spring Boot Starter Web</artifactid>
    </dependency>
    
    </dependencies>
    
    <属性>
    Java.Valp& Gt;1.8 & lt;/java.版本& gt;
    </properties>
    
    
    建立& GT;
    和插件;
    &插件;
    <groupid>org.springframework.boot</groupid>
    <artifactid>Spring引导Maven插件</artifactid>
    & Lt//Plutin & Gt;
    </plugins>
    &建造/建造;
    
    </project>
    < /代码> 
    
    

    Maul.Java

    package main;
    
    导入db.admin.databasequery;
    导入db.admin.local.databasequerylocal;
    导入db.io.filereader;
    导入org.springframework.boot.springapplication;
    导入org.springframework.boot.autoconfigure.enableautoconfiguration;
    导入org.springframework.boot.autoconfigure.springbootsapplication;
    //导入org.springframework.boot.builder.springapplicationbuilder;
    导入org.springframework.context.annotation.componentscan;
    
    /**
    *主类
    *@作者Miriam Mueller
    */
    //@配置
    @Springboot应用程序
    @componentscan(basepackages=“gui.spring”)//sucht alle@controller in gui.spring
    @启用自动配置
    公务舱主{
    
    公共静态void main(string[]args){
    
    
    databasequery query=new databasequerylocal();
    新文件阅读器(查询);
    
    spring应用程序.run(main.class,args);
    
    }
    }
    < /代码> 
    
    

    应用程序控制器

    package gui.spring.controller;
    
    导入db.admin.databasequery;
    导入db.admin.local.databasequerylocal;
    导入db.sample.gene;
    导入db.sample.protein;
    导入org.springframework.stereotype.controller;
    导入org.springframework.ui.model;
    导入org.springframework.web.bind.annotation.requestmapping;
    导入org.springframework.web.bind.annotation.requestmethod;
    导入java.util.list;
    
    @控制器
    公共类应用程序控制器{
    @requestmapping(value=“/”,method=requestmethod.get)
    
    公共字符串DeleteSession(模型模型){
    databasequery query=new databasequerylocal();
    list<protein>proteins=query.getproteins();
    list<gene>genes=query.getgenes();
    
    model.addattribute(“蛋白质”,蛋白质);
    model.addattribute(“基因”,基因);
    返回“索引”;
    }
    }
    < /代码> 
    
    

    蛋白调控蛋白:Java

    package gui.spring.controller;
    
    导入db.admin.databasequery;
    导入db.admin.local.databasequerylocal;
    导入db.sample.protein;
    导入org.springframework.stereotype.controller;
    导入org.springframework.ui.model;
    导入org.springframework.web.bind.annotation.requestmapping;
    导入org.springframework.web.bind.annotation.requestmethod;
    导入org.springframework.web.bind.annotation.requestparam;
    导入java.util.optional;
    
    @控制器
    公共类蛋白控制器{
    
    @请求映射(value=“/protein”,method=requestmethod.get)
    公共字符串einProtenzeigen(模型,@requestparam(“id”)字符串标识符){
    
    databasequery query=new databasequerylocal();
    可选<蛋白质>蛋白质=query.getProteinByName(标识符);
    
    如果(protein.ispresent()){
    gene associatedgene=query.getgenes().stream()。
    .filter(g->g.get protein()。等于(protein.get())
    .findfirst().get();
    
    model.addattribute(“标识符”,protein.get().getIdentifier());
    model.addattribute(“description”,protein.get().getdesc());
    model.addattribute(“sequence”,protein.get().getSequence());
    model.addattribute(“geneid”,associatedgene.getIdentifier());
    
    }否则{
    model.addattribute(“identifier”,“找不到具有此id的蛋白质”+identifier);
    
    model.addattribute(“说明”,“);
    model.addattribute(“序列”,“);
    }
    返回“蛋白质”;
    }
    }
    < /代码> 
    
    < P> GeNeNo.
    
    
    package gui.spring.controller;
    
    导入db.admin.databasequery;
    导入db.admin.local.databasequerylocal;
    导入db.sample.gene;
    导入org.springframework.stereotype.controller;
    导入org.springframework.ui.model;
    导入org.springframework.web.bind.annotation.requestmapping;
    导入org.springframework.web.bind.annotation.requestmethod;
    导入org.springframework.web.bind.annotation.requestparam;
    
    导入java.util.optional;
    
    @控制器
    公共类genecontroller{
    //alle aufrafe der seite localhost:8080/gene landen hier,weil der value die url“/蛋白质”abgreift
    @请求映射(value=“/gene”,method=requestmethod.get)
    公共字符串einganzeigen(模型,@requestparam(“id”)字符串标识符){
    
    databasequery query=new databasequerylocal();
    可选<gene>gene=query.getgenebyname(标识符);
    
    如果(gene.ispresent()){
    model.addattribute(“标识符”,gene.get().getIdentifier());
    model.addattribute(“description”,gene.get().getdesc());
    model.addattribute(“sequence”,gene.get().getSequence());
    }否则{
    //wenn查询kein protein zuruckliefert eine warnung an den nutzer ausgeben:
    model.addattribute(“标识符”,“找不到具有此ID的基因”+标识符);
    //und die anderen属性leer setzten:
    model.addattribute(“说明”,“);
    model.addattribute(“序列”,“);
    }
    
    //liefert die protein.html-datei
    返回“基因”;
    }
    }
    < /代码> 
    
    

    索引.html

    <!文档类型HTML>
    <html lang=“en”xmlns:th=“http://thymeleaf.org”>
    &头;
    <title>索引</title>
    <meta http equiv=“content type”content=“content/html;charset=utf-8”/>gt;
    和/头& GT;
    和身体;
    
    &!--<A th:href=“@/蛋白质”>“蛋白质”</A>-->
    
    <h2>基因和蛋白质</h2>
    
    <table align=“left”border=“1”cellspacing=“5”width=“80%”>
    <td style=“width:50%;”>
    <th:block th:each=“基因:$基因”>
    <p th:text=“$gene.getIdentifier()”></p>
    </th:block>
    &T/GT;
    <td style=“width:50%;”>
    <th:block th:each=“蛋白质:$蛋白质”>
    <p th:text=“$蛋白质.getIdentifier()”></p>
    </th:block>
    &T/GT;
    &表;
    和/身体;
    & lt//html & gt;
    < /代码> 
    
    

    蛋白质.html

    <!文档类型HTML>
    <html lang=“en”xmlns:th=“http://thymeleaf.org”>
    &头;
    <title>蛋白质</title>
    <meta http equiv=“content type”content=“content/html;charset=utf-8”>
    和/头& GT;
    和身体;
    <h2 th:text=“$标识符”></h2>
    
    & BR/>
    <P th:text=“$说明”></P>
    
    & BR/>
    <p th:text=“$序列”style=“width:400px;换行:换行符”></p>
    & BR/>
    
    <A th:href=“gene”什么?id=+$gene id“>基因</a>
    
    和/身体;
    & lt//html & gt;
    < /代码> 
    
    

    基因.html

    <预> <代码>!文档类型HTML> <html lang=“en”xmlns:th=“http://thymeleaf.org”> &头; <title>蛋白质</title> <meta http equiv=“content type”content=“content/html;charset=utf-8”> 和/头& GT; 和身体; <h2 th:text=“$标识符”></h2> & BR/> <P th:text=“$说明”></P> & BR/> <p th:text=“$序列”style=“width:400px;换行:换行符”></p> 和/身体; & lt//html & gt; < /代码> <)

    本地宿主:8080现在显示了基因和蛋白质的列表(见图)。在URI后面http://localhost:8080/protein?id=Q6GZX3(例如)我现在可以显示一种蛋白质的条目。基因也是如此。

    现在,我尝试链接蛋白质和基因(在蛋白质页面上有一个链接,上面写着“基因”,点击后显示基因html页面)。仅限于蛋白质页)。执行后,我的protein.html出现以下错误:

     <a th:href=”gene?id= + ${geneid}”>Gene</a>
    
     could not be parsed. 
    

    为了进一步理解/如有必要: 在解决protein.html-page的链接错误之后, 我的最终目标是:

    使通过index.html显示的列表元素可单击,单击后显示基因/蛋白质的详细信息。此外,在每个基因位点上都有一个按钮,它将你导向相应的蛋白质,反之亦然。

    背景信息:

    List of genes and proteins:

    我的代码到目前为止: 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>com.MMueller</groupId>
        <artifactId>SpringBoot_v2</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <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.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-thymeleaf</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
        </dependencies>
    
        <properties>
            <java.version>1.8</java.version>
        </properties>
    
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    

    主类

    package main;
    
    import db.admin.DatabaseQuery;
    import db.admin.local.DatabaseQueryLocal;
    import db.io.FileReader;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    //import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.context.annotation.ComponentScan;
    
    /**
     * Main class
     * @author Miriam Mueller
     */
    //@Configuration
    @SpringBootApplication
    @ComponentScan(basePackages = "gui.spring")// sucht alle @Controller in gui.spring
    @EnableAutoConfiguration
    public class Main {
    
        public static void main(String[] args) {
    
    
            DatabaseQuery query = new DatabaseQueryLocal();
            new FileReader(query); 
    
            SpringApplication.run(Main.class, args);
    
        }
    }
    

    应用程序控制器.java

    package gui.spring.controller;
    
    import db.admin.DatabaseQuery;
    import db.admin.local.DatabaseQueryLocal;
    import db.sample.Gene;
    import db.sample.Protein;
    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;
    
    @Controller
    public class ApplicationController {
        @RequestMapping(value = "/", method=RequestMethod.GET)
    
        public String deleteSession(Model model){
            DatabaseQuery query = new DatabaseQueryLocal();
            List<Protein> proteins = query.getProteins();
            List<Gene> genes= query.getGenes();
    
            model.addAttribute("proteins", proteins);
            model.addAttribute("genes", genes);
            return "index";
        }
    }
    

    蛋白质控制器.java

    package gui.spring.controller;
    
    import db.admin.DatabaseQuery;
    import db.admin.local.DatabaseQueryLocal;
    import db.sample.Protein;
    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 org.springframework.web.bind.annotation.RequestParam;
    import java.util.Optional;
    
    @Controller
    public class ProteinController {
    
        @RequestMapping(value = "/protein", method = RequestMethod.GET)
        public String einProteinAnzeigen(Model model, @RequestParam("id") String identifier) {
    
            DatabaseQuery query = new DatabaseQueryLocal();
            Optional<Protein> protein = query.getProteinByName(identifier);
    
            if(protein.isPresent()) {
    Gene associatedGene = query.getGenes().stream()
            .filter(g -> g.getProtein().equals(protein.get()))
            .findFirst().get();
    
                model.addAttribute("identifier", protein.get().getIdentifier());
                model.addAttribute("description", protein.get().getDesc());
                model.addAttribute("sequence", protein.get().getSequence());
        model.addAttribute("geneid", associatedGene.getIdentifier());
    
                } else {
                 model.addAttribute("identifier", "No Protein found with this id " + identifier);
    
                 model.addAttribute("description", "");
                 model.addAttribute("sequence", "");
                 }
                return "protein";
            }
    }
    

    通用控制器.java

    package gui.spring.controller;
    
    import db.admin.DatabaseQuery;
    import db.admin.local.DatabaseQueryLocal;
    import db.sample.Gene;
    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 org.springframework.web.bind.annotation.RequestParam;
    
    import java.util.Optional;
    
    @Controller
    public class GeneController {
        //Alle Aufrufe der Seite localhost:8080/gene landen hier, weil der value die URL "/protein" abgreift
        @RequestMapping(value = "/gene", method = RequestMethod.GET)
        public String einGenAnzeigen(Model model, @RequestParam("id") String identifier) {
    
            DatabaseQuery query = new DatabaseQueryLocal();
            Optional<Gene> gene = query.getGeneByName(identifier);
    
            if(gene.isPresent()) {
                model.addAttribute("identifier", gene.get().getIdentifier());
                model.addAttribute("description", gene.get().getDesc());
                model.addAttribute("sequence", gene.get().getSequence());
            } else {
                //wenn query kein Protein zuruckliefert eine Warnung an den Nutzer ausgeben:
                model.addAttribute("identifier", "No Gene found with this id " + identifier);
                //und die anderen Attribute leer setzten:
                model.addAttribute("description", "");
                model.addAttribute("sequence", "");
            }
    
            //liefert die protein.html-Datei
            return "gene";
        }
    }
    

    索引文件

    <!DOCTYPE html>
    <html lang="en" xmlns:th="http://thymeleaf.org">
    <head>
        <title>Index</title>
        <meta http-equiv="Content-Type" content="content/html; charset=UTF-8"/>
    </head>
    <body>
    
        <!--<a th:href="@{/protein}">"Proteins"</a> -->
    
        <h2>Genes and Proteins</h2>
    
        <table align="left" border="1" cellspacing="5" width="80%">
            <td style="width:50%;">
                <th:block th:each="gene:${genes}">
                    <p th:text="${gene.getIdentifier()}"></p>
                </th:block>
            </td>
            <td style="width: 50%;">
                <th:block th:each="protein:${proteins}">
                    <p th:text="${protein.getIdentifier()}"></p>
                </th:block>
            </td>
        </table>
    </body>
    </html>
    

    蛋白质

    <!DOCTYPE html>
    <html lang="en" xmlns:th="http://thymeleaf.org">
    <head>
        <title>Protein</title>
        <meta http-equiv="Content-Type" content="content/html; charset=UTF-8">
    </head>
    <body>
            <h2 th:text="${identifier}"></h2>
    
            <br/>
            <p th:text="${description}"></p>
    
            <br/>
            <p th:text="${sequence}" style="width: 400px; word-wrap: break-word"></p>
    <br/>
    
    <a th:href="gene?id= + ${geneid}">Gene</a>
    
    </body>
    </html>
    

    HTML

    !DOCTYPE html>
    <html lang="en" xmlns:th="http://thymeleaf.org">
    <head>
        <title>Protein</title>
        <meta http-equiv="Content-Type" content="content/html; charset=UTF-8">
    </head>
    <body>
    
        <h2 th:text="${identifier}"></h2>
    
        <br/>
        <p th:text="${description}"></p>
    
        <br/>
        <p th:text="${sequence}" style="width: 400px; word-wrap: break-word"></p>
    
    </body>
    </html>
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Alain Cruz    6 年前

    改变你 th:href 对于以下项目:

    <a th:href="@{'gene?id=' + ${geneid}}">Gene</a>
    

    这将重定向您所需的URL,您调用 einGenAnzeigen 你的方法 GeneController .