最近由于工作原因,没时间更新,开始吧~~
关于json的返回需要用到一个工具包来将书转换为json格式,在此用到的jar包为:
import net.sf.json.JSONArray; //将数据转换为JSON数组
import net.sf.json.JSONObject; //将数据转换为JSON对象
dao层的代码
public class ProInfomaDao extends HibernateDaoSupport { private Logger logger = Logger.getLogger(ProInfomaDao.class); /** * 查询所有数据 * @return */ @SuppressWarnings("unchecked") public ListselBankList() { String sql = "SELECT DISTINCT ENAME FROM PM_PROINFO"; //填写对应的SQL语句 List queryList = getSession().createSQLQuery(sql).list(); List bankList = new ArrayList (); queryList.add(0, "--请选择--"); BanknameDto bankDto = null; if(queryList.size() == 0){ logger.info("查不到相关信息!"); }else{ for(int i = 0;i
service层代码
public class ProInfomaService { private ProInfomaDao proInfomaDao; public void setProInfomaDao(ProInfomaDao proInfomaDao) { this.proInfomaDao = proInfomaDao; } /** * 查询数据 * @return */ public ListselBankList(){ return proInfomaDao.selBankList(); } }
web层
public class ProInfomaAction extends ActionSupport implements ServletRequestAware{ private static final long serialVersiOnUID= 1L; private ProInfomaService proInfomaService; private MapresultMap = new HashMap (); private JSONObject dataroot; private JSONArray bankroot; //此处要返回的属性名称,以bankroot为例,要与struts中 private JSONArray projroot; //bankroot且提供get set方法 private HttpServletRequest request; /** * 查询银行名称 * @return */ public String selBankList() { List selBank = proInfomaService.selBankList(); bankroot = JSONArray.fromObject(selBank); return SUCCESS; } public JSONArray getBankroot() { return bankroot; } public void setBankroot(JSONArray bankroot) { this.bankroot = bankroot; } @Override public void setServletRequest(HttpServletRequest request) { this.request = request; } }
struts2中action的配置
<&#63;xml version="1.0" encoding="UTF-8" &#63;>bankroot
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。