action 层:
private ElecCommonMsg  elecCommonMsg = this.getModel();

@Resource(name=IElecCommonMsgService.SERVICE_NAME)
private IElecCommonMsgService elecCommonMsgService;
public String home(){

ElecCommonMsg commonMsg = elecCommonMsgService.findElecCommonMsg();
ServletActionContext.getContext().getValueStack().pop();
ServletActionContext.getContext().getValueStack().push(commonMsg);
return "home";
}
service层:@Service(IElecCommonMsgService.SERVICE_NAME)
@Transactional(readOnly=true)
public class ElecCommonMsgServiceImpl implements IElecCommonMsgService{
@Resource(name=IElecCommonMsgDao.SERVICE_NAME)
private IElecCommonMsgDao  elecCommonMsgDao; public ElecCommonMsg findElecCommonMsg() {
List<ElecCommonMsg> list = elecCommonMsgDao.findCollectionByConditionNoPage("", null, null);
ElecCommonMsg  commonMsg = null;
if(list!=null && list.size()>0){
commonMsg = list.get(0);
}

return commonMsg;
}}
public List<T> findCollectionByConditionNoPage(String condition,
Object[] param, Map<String, String> orderby) {
String hql = "select o from "+entityClass.getSimpleName()+" o where 1=1";
String orderbyhql = this.orderby(orderby);
String finalHql = hql + condition + orderbyhql;
List<T> list = this.getHibernateTemplate().find(finalHql, param);

return list;
} private String orderby(Map<String, String> orderby) {
StringBuffer buffer = new StringBuffer("");
if(orderby!=null && orderby.size()>0){
buffer.append(" order by ");
for(Map.Entry<String, String> map:orderby.entrySet()){
buffer.append(map.getKey() +"  " +map.getValue()+",");
}
}
buffer.deleteCharAt(buffer.length()-1);
return buffer.toString();
}
最后报错是String index out of range: -1
数据库中有数据

解决方案 »

  1.   

    这是页面报的错Struts Problem ReportStruts has detected an unhandled exception:Messages:
    String index out of range: -1
    File: java/lang/AbstractStringBuilder.java
    Line number: 770
    Stacktracesjava.lang.StringIndexOutOfBoundsException: String index out of range: -1
      

  2.   

    // if判断为false,buffer.length = 0 ,deleteCharAt,报错吧。
    StringBuffer buffer = new StringBuffer("");
    if(orderby!=null && orderby.size()>0){
    buffer.append(" order by ");
    for(Map.Entry<String, String> map:orderby.entrySet()){
    buffer.append(map.getKey() +"  " +map.getValue()+",");
    }
    }
    buffer.deleteCharAt(buffer.length()-1);