2017-03-29 54 views
0

abc.jsp頁面。我正嘗試在表單中插入中文字體。在jsp頁面插入的特殊字符和中文字體在彈簧控制器中不起作用

<!DOCTYPE html> 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> 
<%@ page language="java" contentType="text/html; charset=utf-8" 
    pageEncoding="utf-8"%> 
<html lang="en"> 

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

<form id="bform" action="addemo" method="POST" enctype="multipart/form-data" class='form-group' accept-charset="utf-8"> 
          <div class='form-group'> 
           <label>Book Image</label> <input type='file' name="file" 
            class='btn' required="required" id="image_file" 
            onchange="return validateimage();"> <img 
            src="img/user_default_large.png" alt="" id="uploadPreview" 
            style="height: 130px; width: 21%;"> 
          </div> 
          <div class='form-group'> 
           <label>Book Title</label> <input type='text' name='title' 
            class='form-control' placeholder='Title' required="required"> 
          </div> 
          <div class='form-group'> 
           <input type='Submit' name='cat_img' value='Add' 
            class='btn btn-success'> <input type='reset' 
            name='cat_img' value='Cancel' class='btn btn-primary'> 
          </div> 
         </form> 

在上述形式時我加入中國字體「你好」和 提交表單請求轉到控制器。

@RequestMapping(value = "addemo", method = RequestMethod.POST) 
    public JSONObject view(HttpServletRequest request,@Valid Books model, BindingResult results, 
      @RequestPart(name = "file", required = false) MultipartFile image) throws ExecutionException, UnsupportedEncodingException { 
     request.setCharacterEncoding("utf-8"); 
     String username = request.getParameter("title"); 
     System.out.println("encoding: "+request.getCharacterEncoding()); 
     System.out.println("received: "+username); 
     System.out.println("received: "+username); 
     JSONObject jsonObject = new JSONObject(); 
     System.out.println(model.getTitle()); 

我得到這個我的控制檯上 編碼:UTF-8 收到:ä½A¥½ 收到:ä½A¥½ ä½A¥½ ä½A¥½

應該如何我得到了字符串參數中的utf-8編碼數據。請解釋一下 。

+0

您需要爲您的控制檯設置編碼utf-8 –

+0

窗口>首選項>常規>內容類型,將UTF-8設置爲所有內容類型的默認編碼。 窗口>首選項>常規>工作區,將「文本文件編碼」設置爲「其他:UTF-8」。 但它不工作。 @ RizwanM.Tuman –

回答

相關問題