2009-04-24
1、关于ajax的一点知识
ajax本身上异步交互的,这也是它的成功之处,但异步并不总是有用的,今天就遇到一个因为异步的引起的问题,就是提交表单时,要进行表单验证,其中有些表单是要进行ajax验证的:
function submitForm(){
var storeName=$("#storeName");
var url="${req.contextPath}/store/checkStoreName.action";
var flag=true;
$.post(url,{storeName:storeName.val()},
function(data){
if(data=="exist"){
showMsg_OnError(document.getElementById("storeName"),"店铺名已经存在");
flag=false;
}else{
flag=true;
showMsg_OnPass(obj);
}
});
if(!flag){
return false;
}
var storeClass=$("#storeClass");
if(storeClass.val()==""){
showMsg_OnFocus(document.getElementById("storeClass"),"请选择店铺类型");
storeClass.focus();
return false;
}
document.form1.submit();
}
在这段代码中有一段ajax验证的代码,同时一个哨兵flag,如果店铺名存在的话,就把flag设为false,然后在ajax调用后判断这个哨兵字段为false时,表单就不提交,但因为ajax是异步的,所以ajax在还没有来得及调用success后的方法,就调用submitForm()方法后面的代码了,而这时候的flag还是true(即使ajax验证店铺名存在),
解决这个问题的办法就是不要让ajax异步执行,这时候就得用jQuery的$.ajax()这个方法了:
var flag=true;
$.ajax({
type:"post",
url:url,
data:"storeName="+storeName.val(),
async:false,
success:function(data){
if(data=="exist"){
showMsg_OnError(document.getElementById("storeName"),"店铺名已经存在");
document.getElementById("storeName").focus();
flag=false;
}else{
flag=true;
showMsg_OnPass(document.getElementById("storeName"));
}
}
});
if(!flag){
return false;
}
2、用webwork的拦截器来过滤页面输入的HTML标签
package com.upcenter.interceptor;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.regex.*;
import javax.servlet.ServletContext;
import com.opensymphony.xwork.ActionInvocation;
import com.opensymphony.xwork.interceptor.Interceptor;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.opensymphony.webwork.ServletActionContext;
public class FilterInterceptor implements Interceptor {
/**
* 替换bean中的所有string类型字段的script等危险脚本 注意参数仅限于bean,不能为string等基础类型
* beanFieldWipeScript
*
* @param obj
* @throws Exception
* @return void
* @throws Exception
*/
public static void beanFieldWipeScript(Object obj) throws Exception {
if (obj == null)
return;
Field fieldsa[] = obj.getClass().getDeclaredFields(); // 取得clsa的所有field
Field fielda;
String ecclasspath = "com.upcenter.model";
// 在a中查找字段fieldname
for (int j = 0; j < fieldsa.length; j++) {
fielda = fieldsa[j];
if (fielda.getType().getName().toLowerCase().indexOf(
"java.lang.string") != -1) {// 属性为string类型
int mod = fielda.getModifiers();
String modifiers = Modifier.toString(mod).toLowerCase(); // 字段的修饰关键字
if (modifiers.indexOf("static") == -1
&& modifiers.indexOf("final") == -1) { // 不包含static和final关键字
fielda.setAccessible(true);
String tmpfieldvalue = fielda.get(obj) == null ? null
: (String) fielda.get(obj);
fielda.set(obj, wipeScript(tmpfieldvalue));
}
} else if (fielda.getType().getName().toLowerCase().indexOf(
ecclasspath) != -1) { // 为model时
fielda.setAccessible(true);
beanFieldWipeScript(fielda.get(obj));// 递归调用处理对象内的对象
}
}
//System.out.println("脚本过滤完毕:" + obj.getClass().getName());
}
/**
* 过滤script 过滤iframe 过滤on事件 wipeScript
*
* @param html
* @return
* @return String
* @throws Exception
*/
public static String wipeScript(String html) {
if (html == null){
return null;
}
//System.out.println("过滤前:"+html);
html = html.replaceAll("<", "<").replaceAll(">", ">");
//System.out.println("过滤后:"+html);
return html;
}
public void destroy() {
}
public void init() {
}
public String intercept(ActionInvocation invocation) throws Exception {
Object acionobj = invocation.getAction();
beanFieldWipeScript(acionobj); // 调用进行过滤
return invocation.invoke();
}
}
2009-04-27
1、在java中的正则表达式中,/s并不能匹配全角空格:要替换全角空格直接输入全角空格就可以了:
String keyWords = model.getKeyWords().replaceAll(" "," ");
2009-04-28
反过来用like,性能可能不好,但也不失为一种解决问题的思路,如下面:是否keyword包含非法字符
select * from dict_keyword_filter d where #keyword# like concat(concat('%',keyword),'%')
2009-04-29
1、jquery的:contains(text)的用法:
含意:匹配包含给定文本的元素
$("#more_courses #courses_table tbody tr").hide().filter(":contains("+$(this).val()+")").show();
2、一个正则表达式的网站,以前老是忘记
3、jquery的index()方法
可以通过index取得元素的索引值,上代码:
$("td").hover(function(){ //alert($("li").index(this)); alert($("td").index(this)); });
<table>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
</table>