1. 程式人生 > >SSM中jsp向後臺Controller傳值中文亂碼的奇葩解決!!!

SSM中jsp向後臺Controller傳值中文亂碼的奇葩解決!!!

場景

進行簡單的SSM整合時,jsp提交使用者名稱到後臺Controller時,插入資料時顯示中文亂碼。

然後,log4j配置輸出sql語句,看到sql語句執行插入時,值就已經是亂碼了。

關於log4j配置輸出sql語句,參照:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/85159383

說明在插入資料庫之前就已經亂碼了。

然後在Controller中打斷點,可以看到在jsp傳遞過來時就已經是亂碼了。

然後檢視jsp頁面設定

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

加了!!!

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

  加了!!

解決

在使用bootstrap的模板時,直接將其表單複製過來,添加了action屬性,沒加method屬性為post!!!!!!

<form action="addUser" method="post">
  <div class="form-group">
    <label for="name">使用者名稱</label>
    <input type="text" class="form-control" id="name" name ="name" placeholder="name">
  </div>
  <div class="form-group">
    <label for="age">年齡</label>
    <input type="number" class="form-control" id="age"  name="age" placeholder="age">
  </div>
  <button type="submit" class="btn btn-default">新增使用者</button>
</form>

將method改為post好了!!!!