0

我的网络应用程序有一些问题,如下所示:

1)我通过 Jquery ajax 调用控制器并以 JSON 格式获取响应数据。

2) 该控制器点击数据库并从数据库中检索数据,检索到的数据为阿拉伯文格式。

3) 然后我在 jsp 页面中构造的 Jquery 对话框中填充该数据,但该数据格式不正确

======================ajax调用======================

$.ajax({

       url: "Login/getUserData.html",
       type: "GET",
      cache: false, 
      async: false,
   dataType:"json",
contentType: "application/json; charset=utf-8",
   success : function(data) {
     jQuery.parseJSON(data);
     if(data!=null)
         {
                            //*****************Start***************    
                            $("#post").dialog({
                                height : 500,
                                width : 550,
                                resizable: false,
                                modal : true,
                                open: function(){
                                    $('#cName').val(data.fname+" "+data.lname);     $('#cEmail').val(data.email);      
                                    $('#cNo').val(data.contact);    
                                    $('#dateL').val(data.currentDate);
                                   }
                            }); 
                             //*****************End***************    
                        }
                        else
                        {
                            $("#notLoggedInMessageDialog").dialog('open');
                        }
                    },
                    error:function(){
                        alert("Error occured");
                    }}); 

      =====================LoginController.java =================
@Controller
@RequestMapping("/Login/")
@Scope("request")
public class LoginController {

/*
 * method to get log-id user's data
 */
@RequestMapping(value = "/getUserData", method = RequestMethod.GET, produces =    {"application/json; charset=UTF-8"})
@ResponseBody
public String getUserData(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
    System.out.println("================inside LoginController.getUserData()=============");
    UserModel uModel = (UserModel) request.getSession().getAttribute("user");
    request.setCharacterEncoding("UTF-8");
    response.setContentType("text/html;charaset=UTF-8");
    String user = null;
    if (uModel != null) {
        UserModel newUserModel = new UserModel();
        newUserModel.setuId(uModel.getuId());
        newUserModel.setContact(uModel.getContact());
        String email = new String(uModel.getEmail().getBytes("UTF-8"));
        System.out.println(uModel.getEmail() + "   encoded email = " + email);
        newUserModel.setEmail(uModel.getEmail());
        newUserModel.setFname(uModel.getFname());
        newUserModel.setLname(uModel.getLname());
        newUserModel.setEmailTemplate(uModel.getEmailTemplate());
        String current_Date = new SimpleDateFormat("MM/dd/yyyy").format(new Date());
        newUserModel.setCurrentDate(current_Date);
        Gson gson = new GsonBuilder().setDateFormat("MM/dd/yyyy").create();
        user = gson.toJson(newUserModel);
        System.out.println("user = " + user);
    }
    return user;
}
}

====================postlisting.jsp====================
<%@ page language="java" contentType="text/html; charset=UTF-8"
     pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<!DOCTYPE html>
<%
String direction = (String) request.getAttribute("data");
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charaset=UTF-8");
%>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
     <div>
        <fieldset  style="border: none;">
            <center>
                <legend>
                    <b><"label.extruder.myProfile.newListing.basicinfo"/></b>
                </legend>
                <table id="f1" style="text-align: justify;">
                    <tr>
                        <td ><"label.extruder.myProfile.newListing.cntName"/>:*</td>
                        <td ><input type="text" style="width: 100px;" id="cName"/></td>
                    </tr>
                    <tr>
                        <td><"label.extruder.myProfile.newListing.cntEmail"/>:*</td>
                        <td><input type="text" id="cEmail" style="width: 100px;"/></td>
                    </tr>
                    <tr>
                        <td><"label.extruder.myProfile.newListing.cntNo"/>:*</td>
                        <td><input type="text" id="cNo" style="width: 100px;"/></td>
                    </tr>
                    <tr>
                        <td><"label.extruder.myProfile.newListing.dateL"/>:*</td>
                        <td><input type="text" id="dateL" style="width: 100px;"/></td>
                    </tr>
                    <tr>
                        <td><"label.extruder.myProfile.newListing.dateA"/>:*</td>
                        <td><input type="text" id="dateA" style="width: 100px;"/></td>
                    </tr>
                    <tr>
                        <td><"label.extruder.myProfile.newListing.rent"/>:*</td>
                        <td><input type="text" id="rent" style="width: 100px;" /></td>
                    </tr>
                      </tr>
                </table>
               </center>
        </fieldset>
          </div>
    </body>
</html>

任何机构都可以解决这个问题吗?

4

3 回答 3

1

我已通过将返回类型更改为 void 并在控制器中添加一些代码来解决此问题,如下所示:

@Controller
@RequestMapping("/Login/")
@Scope("request")
public class LoginController {

/*
 * method to get log-id user's data
 */
@RequestMapping(value = "/getUserData", method = RequestMethod.GET, produces =         {"application/json; charset=UTF-8"})
@ResponseBody
public void getUserData(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
System.out.println("================inside   LoginController.getUserData()=============");
UserModel uModel = (UserModel) request.getSession().getAttribute("user");
 String user = null;
if (uModel != null) {
    UserModel newUserModel = new UserModel();
    newUserModel.setuId(uModel.getuId());
    newUserModel.setContact(uModel.getContact());
    String email = new String(uModel.getEmail().getBytes("UTF-8"));
    System.out.println(uModel.getEmail() + "   encoded email = " + email);
    newUserModel.setEmail(uModel.getEmail());
    newUserModel.setFname(uModel.getFname());
    newUserModel.setLname(uModel.getLname());
    newUserModel.setEmailTemplate(uModel.getEmailTemplate());
    String current_Date = new SimpleDateFormat("MM/dd/yyyy").format(new Date());
    newUserModel.setCurrentDate(current_Date);
    Gson gson = new GsonBuilder().setDateFormat("MM/dd/yyyy").create();
    user = gson.toJson(newUserModel);

   //new code added
response.setCharacterEncoding("utf-8");
    response.getWriter().write(user);
}
return user;
}
}
于 2013-05-10T06:09:50.980 回答
1

在 Head 块中使用元标记

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

希望对你有帮助

于 2013-05-09T07:47:53.757 回答
0

请在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>  
   <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>

于 2015-03-08T04:26:03.837 回答