如何在SPRING MVC POJO的字段中保留日语字符 [英] How to hold japanese characters in SPRING MVC POJO's field

查看:96
本文介绍了如何在SPRING MVC POJO的字段中保留日语字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用构建Web应用程序

  1. Spring MVC
  2. Spring Security
  3. Hibenate
  4. MySQl

我想在我的应用程序中添加国际化支持.我想使用Hibernate将日语字符存储并检索到mySQL db.

我已将数据库字符集设置为 UTF-8 ,并且还将属性添加到了 hibernate-cfg.xml

属性名称="hibernate.connection.characterEncoding"> UTF-8

我已经完成了简单的POC操作,其中可以从java文件中声明一些带有日语字符的字符串变量,并将它们成功插入DB中,并且通过日语字符进行搜索也可以正常工作.

但是当我在JSP文件上填写表格时,所有字段值都以POJO的形式传递给控制器​​,并且POJO字段中的所有日文字符都被自动转换为数字字符引用,如下面的示例所述,

CreateUser.jsp

<form:form method="post" commandName="userModel">
    <%@include file="/tp/web/iBusinessException.jsp"%>
    <table width="700" border="0" align="center" cellpadding="4"
        cellspacing="0">
        <tr>
            <td class="tdWscHeading">
                <img
                    src="<%=request.getContextPath()%>/tp/web/console/include/images/iconCreateuser.gif"
                    alt="Task Summary" width="20" height="20" align="absmiddle">
                <spring:message code='label.createuser'></spring:message>
            </td>
        </tr>
    </table>
    <table width="700" border="0" align="center" cellpadding="4"
        cellspacing="1" class="tableWscmain">
        <tr>
            <td width="250" class="tdWscContent fontBold">
                <spring:message code='label.firstname'></spring:message>
                <span class="fontRed"> *</span>
            </td>
            <td class="tdWscContent">
                <form:input path="firstname" cssClass="formINPUT"
                    autocomplete="off" />
                <!-- Error Message if Key is Empty Starts -->

                <span class="error"> <spring:bind path="firstname">
                        <c:if test="${not empty status.errorMessage}">
                            <c:out value="${status.errorMessage}" escapeXml="false" />
                        </c:if>
                    </spring:bind> </span>
                <!-- Error Message if key is Empty Ends -->
            </td>
        </tr>
        <tr>
            <td class="tdWscContent fontBold">
                <spring:message code='label.lastname'></spring:message>
                <span class="fontRed"> *</span>
            </td>
            <td class="tdWscContent">
                <form:input path="lastname" cssClass="formINPUT"
                    autocomplete="off" />
                <!-- Error Message if Key is Empty Starts -->
                <span class="error"> <spring:bind path="lastname">
                        <c:if test="${not empty status.errorMessage}">
                            <c:out value="${status.errorMessage}" escapeXml="false" />
                        </c:if>
                    </spring:bind> </span>
                <!-- Error Message if key is Empty Ends -->
            </td>
        </tr>
        <tr>
            <td class="tdWscContent fontBold">
                <spring:message code='label.username'></spring:message>
                <span class="fontRed"> *</span>
            </td>
            <td class="tdWscContent">
                <form:input path="username" cssClass="formINPUT"
                    autocomplete="off" />
                <!-- Error Message if Key is Empty Starts -->
                <span class="error"> <spring:bind path="username">
                        <c:if test="${not empty status.errorMessage}">
                            <c:out value="${status.errorMessage}" escapeXml="false" />
                        </c:if>
                    </spring:bind> </span>


                <!-- Error Message if key is Empty Ends -->
            </td>
        </tr>
        <tr>
            <td class="tdWscContent fontBold">
                <spring:message code='label.password'></spring:message>
                <span class="fontRed"> *</span>
            </td>
            <td class="tdWscContent">
                <form:password path="password" cssClass="formINPUT"
                    autocomplete="off" showPassword="false" />
                <!-- Error Message if Key is Empty Starts -->
                <span class="error"> <spring:bind path="password">
                        <c:if test="${not empty status.errorMessage}">
                            <c:out value="${status.errorMessage}" escapeXml="false" />
                        </c:if>
                    </spring:bind> </span>
                <!-- Error Message if key is Empty Ends -->
            </td>
        </tr>
        <tr>
            <td class="tdWscContent fontBold">
                <spring:message code='label.email'></spring:message>
                <span class="fontRed"> *</span>
            </td>
            <td class="tdWscContent">
                <form:input path="email" cssClass="formINPUT"
                    autocomplete="off" />
                <!-- Error Message if Key is Empty Starts -->
                <span class="error"> <spring:bind path="email">
                        <c:if test="${not empty status.errorMessage}">
                            <c:out value="${status.errorMessage}" escapeXml="false" />
                        </c:if>
                    </spring:bind> </span>
                <!-- Error Message if key is Empty Ends -->
            </td>
        </tr>

        <tr>
            <td class="tdWscContent fontBold">
                <spring:message code='label.groupname'></spring:message>
                <span class="fontRed"> *</span>
            </td>
            <td class="tdWscContent">
                <select id="groupname" name="groupname">
                    <c:forEach items="${GROUPS_LIST_RESULT}" var="option">
                        <option value='<c:out value="${option.groupname}" />'>
                            <c:out value="${option.groupname}" />
                        </option>
                    </c:forEach>
                </select>
                <!-- Error Message if Key is Empty Starts -->
                <span class="error"> <form:errors path="groupname"></form:errors>
                </span>
                <!-- Error Message if key is Empty Ends -->
            </td>
        </tr>

        <tr align="center">
            <td colspan="2" class="tdWscContent fontBold">
                <input type="submit" name=""
                    value="<spring:message code="label.createuser"></spring:message>">
            </td>
        </tr>
    </table>
