【问题标题】:What happens with strings in russian when I POST form当我发布表单时,俄语中的字符串会发生什么
【发布时间】:2016-09-11 07:46:41
【问题描述】:

在我的网络应用程序中有一个编辑表单,使用英语时一切正常,但是当我尝试用俄语创建新的测试记录时,我得到了类似的东西,在提交表单后:На русском тес&#1090 而不是 @987654322 @... 我的html表单:

 <form th:action="@{'/save/' + ${recipe.id}}" method="post" th:object="${recipe}">
                    <input type="hidden" id="recipeIdInput" th:field="${recipe.id}" th:value="${recipe.id}"/>
                    <input type="hidden" id="recipeFavorite" th:field="${recipeFavorite}" th:value="${recipeFavorite}"/>

                    <div th:each="favUser, stat : ${recipe.favoriteUsers}" hidden="hidden">
                        <input hidden="hidden" th:id="@{'favUser' + ${stat.index}}" th:value="${favUser.username}"/>
                    </div>

                    <p hidden="hidden" id="recipeId" th:text="${recipe.id}"/>
                    <p hidden="hidden" id="recipeUserName" th:text="${recipe.user.username}"/>

                    <div class="grid-100 row controls">
                        <div class="grid-50">
                            <h2> Recipe Editor </h2>
                        </div>
                        <div class="grid-50">
                            <div class="flush-right">
                                <button type="submit">Save Recipe</button>
                                <a href="/"><button type="button" class="secondary">Cancel</button></a>
                            </div>
                        </div>
                    </div><div class="clear"></div>

                    <div class="grid-100 row">
                        <div class="grid-20">
                            <p class="label-spacing">
                                <label> Name </label>
                            </p>
                        </div>
                        <div class="grid-40">
                            <p>
                                <input id="recipeName" type="text" th:placeholder="${recipe.name}" th:field="${recipe.name}"/>
                            </p>
                        </div>
                        <div class="grid-40">

我的控制器:

@RequestMapping(path = "/save/{id}", method = RequestMethod.POST)
public String saveRecipe(@ModelAttribute @Valid Recipe recipe, BindingResult result, @PathVariable Long id, RedirectAttributes attributes) {

    recipe.setUser(getLoggedUser());
    if (recipe.getCategory() != null)
        recipe.setCategory(categoryService.findById(recipe.getCategory().getId()));
    recipe.setFavoriteUsers(recipeService.findById(recipe.getId()).getFavoriteUsers());
    recipe.setPhoto(recipeService.findById(recipe.getId()).getPhoto());

    if (result.hasErrors()) {
        for (ObjectError o : result.getAllErrors())
            attributes.addFlashAttribute("flash",new FlashMessage(o.toString(), FlashMessage.Status.FAILURE));
        attributes.addFlashAttribute("recipe", recipe);
        attributes.addFlashAttribute("recipeFavorite", recipe.isFavorite(getLoggedUser()));
        return "redirect:/edit/"+id;
    }

    recipeService.save(recipe);
    attributes.addFlashAttribute("flash", new FlashMessage("Recipe saved.", FlashMessage.Status.SUCCESS));
    return "redirect:/";
}

那么,那些漂亮的俄语字母会发生什么以及如何解码呢? 提前致谢。

【问题讨论】:

    标签: java spring web-applications encode


    【解决方案1】:

    它的编码问题。您需要在此处使用CharacterEncodingFilter 设置UTF-8 编码。

    在 web.xml 中指定以下过滤器

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

    此过滤器的用途:- 允许指定字符编码的过滤器 要求。这很有用,因为当前的浏览器通常不会设置 即使在 HTML 页面或表单中指定了字符编码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-02
      • 1970-01-01
      • 1970-01-01
      • 2014-04-12
      • 1970-01-01
      • 1970-01-01
      • 2017-02-17
      • 2017-12-17
      相关资源
      最近更新 更多