action 获取request
ActionContext ctx = ActionContext.getContext();
HttpServletRequest request = (HttpServletRequest) ctx.get(ServletActionContext.HTTP_REQUEST);
日期格式范例
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
GregorianCalendar gc =new GregorianCalendar();
gc.add(GregorianCalendar.HOUR_OF_DAY, -2);
字符串小写比较
"1".equalsIgnoreCase(stype)
标准dao查寻列表方法
public List
return this.getHibernateTemplate().executeFind(new HibernateCallback(){
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
String sql = " select mobile from CT_MESSAGE_MOBILENUM order by mobile ";
Query query = session.createSQLQuery(sql.toString());
List list = query.list();
return list;
}
});
}
·if(StringUtils.isNotEmpty(this.errbackmsg))//判断是否为空
·
·return testStr.replaceAll("^[ ]+|[ ]+$","");//清除所有空格
·String regx = "[a-zA-Z0-9]{1}//w+([//.-]?//w+)*@//w+([//.-]?//w+)*(//.//w{2,3})";
Pattern p = Pattern.compile(regx);
Matcher m = p.matcher(mail);
return m.matches();
·String addString="";
if(areatype==1){ //全国
addString =" and a.numareatype = 1 ";
}else if (areatype ==2) { //省
addString="and a.numareatype =2 and a.numprovinceid ="+numprovinceguid; }else { //市
addString="and a.numareatype =3 and a.numcityid = "+numcityguid;
}
String sql="select sum(a.numarrivedtotal) as total,to_char(a.datbegindate,'yyyy-mm') as months " +
"from ct_ecstatisticsbyarea a " +
"where a.datbegindate>=to_date(:sd,'yyyy-mm-dd') " +
"and a.datbegindate<=to_date(:ed,'yyyy-mm-dd') " +
"and a.numcycletype = 5 " +
addString+
" group by a.datbegindate order by a.datbegindate ";
Query query = session.createSQLQuery(sql).addScalar("total", Hibernate.LONG).addScalar("months", Hibernate.STRING);
·
一般报java.lang.NullPointerException的原因有以下几种:
1、字符串变量未初始化;
2、接口类型的对象没有用具体的类初始化,比如:
List lt;会报错
List lt = new ArrayList();则不会报错了
3、当一个对象的值为空时,你没有判断为空的情况。
你可以试着把下面的代码前加一行代码:
if(rb!=null && rb!="")
改成:
if(rb==null);
if(rb!==null&&rb!="")
或者在使用该变量之前,事先赋0值。
jsp页面中截取字符串
DAO分页查寻示例
public List findReprotResultSet_ByPages(final int iCurrentPage,final int pageSize,final String querySql){
log.debug("finding Ct400CountWarehouse instance by findCallLengthSortData()");
try {
return getHibernateTemplate().executeFind(new HibernateCallback(){
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
// log.info("queryStr= "+querySql);
Query query = session.createSQLQuery(querySql);
query.setFirstResult(iCurrentPage);
query.setMaxResults(pageSize);
List
DAO查寻纪录总数标准方法
public long findReprotResultSetCount(final String querySql){
try {
Query queryCount = getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(querySql);
return ((BigDecimal)queryCount.uniqueResult()).intValue();
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
}
正则判断使用两例
boolean reStr = currentStr.matches("[0-9A-Za-z]*")判断是否英文和数字
String regEx = "^[a-zA-Z/u4e00-/u9fa5]+$";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(currentStr);
return m.matches();
日期格式转换
public static Date parseDate(String mydate){
Date ret = null;
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
try{
ret = fmt.parse(mydate);
}catch(Exception e){e.printStackTrace();}
return ret;
}
解决中文字符乱码
String agent = request.getHeader("USER-AGENT"); //中文字符乱码
if (null != agent && agent.indexOf("MSIE") != -1)
{// ie
filedisplay = java.net.URLEncoder.encode(filedisplay, "UTF-8");
}
else
{// firefox
filedisplay = new String(filedisplay.getBytes("UTF-8"), "iso8859-1");
}
限制上传文件大小。
在jsp页面访问标签内容
通过某属性查找对象列表
public List findByProperty(String propertyName, Object value) {
log.debug("finding Ct400destination instance with property: "
+ propertyName + ", value: " + value);
try {
String queryString = "from Ct400destination as model where model."
+ propertyName + "= ? order by model.numassignrate desc";
return getHibernateTemplate().find(queryString, value);
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
}
StringUtils用法示例
StringUtils.isEmpty(null) = true
StringUtils.isEmpty("") = true
StringUtils.isEmpty(" ") = false //注意在 StringUtils 中空格作非空处理
StringUtils.isEmpty(" ") = false
StringUtils.isEmpty("bob") = false
StringUtils.isEmpty(" bob ") = false
StringUtils.isNotEmpty(null) = false
StringUtils.isNotEmpty("") = false
StringUtils.isNotEmpty(" ") = true
StringUtils.isNotEmpty(" ") = true
StringUtils.isNotEmpty("bob") = true
StringUtils.isNotEmpty(" bob ") = true
StringUtils.isBlank(null) = true
StringUtils.isBlank("") = true
StringUtils.isBlank(" ") = true
StringUtils.isBlank(" ") = true
StringUtils.isBlank("/t /n /f /r") = true //对于制表符、换行符、换页符和回车符
StringUtils.isBlank() //均识为空白符
StringUtils.isBlank("/b") = false //"/b"为单词边界符
StringUtils.isBlank("bob") = false
StringUtils.isBlank(" bob ") = false
StringUtils.trim(null) = null
StringUtils.trim("") = ""
StringUtils.trim(" ") = ""
StringUtils.trim(" /b /t /n /f /r ") = ""
StringUtils.trim(" /n/tss /b") = "ss"
StringUtils.trim(" d d dd ") = "d d dd"
StringUtils.trim("dd ") = "dd"
StringUtils.trim(" dd ") = "dd"
StringUtils.trimToEmpty(null) = ""
StringUtils.trimToEmpty("") = ""
StringUtils.trimToEmpty(" ") = ""
StringUtils.trimToEmpty(" /b /t /n /f /r ") = ""
StringUtils.trimToEmpty(" /n/tss /b") = "ss"
StringUtils.trimToEmpty(" d d dd ") = "d d dd"
StringUtils.trimToEmpty("dd ") = "dd"
StringUtils.trimToEmpty(" dd ") = "dd"
StringUtils.strip(null) = null//去掉字符串两端的空白符
StringUtils.strip("") = ""
StringUtils.strip(" ") = ""
StringUtils.strip(" /b /t /n /f /r ") = "/b"
StringUtils.strip(" /n/tss /b") = "ss /b"
StringUtils.strip(" d d dd ") = "d d dd"
StringUtils.strip("dd ") = "dd"
StringUtils.strip(" dd ") = "dd"
s:iterator及s:if标签使用示例
分页获取list的DAO标准写法
public List getList() throws HibernateException {
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
Query query = session.createQuery(
"from Table");
query.setFirstResult(0);
query.setMaxResults(10);
List list = query.list();
tx.commit();
HibernateUtil.closeSession();
return list;
}
判断对象中的list是否为空