</form:form>

MyController

@RequestMapping(value = USERADMINISTRATION_NAMESPACE + "/createUser.do", method = RequestMethod.POST)
public ModelAndView submitCreateUserPage(
        @ModelAttribute("userModel") User user, BindingResult result

) throws UserAlreadyExistsException {
    String password = "";
    password = user.getPassword();

/* Here , password i am getting like **&#164;** ,  */
/* I want exactly same Japanese characters as entered by user */
}

我在应用程序中未使用任何字符编码过滤器.

从jsp到controller,它会自动转换为上述数字. 我想要与用户在jsp中输入的日语字符完全相同,并希望将其插入数据库并显示在页面上.

我正在将所有用户输入字段的值从JSP作为POJO传递给控制器​​.

请帮助...

解决方案

最后,我在基于Spring MVC的应用程序中成功实现了国际化支持.

我已按照以下步骤使用Spring MVC,Hibernate,MYSQL或Oracle数据库以及Jboss或webLogic作为应用程序服务器将国际化支持合并到我的Web应用程序中.

假设我们要添加对日语的国际化支持,即用户应该能够在Web表单中输入日语字符,并且应该以与用户输入的格式相同的格式保存,并且还应该在网页上以相同的语言显示

请按照以下步骤操作.

  1. 确保在操作系统中安装了日语支持(特定于区域的安装).如果没有,请安装它.

  2. 如果使用任何IDE,请通过将文本编码更改为UTF-8 来配置IDE以支持日语.例如,如果您使用的是Eclipse,则将文本文件编码更改为UTF-8.您可以通过此路径(Window-> Preference-> General-> Workspace)

  3. 进行更改
  4. 将Spring框架的内置字符编码过滤器放置为过滤器链(web.xml)中的第一个过滤器,以确保其在请求处理期间首先运行,在响应处理期间最后运行

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>
    <!-- set forceEncoding to true if you want to override encoding of servlet -->
    <param-name>forceEncoding</param-name>
    <param-value>true</param-value> 
</init-param>

<filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

4通过在JSP顶部添加以下提到的代码,将JSP页面编码设置为UTF-8.

<%@ page language="java" pageEncoding="UTF-8" contentType="text/html;charset=UTF-8"%>

5通过在hibernate-cfg.xml中添加以下属性,将Hibernate连接编码方案设置为UTF-8

<property name="hibernate.connection.characterEncoding">UTF-8</property>

注意:如果使用JBoss应用程序服务器,请确保已将 characterEncoding = UTF-8 附加到数据库服务配置文件中的连接URL (用于mySQL数据库的mysql-ds.xml,例如).

<datasources>

<local-tx-datasource>

    <jndi-name>WSCDS</jndi-name>

   <connection-url>
   jdbc:mysql://{ipaddress}:{port}/{database_name}?characterEncoding=UTF-8
   </connection-url>

