JAVA常用操作语句----项目中的总结二

来源:互联网 发布:壁虎漫步知乎 编辑:程序博客网 时间:2024/05/21 14:58
  • jsp输出格式化后的日期

<fmt:formatDate value="${whiteNumber.crtdate }" pattern="yyyy-MM-dd HH:mm:ss"/>

 

  • 如是model里的list在action中取不到值。可能在该model中的list的setter和getter方式中,使用的可能是lazy加载,改为EAGER即可。

 

 

  • <s:iterator>标签里还有<s:iterator>标签,里面的标签在使用中如果不显示数据,可能是在外部的标签对应的bean里,对bean里的list的setter和getter方法设置为private,改为public即可同样在jsp中用${model.name}取不对值,也可能在bean里的name的setter和getter属性为private ,改为public即可。

 

 

  • title="hehe&#13;hehe"  &#13可以让alt提示换行。

 

 

  • iframe标签要加</iframe>关闭标签,不然后面的元素有可能无法显示
  • IE下js的数组,“,”后都会有一空格,而FF下则没有。

 

  • 不让链接折行的css:

 

a {
     white-space:nowrap;
}

 

  • js得到父窗口支持IE FF双浏览器

var iframe = parent.document.getElementById("msgrightframe");
var iDoc = iframe.contentDocument?iframe.contentDocument:iframe.contentWindow.document;// Firefox : IE

得到父窗体的iframe


  • div自动换行,且只使用y滚动条,兼容ff:

<div id="numDiv" style="display:block;word-wrap:break-word;word-break:break-all;width:495px;height:100px;overflow-y:auto;overflow-x:hidden;border:1px solid;border-color:#7f9db9;padding:2px;"></div>

 

  • 判断session是否过期,并执行某函数

    function isSessionOut(fun)//fun:session未过期时执行的函数名
    {
        var myConn = new XHConn();
        if (!myConn) alert("XMLHTTP不能用,请用更新的浏览器.");
        var fnWhenDone = function (oXML) {
                            var ret = oXML.responseText;
                            if(ret.indexOf('~~') != -1)
                            {
                                if(parent!=null)
                                    parent.window.location = '/';
                                else
                                    window.location = '/';
                            }
                            else
                            {
                                if(fun!=null && fun!="")
                                    eval(fun);
                            }
                         };
    myConn.connect("/users/showchangepwd.do", "POST","", fnWhenDone);
    }
(showchangepwd.do指向的jsp页面包含~~字符)
使用方法1、onclick="openNew2('2','${item.id }')"
            function openNew2(num,itemid)
            {
                isSessionOut("openNew('"+num+"','"+itemid+"')");
            }
    2、onclick="isSessionOut();"
    3、onclick="isSessionOut('goremove()')"

 

 

  • 不同浏览器使用不同的css

    if(window.navigator.userAgent.indexOf("MSIE")>=1){
          //如果浏览器为IE
          setActiveStyleSheet("ie.css");
    }
    else if(window.navigator.userAgent.indexOf("Firefox")>=1){
        //如果浏览器为Firefox
        setActiveStyleSheet("ie.css");
    }
    else{
        //如果浏览器为其它
        setActiveStyleSheet("ie.css");
    }
    function setActiveStyleSheet(filename){
        document.write("<link href=/"/images//"+filename+"/" type=/"text//css/" rel=/"stylesheet/">");
    }

 

  • 复制文件

public static void copyfile(String pathSrc,String pathDest) throws IOException
  {
      FileInputStream fi=new FileInputStream(pathSrc);
      FileOutputStream fo=new FileOutputStream(pathDest);
      byte data[]=new byte[fi.available()];
      fi.read(data);
      fo.write(data);
      fi.close();
      fo.close();
  }

 

  • JAVA替换字符

    java:line = line.replaceAll(" ", "");
    line = line.replaceAll(";", ";");
    line = line.replaceAll("/t", "");//去除tab

 

 

  • 避免lastIndexOf异常

if (selectedNo.lastIndexOf(",") == selectedNo.length() - 1)
    {
             selectedNo.substring(0, selectedNo.length() - 1);
    }

    可改为:
    if (selectedNo. endsWith (","))
    {
             selectedNo.substring(0, selectedNo.length() - 1);
    }
    lastIndexOf函数有可能抛出StringIndexOutOfBoundsException

 

 

  • td高度可根据文字多少自适应

IE下去掉table标签的样式(table-layout: fixed;)

 

 

  • dhtmlTree:Tooltips设置

有三种方法去设置tooltip :
    使用结点的label("text"item结点的text属性)作为tooltip - enableAutoTooltips(mode) - 默认为false
    使用item结点的"tooltip"属性作为tooltip(如果此属性被设置了则默认使用此方法)
    使用setItemText(itemId,newLabel,newTooltip) 方法

 

  • 可以在myeclipse切换到多个tomcat中的其它tomcat,切换到相应tomcat后,再部署项目到服务器上。

 

  • 图片载入后自适应设置的尺寸

<IMG onload="javascript:if(this.width>700) {this.height=(700)*this.height/this.width;this.width=700}" border=1>

 

 

  • 通过getHibernateTemplate获得一对象时,调用该对象的list属性对应的get方法,即可在该对象中加载此list

 public CtMmsMessage getMessage(long msgid) throws Exception {
        CtMmsMessage m = (CtMmsMessage) getHibernateTemplate().get(CtMmsMessage.class, msgid);
        if (m == null) return null;
        m.getReceiver();  //别忘了触发一下加载
        m.getFramelist().size();  //别忘了触发一下加载
        return m;
    }

 

  • 在sql语句中的join和where中用到的字段,建立索引,这样可以大幅提高查询速度。项目证明,对于大数据量统计,可提高百倍速度。

 

  • 提供转换编码后的供下载用的文件名 ,解决IE、firefox及linux环境下下载文件中文名乱码问题。

public String getDownloadFileName(String nameStr)
    {
        String downFileName = nameStr;
        try {
            ActionContext ctx = ActionContext.getContext();
            HttpServletRequest request = (HttpServletRequest) ctx.get(ServletActionContext.HTTP_REQUEST);
            String agent = request.getHeader("USER-AGENT"); 
            if (null != agent && agent.indexOf("MSIE") != -1) {// ie 
                downFileName = java.net.URLEncoder.encode(downFileName, "UTF-8");
            } else {// firefox 
                downFileName = new String(downFileName.getBytes("UTF-8"), "ISO8859-1"); 
            } 
        } catch (UnsupportedEncodingException e) {
            //e.printStackTrace();
            downFileName="unknow.xls";
        }
        return downFileName;
    }

原创粉丝点击