JSP开发常用问题解决

1、URL传中文参数乱码(tomcat服务器设置编码utf-8,谷歌、火狐、IE8/9 测试正常)
   前台js:var url = encodeURI(含中文参数的URL);
   后台action: String username=req.getParameterMap().get(key).toString();直接拿
       req.setAttribute("username", username);
   跳转后的页面: 用${username} 即可拿到
2、 iframe 中调用父页面js里面的方法: parent.方法名
3、IE不支持console但是在其他浏览器我们又希望打印调试信息,解决办法:
    $.extend({
        infoOptions:{isAllowInfo : true}, // 是否允许调试参数
        info:function(str, opts){
            var options = $.extend($.infoOptions, opts);
            if(options.isAllowInfo && typeof console != 'undefined'){
                if(console.info){
                    console.info(str);
                } else if(console.debug){
                    console.debug(str);
                } else {
                    // 不支持控制台的浏览器,比如IE8  就不做任何操作
                }
            }
        }
    });
   使用方法:$.info('调试信息'),这样支持console的浏览器打印出调试信息,不支持console的浏览器则不做任何操作
4、js中top、parent分别代表上面,加入a.jsp中iframe标签嵌入b.jsp,b.jsp中iframe又嵌入了c.jsp
那么 b.jsp中的 top=parent ,c.jsp中的top=parent.parent
5、web开发工具方法,如:IE不支持console.log,这样重写后放在工具js中,直接调用$.log("ddd");就能适应所有浏览器,还有容易出问题的eval方法,这里封装重写后,即便出问题js还会继续执行。。
$.extend({    
	'log': function(msg, showtime){    
		if(window.console && window.console.log){    
			if(showtime){    
				console.log('时间戳:' + new Date());    
			}    
			console.log(msg);    
		}    
	},    
	'doajax': function(p){    
		$.ajax($.extend({			
    
			type: 'post',    
			dataType: 'json'    
		},p));    
	},    
	'getTopWin':function(window){    
		if(window.parent == window){    
			return window;    
		}    
		if(window.parent._popwin && window.parent._popwin.istopwin){    
			return window.parent;    
		}else{    
			return getTopWin(window.parent);    
		}    
	},    
	'eval':function(expstr){    
		try{    
			return eval(expstr);    
		}catch(e){    
			return undefined;    
		}    
	}    
})
6、CSS hack由于不同的浏览器,比如Internet Explorer 6,Internet Explorer 7,Mozilla Firefox等,对CSS的解析认识不一样,因此会导致生成的页面效果不一样,得不到我们所需要的页面效果。 这个时候我们就需要针对不同的浏览器去写不同的CSS,让它能够同时兼容不同的浏览器,能在不同的浏览器中也能得到我们想要的页面效果(碰到CSS浏览器兼容性问题可以搜下这个)


你可能感兴趣的:(web开发,常用问题)