这是否可能使用Thymeleaf和Spring将UTF-8编码的HTML代码注入到HTML页面中?

huangapple go评论82阅读模式
英文:

Is this possible to inject html code in utf-8 encoding to html page using thymeleaf and spring

问题

最近,我决定尝试学习在Spring MVC中使用Thymeleaf。目前,我正在存储诸如img、iframe等HTML主体片段。问题出现在将这些HTML片段使用Thymeleaf标签和UTF-8编码以及西里尔字母符号注入到一个通用HTML页面中之后。更准确地说,这些HTML片段以Model属性的形式来自Spring控制器。是否可能将片段直接注入到像下面这样的通用HTML页面中:

<!DOCTYPE html>
<html xmlns:th="https://www.thymeleaf.org">
<head></head>
<body>
[(${contents})] <!-- contents是我想要注入的片段 -->
</body>
</html>

控制器的请求方法如下所示:

@GetMapping("/getArticle/{id}")
public String getArticle(@PathVariable Long id, Model model) throws IOException {
    Article article = articleService.getById(id);
    String contents = new String(Files.readAllBytes(Paths.get(article.getText())));
    model.addAttribute("contents", contents);
    return "news/article";
}

内容插入片段示例:

<h1>Кириллица</h1>
<br/><iframe style='width: 30%; height: 35%;' src='link'></iframe><br/>
Классный видос, да?

合并前两个列表的结果给了我这个

正如我之前提到的,我遇到了一些曲线符号而不是俄语字符。另外,我应该注意,在没有注入的情况下正常加载页面时,所有俄语符号都显示正确。这是我的Spring配置中的ViewResolver和TemplateResolver:

@Bean
public SpringResourceTemplateResolver templateResolver() {
    SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
    templateResolver.setApplicationContext(applicationContext);
    templateResolver.setPrefix("/WEB-INF/views/");
    templateResolver.setSuffix(".html");
    templateResolver.setCharacterEncoding("UTF-8");
    templateResolver.setTemplateMode(TemplateMode.HTML);
    templateResolver.setCacheable(true);
    return templateResolver;
}

@Bean
public ThymeleafViewResolver viewResolver() {
    ThymeleafViewResolver thymeleafViewResolver = new ThymeleafViewResolver();
    thymeleafViewResolver.setCharacterEncoding("UTF-8");
    thymeleafViewResolver.setContentType("text/html; charset=UTF-8");
    return thymeleafViewResolver;
}

所以,问题是:是否可能使用正确的UTF-8编码使用Thymeleaf标签直接注入从控制器接收的HTML代码片段,而不会出现曲线符号?

英文:

Recently I've decided to try to learn Thymeleaf with Spring MVC. And currently I'm storing html body fragments such as img, iframe etc. The problem has appeared after injecting this html fragments into one general html page using thymeleaf tags with UTF-8 encoding and cyrillic symbols. To be more precisely, these html fragments come in Model attribute from Spring controller. Is it possible to inject just the fragment into the one general html page such as below:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;html xmlns:th=&quot;https://www.thymeleaf.org&quot;&gt;
&lt;head&gt;&lt;/head&gt;
&lt;body&gt;
[(${contents})] &lt;!-- contents is that fragment I want to inject --&gt;
&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

Controller's request method is shown below:

    @GetMapping(&quot;/getArticle/{id}&quot;)
public String getArticle(@PathVariable Long id, Model model) throws IOException {
    Article article = articleService.getById(id);
    String contents = new String(Files.readAllBytes(Paths.get(article.getText())));
    model.addAttribute(&quot;contents&quot;, contents);
    return &quot;news//article&quot;;
}

Content insert fragment example:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;h1&gt;Кириллица&lt;/h1&gt;
&lt;br/&gt;&lt;iframe style=&#39;width: 30%; height: 35%;&#39; src=&#39;link&#39;&gt;&lt;/iframe&gt;&lt;br/&gt;
Классный видос, да?

<!-- end snippet -->

The result of combining 2 previous lists gives me this

As I've previously mentioned, I've faced the trouble I have some curve symbols instead of russian ones. Also, I should note that, in case of normal page loading without injection, all the russian symbols are shown correctly. There's my ViewResolver and TemplateResolver from Spring configuration:

@Bean
public SpringResourceTemplateResolver templateResolver() {
    SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
    templateResolver.setApplicationContext(applicationContext);
    templateResolver.setPrefix(&quot;/WEB-INF/views/&quot;);
    templateResolver.setSuffix(&quot;.html&quot;);
    templateResolver.setCharacterEncoding(&quot;UTF-8&quot;);
    templateResolver.setTemplateMode(TemplateMode.HTML);
    templateResolver.setCacheable(true);
    return templateResolver;
}

@Bean
public ThymeleafViewResolver viewResolver() {
    ThymeleafViewResolver thymeleafViewResolver = new ThymeleafViewResolver();
    thymeleafViewResolver.setCharacterEncoding(&quot;UTF-8&quot;);
    thymeleafViewResolver.setContentType(&quot;text/html; charset=UTF-8&quot;);
    return thymeleafViewResolver;
}

So, the question is next: is this possible to directly inject html code which is received from the controller as an attribute using thymeleaf tags with correct UTF-8 encoding without curve symbols?

答案1

得分: 2

你可以这样做,但至少需要将这一行进行更改:

String contents = new String(Files.readAllBytes(Paths.get(article.getText())));

改为这样:

String contents = Files.readString(Paths.get(article.getText()), StandardCharsets.UTF_8);

这里使用了带有java.nio.charset.StandardCharsets.UTF_8字符集的readString()方法。否则,readAllBytes() 方法读取的字节将以可能是其他(非UTF-8)编码的形式写入字符串中,可能取决于JVM在启动时选择的默认字符集。

然而,对于学习Thymeleaf,我建议你查阅Thymeleaf的fragments(片段)(如果你还没有这样做的话),因为这些是专为此目的设计的。

这将引导你进入template layouts(模板布局),具有很大的灵活性。

英文:

You can do this, but you would at least need to change this line:

String contents = new String(Files.readAllBytes(Paths.get(article.getText())));

to this:

String contents = Files.readString(Paths.get(article.getText()), StandardCharsets.UTF_8);

This uses the readString() method with a java.nio.charset.StandardCharsets.UTF_8 character set. Otherwise, your bytes from readAllBytes() are being written to a string in what could be some other (non-UTF-8) encoding - probably depending on the default character set picked up by the JVM when it started.

However, for learning Thymeleaf, I would recommend looking into Thymeleaf fragments (if you have not already done so), since these are designed for this purpose.

This will then lead you into template layouts, which have a lot of flexibility.

huangapple
  • 本文由 发表于 2020年9月9日 00:29:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63798024.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定