<driver-class>com.mysql.jdbc.Driver</driver-class>

<user-name>{username}</user-name>

<password>{password}</password>  

<metadata>

   <type-mapping>mySQL</type-mapping>

</metadata>

I am building web application using

  1. Spring MVC
  2. Spring Security
  3. Hibenate
  4. MySQl

I want to add internationalization support to my application.e.g. I want to store and retrive japanese characters to mySQL db using Hibernate.

I have set DB charset to UTF-8 and also added property to hibernate-cfg.xml

property name="hibernate.connection.characterEncoding">UTF-8

I have done simple POC in which from java file , I am able to declare some string variable with japanese characters and they are successfully inserted into DB and search by japanese characters is also working fine.

But when on JSP file , i fill up form, all field value are passed to controller in form of POJO, and all japansese characters in POJO's field are sutomatically converted into numerical character references as mentioned in below example,

CreateUser.jsp

<form:form method="post" commandName="userModel">
    <%@include file="/tp/web/iBusinessException.jsp"%>
    <table width="700" border="0" align="center" cellpadding="4"
        cellspacing="0">
        <tr>
            <td class="tdWscHeading">
                <img
                    src="<%=request.getContextPath()%>/tp/web/console/include/images/iconCreateuser.gif"
                    alt="Task Summary" width="20" height="20" align="absmiddle">
                <spring:message code='label.createuser'></spring:message>
            </td>
        </tr>
    </table>
    <table width="700" border="0" align="center" cellpadding="4"
        cellspacing="1" class="tableWscmain">
        <tr>
            <td width="250" class="tdWscContent fontBold">
                <spring:message code='label.firstname'></spring:message>
                <span class="fontRed"> *</span>
            </td>
            <td class="tdWscContent">
                <form:input path="firstname" cssClass="formINPUT"
                    autocomplete="off" />
                <!-- Error Message if Key is Empty Starts -->

                <span class="error"> <spring:bind path="firstname">
                        <c:if test="${not empty status.errorMessage}">
                            <c:out value="${status.errorMessage}" escapeXml="false" />
                        </c:if>
                    </spring:bind> </span>
                <!-- Error Message if key is Empty Ends -->
            </td>
        </tr>
        <tr>
            <td class="tdWscContent fontBold">
                <spring:message code='label.lastname'></spring:message>
                <span class="fontRed"> *</span>
            </td>
            <td class="tdWscContent">
                <form:input path="lastname" cssClass="formINPUT"
                    autocomplete="off" />
                <!-- Error Message if Key is Empty Starts -->
                <span class="error"> <spring:bind path="lastname">
                        <c:if test="${not empty status.errorMessage}">
                            <c:out value="${status.errorMessage}" escapeXml="false" />
                        </c:if>
                    </spring:bind> </span>
                <!-- Error Message if key is Empty Ends -->
            </td>
        </tr>
        <tr>
            <td class="tdWscContent fontBold">
                <spring:message code='label.username'></spring:message>
                <span class="fontRed"> *</span>
            </td>
            <td class="tdWscContent">
                <form:input path="username" cssClass="formINPUT"
                    autocomplete="off" />
                <!-- Error Message if Key is Empty Starts -->
                <span class="error"> <spring:bind path="username">
                        <c:if test="${not empty status.errorMessage}">
                            <c:out value="${status.errorMessage}" escapeXml="false" />
                        </c:if>
                    </spring:bind> </span>


                <!-- Error Message if key is Empty Ends -->
            </td>
        </tr>
        <tr>
            <td class="tdWscContent fontBold">
                <spring:message code='label.password'></spring:message>
                <span class="fontRed"> *</span>
            </td>
            <td class="tdWscContent">
                <form:password path="password" cssClass="formINPUT"
                    autocomplete="off" showPassword="false" />
                <!-- Error Message if Key is Empty Starts -->
                <span class="error"> <spring:bind path="password">
                        <c:if test="${not empty status.errorMessage}">
                            <c:out value="${status.errorMessage}" escapeXml="false" />
                        </c:if>
                    </spring:bind> </span>
                <!-- Error Message if key is Empty Ends -->
            </td>
        </tr>
        <tr>
            <td class="tdWscContent fontBold">
                <spring:message code='label.email'></spring:message>
                <span class="fontRed"> *</span>
            </td>
            <td class="tdWscContent">
                <form:input path="email" cssClass="formINPUT"
                    autocomplete="off" />
                <!-- Error Message if Key is Empty Starts -->
                <span class="error"> <spring:bind path="email">
                        <c:if test="${not empty status.errorMessage}">
                            <c:out value="${status.errorMessage}" escapeXml="false" />
                        </c:if>
                    </spring:bind> </span>
                <!-- Error Message if key is Empty Ends -->
            </td>
        </tr>

        <tr>
            <td class="tdWscContent fontBold">
                <spring:message code='label.groupname'></spring:message>
                <span class="fontRed"> *</span>
            </td>
            <td class="tdWscContent">
                <select id="groupname" name="groupname">
                    <c:forEach items="${GROUPS_LIST_RESULT}" var="option">
                        <option value='<c:out value="${option.groupname}" />'>
                            <c:out value="${option.groupname}" />
                        </option>
                    </c:forEach>
                </select>
                <!-- Error Message if Key is Empty Starts -->
                <span class="error"> <form:errors path="groupname"></form:errors>
                </span>
                <!-- Error Message if key is Empty Ends -->
            </td>
        </tr>

        <tr align="center">
            <td colspan="2" class="tdWscContent fontBold">
                <input type="submit" name=""
                    value="<spring:message code="label.createuser"></spring:message>">
            </td>
        </tr>
    </table>
