关于JSF页面第一次输入中文乱码,刷新后正常显示问题

问题描述

写了一个JSF页面,发现提交数据时,第一次中文乱码,之后再次提交就会正常显示,页面如下

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:c="http://java.sun.com/jsp/jstl/core">
<h:head>
   <title>JSF Spring Hibernate Integration</title>
   <style type="text/css">
      .tg {
         border-collapse: collapse;
         border-spacing: 0;
         border-color: #ccc;
      }

      .tg td {
         font-family: Arial, sans-serif;
         font-size: 14px;
         padding: 10px 5px;
         border-style: solid;
         border-width: 1px;
         overflow: hidden;
         word-break: normal;
         border-color: #ccc;
         color: #333;
         background-color: #fff;
      }

      .tg th {
         font-family: Arial, sans-serif;
         font-size: 14px;
         font-weight: normal;
         padding: 10px 5px;
         border-style: solid;
         border-width: 1px;
         overflow: hidden;
         word-break: normal;
         border-color: #ccc;
         color: #333;
         background-color: #f0f0f0;
      }

      .tg .tg-4eph {
         background-color: #f9f9f9
      }
   </style>

</h:head>
<h:body>
   <h1>Add a Person</h1>
   <h:form>
      <table>
         <tr>
            <td><label>Name</label></td>
            <td><h:inputText id="name" value="#{student.name}" converter="encodingConvert"/></td>
         </tr>
         <tr>
            <td><label>Address</label></td>
            <td><h:inputText id="address" value="#{student.address}" converter="encodingConvert"/></td>
         </tr>
         <tr>
            <td colspan="2">
               <h:commandButton action="#{studentService.insertStudent(student)}" value="添加学生" />
            </td>
         </tr>
      </table>
   </h:form>
   <br/>
   <h3>Persons List</h3>

   <c:if test="${!empty studentService.selectAllStudents()}">
      <table class="tg">
         <tr>
            <th width="80" > Student ID</th>
            <th width="120">Student Name</th>
            <th width="120">Student Address</th>
         </tr>
         <ui:repeat value="${studentService.selectAllStudents()}" var="stu">
            <tr>
               <td>${stu.id}</td>
               <td>${stu.name}</td>
               <td>${stu.address}</td>
            </tr>
         </ui:repeat>
      </table>
   </c:if>
</h:body>
</html>

加载完页面后首次提交前
在这里插入图片描述
提交后发现乱码
在这里插入图片描述

数据库也是乱码
在这里插入图片描述

再次添加,正常显示
在这里插入图片描述

解决办法

伪解决,让页面加载出来后刷新一下就好了

<script type="text/javascript">
        function fresh() {
            if (sessionStorage.getItem("first") != "1") {
                sessionStorage.setItem("first", "1");
                location.reload();
            }
        }
</script>

<!-- 给页面body添加监听事件 -->
<h:body onload="fresh()">
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

别来BUG求求了

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值