【发布时间】:2012-01-30 10:37:46
【问题描述】:
我正在使用构建 Web 应用程序
- Spring MVC
- Spring 安全性
- 休眠
- MySQl
我想为我的应用程序添加国际化支持。例如我想使用 Hibernate 将日文字符存储和检索到 mySQL db。
我已将 DB charset 设置为 UTF-8 并将属性添加到 hibernate-cfg.xml
属性名称="hibernate.connection.characterEncoding">UTF-8
我已经完成了简单的 POC,在其中从 java 文件中,我可以用日文字符声明一些字符串变量,并且它们已成功插入数据库,并且日文字符搜索也可以正常工作。
但是当我在 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>
我的控制器
@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 **¤** , */
/* I want exactly same Japanese characters as entered by user */
}
我的应用程序中没有使用任何字符编码过滤器。
从 jsp 到 controller ,会自动转换成上面这样的数字.. 我想要用户在 jsp 中输入的完全相同的日文字符,并希望将相同的日文字符插入数据库并显示在页面上。
我将所有用户输入字段的值作为 POJO 从 JSP 传递到控制器。
请帮忙...
【问题讨论】:
标签: spring-mvc internationalization pojo