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

Java-Thymeleaf-HTML:使用开关大小写的彩色字母:不需要的段落/换行符

  •  1
  • Shushiro  · 技术社区  · 7 年前

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

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

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

    感谢您的时间和努力:)

    2 回复  |  直到 7 年前
        1
  •  1
  •   StaticBeagle    7 年前

    发生的事情是 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 将显示在新行中。您可以通过更改 display 在那些部门的未来 inline inline-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 例如 span . 移除 div's 从那时起,它也会起作用 small 也是一个内联元素。

        2
  •  1
  •   pcoates    7 年前

    你可以用一种 float: left 让你的div块按照你想要的方式排列。

    我建议您换成使用外部样式表来保存样式。然后你可以在你的基因上设置不同的类别,并以这种方式设计它们。这比试图在html中管理它们要容易得多。然后你就可以摆脱它了 th:switch

    创建一个 main.css resources/static/css/

    div.gene {
        border: 1px solid #999;
        float: left;
        height: 14px;
        width: 14px;
        text-align: center;
        font-size: 12px;
    }
    
    div.A {
        color: blue;
    }
    
    div.T {
        color: yellow;
    }
    
    div.C {
        color: forestgreen;
    }
    
    div.G {
        color: red;
    }
    

    将以下内容添加到 <head> 标记您的基因html,以便它可以获得css文件

    <link rel="stylesheet" href="/css/main.css" />
    

    更改gene.html以添加类

    代替

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

    具有

        <div th:class="${'gene ' + char}" th:text="${char}"/>