azure应用程序服务上的spring应用程序中没有utf-8编码

9gm1akwq  于 2021-10-10  发布在  Java
关注(0)|答案(0)|浏览(196)

我在azure应用程序服务上部署的spring应用程序有问题。在azure上托管的网站上我没有波兰字符,但在本地运行完全正常。
当地成果:

东道国的结果

我尝试了多种解决方案,从更改属性文件到在MVCConfiguer中创建bean。
我尝试过的代码实现
网络配置类

@Configuration
    public class WebConfig implements WebMvcConfigurer {

        @Bean
        @Description("Thymeleaf template resolver serving HTML 5")
        public ClassLoaderTemplateResolver templateResolver() {

            ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();

            templateResolver.setPrefix("templates/");
            templateResolver.setCacheable(false);
            templateResolver.setSuffix(".html");
            templateResolver.setTemplateMode("HTML5");
            templateResolver.setCharacterEncoding("UTF-8");

            return templateResolver;
        }

        @Bean
        @Description("Thymeleaf template engine with Spring integration")
        public SpringTemplateEngine templateEngine() {

            SpringTemplateEngine templateEngine = new SpringTemplateEngine();
            templateEngine.setTemplateResolver(templateResolver());

            return templateEngine;
        }

        @Bean
        @Description("Thymeleaf view resolver")
        public ViewResolver viewResolver() {

            ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
            viewResolver.setTemplateEngine(templateEngine());
            viewResolver.setCharacterEncoding("UTF-8");

            return viewResolver;
        }

        @Override
        public void addViewControllers(ViewControllerRegistry registry) {
            registry.addViewController("/").setViewName("index");
        }
    }

控制器:

@RequestMapping(value = "/" ,produces =  "text/plain;charset=UTF-8")
    public String index(Model model) {
        model.addAttribute( "title","Co byś chciał zjeść");
        return "index.html";
    }

index.html

<!Doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">

<head>
    <title>Java Spring Application</title>
    <meta charset="utf-8">
    <link href="../static/css/site.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
</head>

<body>
<div class="position-relative overflow-hidden p-3 p-md-5 m-md-3 text-center bg-light">
        <div class="col-md-5 p-lg-5 mx-auto my-5">
            <h1 class="display-4 fw-normal" th:text="${title}"></h1>
            <p class="lead fw-normal">Poniżej wpisz nazwę dania które chcesz wyszukać</p>
            <div class="input-group rounded">
                <input type="search" class="form-control rounded" placeholder="Wyszukaj" aria-label="Search"
                       aria-describedby="search-addon" />
                <!-- <span class="input-group-text border-0" id="search-addon">-->
                    <i class="fas fa-search"></i>
                </span>
            </div>
        </div>
</div>
</body>

</html>

属性文件

web.xml文件

<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

  <display-name>Sample Spring App</display-name>
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

任何提示都会有帮助:)

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题