Java Springboot/Thymeleaf:无法解析html模板上的链接

Java Springboot/Thymeleaf:无法解析html模板上的链接,java,html,spring-boot,thymeleaf,href,Java,Html,Spring Boot,Thymeleaf,Href,我继续我的Springboot/Thymeleaf项目,并解决了前面提到的一些问题() localhost:8080现在显示了基因和蛋白质的列表(见图)。在URI之后(例如),我现在可以显示一个蛋白质的条目。基因也是如此 现在,我尝试链接蛋白质和基因(在蛋白质页面上有一个链接,上面写着“基因”,点击后会显示基因html页面。目前为止只针对蛋白质页面)。执行后,我的protein.html出现以下错误: <a th:href=”gene?id= + ${geneid}”>Gene&l

我继续我的Springboot/Thymeleaf项目,并解决了前面提到的一些问题()

localhost:8080现在显示了基因和蛋白质的列表(见图)。在URI之后(例如),我现在可以显示一个蛋白质的条目。基因也是如此

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

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

 could not be parsed. 
Applicationcontroller.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";
    }
}
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";
        }
}
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";
    }
}
包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)
公共字符串删除会话(模型){
DatabaseQuery query=新建DatabaseQueryLocal();
List proteins=query.getProteins();
List genes=query.getGenes();
添加属性(“蛋白质”,蛋白质);
添加属性(“基因”,基因);
返回“索引”;
}
}
proteInControl.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";
    }
}
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";
        }
}
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";
    }
}
包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;
@控制器
公共类代理{
@RequestMapping(value=“/protein”,method=RequestMethod.GET)
公共字符串EINProteinaZeigen(模型模型,@RequestParam(“id”)字符串标识符){
DatabaseQuery query=新建DatabaseQueryLocal();
可选protein=query.getProteinByName(标识符);
if(protein.isPresent()){
Gene associatedGene=query.getGenes().stream()
.filter(g->g.getProtein().equals(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(“标识符”,“未找到具有此id的蛋白质”+标识符);
model.addAttribute(“description”,”);
model.addAttribute(“sequence”,”);
}
返回“蛋白质”;
}
}
GeneController.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";
    }
}
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";
        }
}
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";
    }
}
包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;
@控制器
公共类基因控制器{
//所有本地宿主:8080/gene landen hier,weil der value die URL”/protein“abgreift
@RequestMapping(value=“/gene”,method=RequestMethod.GET)
公共字符串einGenAnzeigen(模型模型,@RequestParam(“id”)字符串标识符){
DatabaseQuery query=新建DatabaseQueryLocal();
可选基因=query.getGeneByName(标识符);
if(gene.isPresent()){
model.addAttribute(“标识符”,gene.get().getIdentifier());
model.addAttribute(“description”,gene.get().getDesc());
model.addAttribute(“sequence”,gene.get().getSequence());
}否则{
//wenn Question kein Protein zuruckliefert eine警告Nutzer ausgeben:
model.addAttribute(“标识符”,“未找到具有此id的基因”+标识符);
//AND die anderen属性leer setzten:
model.addAttribute(“description”,”);
model.addAttribute(“sequence”,”);
}
//liefert die protein.html-Datei
返回“基因”;
}
}
index.html

<!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>

指数
基因和蛋白质

protein.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>

蛋白质



Gene.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>
!DOCTYPE html>
蛋白质



将您的
th:href
更改为以下内容:

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


这将重定向您所需的URL,您可以在
GeneController
中调用
eingenezeigen
方法。我尝试了它,得到了:出现意外错误(type=Internal Server error,status=500)。无法解析为表达式:“@{”gene?id=”(模板:“protein”-第20行,第12列)此外,IntelliJ将这一行突出显示为html的“标记未关闭”。当删除第二个“bevore>时,这是可以的,但在执行时也会导致错误。很抱歉,我在编写它之后必须立即对其进行编辑,因为我发现我放置了一个错误“括号后面。编辑过的版本现在应该可以用了。如果你还需要什么,请告诉我。谢谢。我想,我