</form:form>

MyController

@RequestMapping(value = USERADMINISTRATION_NAMESPACE + "/createUser.do", method = RequestMethod.POST)
public ModelAndView submitCreateUserPage(
        @ModelAttribute("userModel") User user, BindingResult result

) throws UserAlreadyExistsException {
    String password = "";
    password = user.getPassword();

/* Here , password i am getting like **&#164;** ,  */
/* I want exactly same Japanese characters as entered by user */
}

I have not use any character encoding filter in my application.

From jsp to controller , it is automatically converted into such above number.. I want exactly same Japanese characters as entered by user in jsp and want to insert same into DB and display on page.

I am passing all user input field's value from JSP to controller as a POJO.

Please help...

解决方案

Finally I succeed in implementing internationalization support in my Spring MVC based application....

I have followed below steps to incorporate internationalization support to my web application using Spring MVC, Hibernate , MYSQL or Oracle Database and Jboss or webLogic as a application server.

Let's say we want to add internationalization support for Japanese language.i.e user should be able to enter Japanese characters in web forms and it should be saved in same format as entered by user and also should be displayed in same language on web page.

Follow below steps.

  1. Make sure you have Japanese language support (Region specific installation) installed into operating system. If not, please install it.

  2. If you are using any IDE then configure IDE to support Japanese language by changing text encoding to UTF-8. For example,If you are using my Eclipse then change text file encoding to UTF-8. You can change by this path (Window->Preference->General->Workspace)

  3. Place Spring framework’s in built character encoding filter as a first filter in filter chain (web.xml) to make sure it runs first during request processing and runs last during response processing

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>
    <!-- set forceEncoding to true if you want to override encoding of servlet -->
    <param-name>forceEncoding</param-name>
    <param-value>true</param-value> 
</init-param>

<filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

4 Set JSP Page encoding to UTF-8 by adding below mentioned code at top of JSP.

<%@ page language="java" pageEncoding="UTF-8" contentType="text/html;charset=UTF-8"%>

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

5 Set Hibernate connection encoding scheme to UTF-8 by adding following property to hibernate-cfg.xml

<property name="hibernate.connection.characterEncoding">UTF-8</property>

Note : If using JBoss application server, please make sure you have appended characterEncoding=UTF-8 to connection-url in database service configuration file (For ex. mysql-ds.xml for mySQL database) as mentioned below.

<datasources>

<local-tx-datasource>

    <jndi-name>WSCDS</jndi-name>

   <connection-url>
   jdbc:mysql://{ipaddress}:{port}/{database_name}?characterEncoding=UTF-8
   </connection-url>

<driver-class>com.mysql.jdbc.Driver</driver-class>

<user-name>{username}</user-name>

<password>{password}</password>  

<metadata>

   <type-mapping>mySQL</type-mapping>

</metadata>

这篇关于如何在SPRING MVC POJO的字段中保留日语字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