1. JavaScript脚本,beansoft编写的.自己改了一点点
function
AjaxFormer (form, resultDivId) {
/* * form instance */
this .form = form;
/* * div to display result in */
this .resultDivId = resultDivId;
/* *
* This will call the ajax submit method to do the form submit.
* @author beansoft
* @version 1.0 2006-05-04
*/
this .ajaxSubmitForm = function (url , method ,onCompleteMethod) {
var elements = form.elements; // Enumeration the form elements
var element;
var i;
var postContent = "" ; // Form contents need to submit
for (i = 0 ;i < elements.length; ++ i) {
var element = elements[i];
if (element.type == " text " || element.type == " textarea " || element.type == " hidden " ) {
postContent += encodeURIComponent(element.name) + " = " + encodeURIComponent(element.value) + " & " ;
}
else if (element.type == " select-one " || element.type == " select-multiple " ) {
var options = element.options,j,item;
for (j = 0 ;j < options.length; ++ j){
item = options[j];
if (item.selected) {
postContent += encodeURIComponent(element.name) + " = " + encodeURIComponent(item.value) + " & " ;
}
}
} else if (element.type == " checkbox " || element.type == " radio " ) {
if (element.checked) {
postContent += encodeURIComponent(element.name) + " = " + encodeURIComponent(element.value) + " & " ;
}
} else if (element.type == " file " ) {
if (element.value != "" ) {
postContent += encodeURIComponent(element.name) + " = " + encodeURIComponent(element.value) + " & " ;
}
} else {
postContent += encodeURIComponent(element.name) + " = " + encodeURIComponent(element.value) + " & " ;
}
}
}
/* *
* Submit the content of a form to a url.
* @param url - the url to do submit
* @param method - "get" or "post"
* @param postContent - the string with values to be submited
* @param resultDivId - the division of which to display result text in, in null, then
* create an element and add it to the end of the body
* Add by sonic
* 用来在用ajax方法提交后,执行的方法。类似与new ajax.Request(....)
*/
this .ajaxPRSubmit = function (url, method, onCompleteMethod,postContent){
var loadingDiv = document.getElementById( ' loading ' );
if ( ! loadingDiv) {
loadingDiv = document.createElement( " div " );
loadingDiv.id = " loading " ;
document.body.appendChild(loadingDiv);
}
// call in new thread to allow ui to update
window.setTimeout( function () {
loadingDiv.innerHTML = " <img src='./images/loading.gif'/> " ;
loadingDiv.style.display = "" ;
loadingDiv.style.position = " absolute " ;
loadingDiv.style.left = window.screen.availWidth / 2 ;
loadingDiv.style.top = 0 ;
}, 1 );
// code for Mozilla, etc.
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else if (window.ActiveXObject){ // code for IE
xmlhttp = new ActiveXObject( " Microsoft.XMLHTTP " );
}
if (method.toLowerCase() == " get " ) {
xmlhttp.open( " GET " , url + " ? " + postContent, true );
xmlhttp.send( null );
} else if (method.toLowerCase() == " post " ) {
xmlhttp.open( " POST " , url, true );
xmlhttp.setRequestHeader( " Content-Type " , " application/x-www-form-urlencoded " );
xmlhttp.send(postContent);
}
if (xmlhttp) {
xmlhttp.onreadystatechange = function () {
// if xmlhttp shows "loaded"
if (xmlhttp.readyState == 4 ) {
if (xmlhttp.status == 200 ){
//
if (xmlhttp.responseText == ' success ' ){
eval(onCompleteMethod + ' () ' );
} else {
Ext.MessageBox.alert( " 错误提示: " , " 企业名称重复或者其他数据格式不正确 " );
}
} else {
alert( ' AJAX提交发生错误! ' );
}
loadingDiv.innerHTML = "" ;
}
};
} else {
loadingDiv.innerHTML = " Can't create XMLHttpRequest object, please check your web browser. " ;
}
}
/* * form instance */
this .form = form;
/* * div to display result in */
this .resultDivId = resultDivId;
/* *
* This will call the ajax submit method to do the form submit.
* @author beansoft
* @version 1.0 2006-05-04
*/
this .ajaxSubmitForm = function (url , method ,onCompleteMethod) {
var elements = form.elements; // Enumeration the form elements
var element;
var i;
var postContent = "" ; // Form contents need to submit
for (i = 0 ;i < elements.length; ++ i) {
var element = elements[i];
if (element.type == " text " || element.type == " textarea " || element.type == " hidden " ) {
postContent += encodeURIComponent(element.name) + " = " + encodeURIComponent(element.value) + " & " ;
}
else if (element.type == " select-one " || element.type == " select-multiple " ) {
var options = element.options,j,item;
for (j = 0 ;j < options.length; ++ j){
item = options[j];
if (item.selected) {
postContent += encodeURIComponent(element.name) + " = " + encodeURIComponent(item.value) + " & " ;
}
}
} else if (element.type == " checkbox " || element.type == " radio " ) {
if (element.checked) {
postContent += encodeURIComponent(element.name) + " = " + encodeURIComponent(element.value) + " & " ;
}
} else if (element.type == " file " ) {
if (element.value != "" ) {
postContent += encodeURIComponent(element.name) + " = " + encodeURIComponent(element.value) + " & " ;
}
} else {
postContent += encodeURIComponent(element.name) + " = " + encodeURIComponent(element.value) + " & " ;
}
}
}
/* *
* Submit the content of a form to a url.
* @param url - the url to do submit
* @param method - "get" or "post"
* @param postContent - the string with values to be submited
* @param resultDivId - the division of which to display result text in, in null, then
* create an element and add it to the end of the body
* Add by sonic
* 用来在用ajax方法提交后,执行的方法。类似与new ajax.Request(....)
*/
this .ajaxPRSubmit = function (url, method, onCompleteMethod,postContent){
var loadingDiv = document.getElementById( ' loading ' );
if ( ! loadingDiv) {
loadingDiv = document.createElement( " div " );
loadingDiv.id = " loading " ;
document.body.appendChild(loadingDiv);
}
// call in new thread to allow ui to update
window.setTimeout( function () {
loadingDiv.innerHTML = " <img src='./images/loading.gif'/> " ;
loadingDiv.style.display = "" ;
loadingDiv.style.position = " absolute " ;
loadingDiv.style.left = window.screen.availWidth / 2 ;
loadingDiv.style.top = 0 ;
}, 1 );
// code for Mozilla, etc.
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else if (window.ActiveXObject){ // code for IE
xmlhttp = new ActiveXObject( " Microsoft.XMLHTTP " );
}
if (method.toLowerCase() == " get " ) {
xmlhttp.open( " GET " , url + " ? " + postContent, true );
xmlhttp.send( null );
} else if (method.toLowerCase() == " post " ) {
xmlhttp.open( " POST " , url, true );
xmlhttp.setRequestHeader( " Content-Type " , " application/x-www-form-urlencoded " );
xmlhttp.send(postContent);
}
if (xmlhttp) {
xmlhttp.onreadystatechange = function () {
// if xmlhttp shows "loaded"
if (xmlhttp.readyState == 4 ) {
if (xmlhttp.status == 200 ){
//
if (xmlhttp.responseText == ' success ' ){
eval(onCompleteMethod + ' () ' );
} else {
Ext.MessageBox.alert( " 错误提示: " , " 企业名称重复或者其他数据格式不正确 " );
}
} else {
alert( ' AJAX提交发生错误! ' );
}
loadingDiv.innerHTML = "" ;
}
};
} else {
loadingDiv.innerHTML = " Can't create XMLHttpRequest object, please check your web browser. " ;
}
}
JAVA后台代码
我是用Struts的,把没有用FormBean.只用了MODEL OBJECT,所以我把前台的对象类(乱码)用放射的方式重新把乱码部分的encoding转换了一次.这方法可能适合我自己这个项目对其他项目不一定支持吧.
/**
* @description
* @author sonic
* 利用bean的方法,重新set一次转换后的值
* @param object model object对象
*/
public Object beanToGBK(Object object){
try {
Field[] fields = object.getClass().getDeclaredFields();
for ( int i = 0 ; i < fields.length; i ++ ) {
if ( ! fields[i].getType().getName().startsWith( " com. " ) &&! fields[i].getName().equalsIgnoreCase( " serialVersionUID " )){
String fieldName = fields[i].getName(); // 字段名
// get,set,is方法名
String getMethodName = " get " + fieldName.substring( 0 , 1 ).toUpperCase() + fieldName.substring( 1 ,fieldName.length());
String setMethodName = " set " + fieldName.substring( 0 , 1 ).toUpperCase() + fieldName.substring( 1 ,fieldName.length());
// getYYYY()方法
Method getMethod = object.getClass().getMethod(getMethodName, null );
Class[] paramTypes = new Class[ 1 ]; // 这里你要调用的方法只有一个参数
paramTypes[ 0 ] = fields[i].getType(); // 这个参数的类型是String[]
// 但只是中文的时候才进行转换
if (String. class == paramTypes[ 0 ]){
// paramTypes是类型参数
Method setMethod = object.getClass().getMethod(setMethodName,paramTypes);
// 开始转换
Object[] paramArray = {EnCoding.changeEncoding((String)getMethod.invoke(object, null ), " GBK " , " UTF-8 " ) };
// set方法,object为传过来的mo对象,paramArray参数为 字符串参数
setMethod.invoke(object, paramArray);
}
}
}
} catch (Exception e){
System.out.println(e.getMessage());
}
return object;
}
* @description
* @author sonic
* 利用bean的方法,重新set一次转换后的值
* @param object model object对象
*/
public Object beanToGBK(Object object){
try {
Field[] fields = object.getClass().getDeclaredFields();
for ( int i = 0 ; i < fields.length; i ++ ) {
if ( ! fields[i].getType().getName().startsWith( " com. " ) &&! fields[i].getName().equalsIgnoreCase( " serialVersionUID " )){
String fieldName = fields[i].getName(); // 字段名
// get,set,is方法名
String getMethodName = " get " + fieldName.substring( 0 , 1 ).toUpperCase() + fieldName.substring( 1 ,fieldName.length());
String setMethodName = " set " + fieldName.substring( 0 , 1 ).toUpperCase() + fieldName.substring( 1 ,fieldName.length());
// getYYYY()方法
Method getMethod = object.getClass().getMethod(getMethodName, null );
Class[] paramTypes = new Class[ 1 ]; // 这里你要调用的方法只有一个参数
paramTypes[ 0 ] = fields[i].getType(); // 这个参数的类型是String[]
// 但只是中文的时候才进行转换
if (String. class == paramTypes[ 0 ]){
// paramTypes是类型参数
Method setMethod = object.getClass().getMethod(setMethodName,paramTypes);
// 开始转换
Object[] paramArray = {EnCoding.changeEncoding((String)getMethod.invoke(object, null ), " GBK " , " UTF-8 " ) };
// set方法,object为传过来的mo对象,paramArray参数为 字符串参数
setMethod.invoke(object, paramArray);
}
}
}
} catch (Exception e){
System.out.println(e.getMessage());
}
return object;
}
转换EnCoding.java
/**
* 转换字符串的内码.
*
* @param input 输入的字符串
* @param sourceEncoding 源字符集名称
* @param targetEncoding 目标字符集名称
* @return 转换结果, 如果有错误发生, 则返回原来的值
*/
public static String changeEncoding(String input, String sourceEncoding, String targetEncoding) {
if (input == null || input.equals( "" )) {
return input;
}
try {
byte [] bytes = input.getBytes(sourceEncoding);
return new String(bytes, targetEncoding);
} catch (Exception ex) {
}
return input;
}
* 转换字符串的内码.
*
* @param input 输入的字符串
* @param sourceEncoding 源字符集名称
* @param targetEncoding 目标字符集名称
* @return 转换结果, 如果有错误发生, 则返回原来的值
*/
public static String changeEncoding(String input, String sourceEncoding, String targetEncoding) {
if (input == null || input.equals( "" )) {
return input;
}
try {
byte [] bytes = input.getBytes(sourceEncoding);
return new String(bytes, targetEncoding);
} catch (Exception ex) {
}
return input;
}
感谢beansoft提供的解决方案和代码.不过经过测试还是不能100%把转换中文,还是有一点缺陷.....
所以没事别用GBK,还是用UTF-8吧