当柳上原的风吹向天际的时候...

真正的快乐来源于创造

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  368 Posts :: 1 Stories :: 201 Comments :: 0 Trackbacks
 

第一部分:页面中JavaScript的写法:

 

function isSoleContractName(contractNameTextBox){

                
var contractName=contractNameTextBox.value;

                
var sameNameContractId="NONE";

 

                
var url=encodeURI('<%=request.getContextPath()%>/contract/checkSoleContractName.action?contractName='+contractName);

                url
=encodeURI(url);// 有中文进行两次编码

 

                
new Ajax.Request(url,{     

                                   method:'post',     

                                   onSuccess: 
function(ajaxObj){                            

                                                                
var status=ajaxObj.responseXML.getElementsByTagName("status")[0].firstChild.data;

                                                                                                                                

                                                                
if(status=="ok"){

                                                                                
// 返回正确信息

                                                                                

                                                                                
// 找到所有节点放入数组

                                                                                
var id=ajaxObj.responseXML.getElementsByTagName("contractId")[0].firstChild.data;

                                                                                sameNameContractId
=id;

                                                                                

                                                                                
if(sameNameContractId != "NONE"){

                                                                                                
// 得到本身的ID

                                                                                                
var contractId = document.getElementById("instance.contractId").value;

                                                                                                

                                                                                                
if(contractId!=sameNameContractId){

                                                                                                                
// 验证结果有重复的,弹出对话框提示用户

                                                                                                                
var text="您输入的合同名称与ID为"+sameNameContractId+"的合同名称重复,请更改一下。";

                                                                                                                alert(text);

                                                                                                                contractNameTextBox.focus();

                                                                                                }


                                                                                }


                                                                                
else{

                                                                                                
return;

                                                                                }


                                                                }


                                                                
else{

                                                                                
// 返回错误信息

                                                                                
var text=ajaxObj.responseXML.getElementsByTagName("text")[0].firstChild.data;

                                                                                msger.showWarningMsg(
"出现错误"+text);

                                                                }


                                   }
,     

                                   onFailure: 
function()

                                                   msger.showErrorMsg(
"无法取得服务器的响应");

                                   }
   

                                }


                ); 

}


第二部分:

Struts2中对应Action的写法:

public class CheckSoleContractNameAction extends AbstractAction {

                
private static final long serialVersionUID = 9000299191145305626L;

                

                
private TbContractService contractService;

                

                
private String contractName;

                

                
public String execute() throws IOException{

                                HttpServletResponse response 
= ServletActionContext.getResponse();

                                response.setContentType(
"text/xml;charset=UTF-8");

                                response.setHeader(
"Cache-Control","no-cache");

                                HttpServletRequest request
=ServletActionContext.getRequest();

                                request.setCharacterEncoding(
"UTF-8");  

                                

                                contractName
=UTF8Coder.decode(contractName);

                                System.out.println(
"contractName 1="+contractName);

                                

                                
// 得到同名合同的ID

                                String sameNameContractId
=getSameNameComtractId(contractName);

                                

                                
if(sameNameContractId==null){

                                                StringBuilder sb
=new StringBuilder();

                                                sb.append(
"<response>");

                                                sb.append(
"<status>ok</status>");

                                                sb.append(
"<contractId>NONE</contractId>");

                                                sb.append(
"</response>");

                                                

                                                response.getWriter().write(sb.toString());

                                                response.getWriter().close();

                                }


                                
else{

                                                StringBuilder sb
=new StringBuilder();

                                                sb.append(
"<response>");

                                                sb.append(
"<status>ok</status>");

                                                sb.append(
"<contractId>"+sameNameContractId+"</contractId>");

                                                sb.append(
"</response>");

                                                

                                                response.getWriter().write(sb.toString());

                                                response.getWriter().close();

                                }


                                

                                
return null;

                }


}


 

第三部分:涉及后台UTF-8转码的类:

import java.io.UnsupportedEncodingException;

import java.net.URLDecoder;

import java.net.URLEncoder;

 

 

/**

 * UTF8转码器

 * 
@author heyang

 *

 
*/


public class UTF8Coder{

                
private static final String UTF_8 = "utf-8";// 编码形式

 

                
/**

                 * 对文字进行UTF8转码

                 * 
@param str

                 * 
@return

                 
*/


                
public static String encode(String str){

                                
try {

                                                
return URLEncoder.encode(str, UTF_8);

                                }
 catch (UnsupportedEncodingException e) {

                                                
return null;

                                }


                }


                

                
/**

                 * 将转码后的文字还原

                 * 
@param str

                 * 
@return

                 
*/


                
public static String decode(String str){

                                
try {

                                                
return URLDecoder.decode(str, UTF_8);

                                }
 catch (UnsupportedEncodingException e) {

                                                
return null;

                                }


                }


}


 

注意点:

1.       如果请求参数有中文,切记用JavaScript函数encodeURI进行两次编码,在服务器端用URLDecoder类解码,此方式万无一失。

在控制台打印不是可以完全信任的,比如我的英文系统中Eclipse控制台中中文打印出来就是问号,但是实际上是正常的中文。因此,不要以控制台输入正确的文字才认为转码成功。
posted on 2011-03-14 11:35 何杨 阅读(364) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: