gpt4 book ai didi

Java - Thymeleaf - HTML : Color letters using switch case: Unwanted paragraph/newline

转载 作者:行者123 更新时间:2023-11-30 01:58:14 25 4
gpt4 key购买 nike

我正在研究一个由基因、蛋白质和检测组成的生物数据库。我想使用 Spring Boot 和 Thymeleaf 建立一个 Web 可视化。每个基因都显示在一个页面上,其中包含名称、描述和序列。该序列由字母 A、T、G 和 C 组成。我想为这些字母着色(作品)。但是,每个字母都写在一个新行中,而不是一直写入文本直到该行已满(然后到下一行等)。在gene.html中,我在定义颜色时使用了small标签(之前尝试过p,尽管这是我的问题的原因),但使用small并没有帮助。

我希望我提供的代码片段足够了(如果不够,请告诉我你需要什么)

基因.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://thymeleaf.org">
<head>
<title>Gene</title>
<meta http-equiv="Content-Type" content="content/html; charset=UTF-8">
</head>

<body>
<!--import header-->
<header th:include="header"></header>

<div id="main">
<!--GeneID as page heading -->
<h2 th:text="'Gene: '+${identifier}"></h2>
<!--Gene description -->
<p th:text="${description}"></p>
<br/>
<!-- Sequence -->
<h3 th:text="'Sequence:'"></h3>
<!-- For each char in sequence-->
<th:block th:each="char:${sequence}">
<!--Print the char. Possibility to color encode the bases utilizing switch/case
<small th:text="${char}"></small> -->
<div th:switch="${char}">
<div th:case="'A'">
<small style="color: blue" th:text="${char}"></small>
</div>
<div th:case="'T'">
<small style="color: yellow" th:text="${char}"></small>
</div>
<div th:case="'C'">
<small style="color: forestgreen" th:text="${char}"></small>
</div>
<div th:case="'G'">
<small style="color: red" th:text="${char}"></small>
</div>
</div>

</th:block>

<br/>
<br/>
<!--Protein encoded by gene -->
<h3>Protein:</h3>
<a th:href="${'protein?id='+protein}" th:text="${protein}"></a>
</div>

</body>
</html>

GeneController.java

package gui.spring.controller;

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 org.springframework.web.bind.annotation.RequestParam;

import java.util.Optional;
import static main.Main.query;

/**
* @author Miriam Mueller
* @since 05-12-2018
* @version 1.0
* Class to handle view of one Gene. Gene name, description and sequence are shown. The encoded protein is linked.
*/
@Controller
public class GeneController {
//All calls of localhost:8080/gene get to this controller
@RequestMapping(value = "/gene", method = RequestMethod.GET)
public String einGenAnzeigen(Model model, @RequestParam(value="id") String id) {

model.addAttribute("geneSize",query.getGenes().size());
model.addAttribute("proteinSize",query.getProteins().size());
model.addAttribute("assaySize",query.getAssays().size());
Optional<Gene> gene = query.getGeneByName(id);

if(gene.isPresent()) {
// if gene exists
String description = gene.get().getDesc();
String[] arraySeq = gene.get().getSequence().split("(?!^)");
Protein protein = query.getGeneByName(id).get().getProtein();
model.addAttribute("identifier", gene.get().getIdentifier()); //GenID
model.addAttribute("sequence",arraySeq); //gene sequence
model.addAttribute("description",description); //description
model.addAttribute("protein",protein.getIdentifier()); //encoded protein
}else{
// error messages, if no gene with called id exists
model.addAttribute("gene", "There is no Gene with this ID.");
model.addAttribute("protein","There is no Gene with this ID.Therefore, no reference protein was found.");
model.addAttribute("sequence","");
model.addAttribute("description","");
}

// name of html-template
return "gene";
}
}

感谢您的时间和精力:)

最佳答案

发生的事情是 div 元素是 block元素,这意味着它们将垂直堆叠而不是水平堆叠。例如,当您遍历序列时:

    <th:block th:each="char:${sequence}">
<!--Print the char. Possibility to color encode the bases utilizing switch/case
<small th:text="${char}"></small> -->
<div th:switch="${char}">
<div th:case="'A'">
<small style="color: blue" th:text="${char}"></small>
</div>
<div th:case="'T'">
<small style="color: yellow" th:text="${char}"></small>
</div>
<div th:case="'C'">
<small style="color: forestgreen" th:text="${char}"></small>
</div>
<div th:case="'G'">
<small style="color: red" th:text="${char}"></small>
</div>
</div>

</th:block>

每个 div 都将显示在新行中。您可以通过将这些 div 上的 display 更改为 inlineinline-block

来让它们内联显示。
        <div th:case="'A'" style="display:inline-block;">
<small style="color: blue" th:text="${char}"></small>
</div>
<div th:case="'T'" style="display:inline-block;">
<small style="color: yellow" th:text="${char}"></small>
</div>
<div th:case="'C'" style="display:inline-block;">
<small style="color: forestgreen" th:text="${char}"></small>
</div>
<div th:case="'G'" style="display:inline-block;">
<small style="color: red" th:text="${char}"></small>
</div>

或使用另一个默认显示不是block的元素,例如跨度。删除 div's 也可以,因为 small 也是一个内联元素。

关于Java - Thymeleaf - HTML : Color letters using switch case: Unwanted paragraph/newline,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53693197/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com