最近由于工作原因,没时间更新,开始吧~~

关于json的返回需要用到一个工具包来将书转换为json格式,在此用到的jar包为:

1 import net.sf.json.JSONArray;  //将数据转换为JSON数组
2 import net.sf.json.JSONObject;  //将数据转换为JSON对象

dao层的代码

 1 public class ProInfomaDao extends HibernateDaoSupport {
 2  private Logger logger = Logger.getLogger(ProInfomaDao.class);
 3   /**
 4      * 查询所有数据
 5      * @return
 6      */
 7     @SuppressWarnings("unchecked")
 8     public List<BanknameDto> selBankList() {
 9         String sql = "SELECT DISTINCT ENAME FROM PM_PROINFO";            //填写对应的SQL语句
10         List<String> queryList = getSession().createSQLQuery(sql).list();
11         List<BanknameDto> bankList = new ArrayList<BanknameDto>();
12         queryList.add(0, "--请选择--");
13         BanknameDto bankDto = null;
14         if(queryList.size() == 0){
15             logger.info("查不到相关信息!");
16         }else{
17             for(int i = 0;i < queryList.size();i++){
18                 bankDto = new BanknameDto();
19                 if(i == 0){
20                     bankDto.setId(String.valueOf(i+1));
21                     bankDto.setText(queryList.get(i));
22                     bankDto.setSelected("true");
23                 }else{
24                     bankDto.setId(String.valueOf(i+1));
25                     bankDto.setText(queryList.get(i));
26                 }
27                 bankList.add(bankDto);
28             }
29         }
30         return bankList;                    //此处用到hibernate来查询数据,返回结果为一个[demo1,demo2,demo3]的List集合
31     }
32 }

 service层代码

 1 public class ProInfomaService {
 2     private ProInfomaDao proInfomaDao;
 3     public void setProInfomaDao(ProInfomaDao proInfomaDao) {
 4         this.proInfomaDao = proInfomaDao;
 5     }
 6 
 7     /**
 8      * 查询数据
 9      * @return
10      */
11     public List<BanknameDto> selBankList(){
12         return proInfomaDao.selBankList();
13     }
14 }

web层

 1 public class ProInfomaAction extends ActionSupport implements ServletRequestAware{
 2     private static final long serialVersionUID = 1L;
 3     private ProInfomaService proInfomaService;
 4     private Map<String, Object> resultMap = new HashMap<String, Object>();    
 5     private JSONObject dataroot;                                  
 6     private JSONArray bankroot;                                    //此处要返回的属性名称,以bankroot为例,要与struts中
 7     private JSONArray projroot;                                    //<param name="root">bankroot</param>且提供get set方法
 8     private HttpServletRequest request;
 9 
10     /**
11      * 查询银行名称
12      * @return
13      */
14     public String selBankList() {
15         List<BanknameDto> selBank = proInfomaService.selBankList();
16         bankroot = JSONArray.fromObject(selBank);
17         return SUCCESS;
18     }
19 
20     public JSONArray getBankroot() {
21         return bankroot;
22     }
23 
24     public void setBankroot(JSONArray bankroot) {
25         this.bankroot = bankroot;
26     }
27     @Override
28     public void setServletRequest(HttpServletRequest request) {
29         this.request = request;
30     }
31 }

struts2中action的配置

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
 3 <struts>
 4     <package name="proInfo" namespace="/" extends="json-default">
 5    <!--此处返回json只可以用json-default result结果集可以改变返回参数的名称 -->
 6         <action name="query" class="noticeAction" method="selBankList">
 7             <result name="success" type="json">
 8             </result>
 9             <result name="returnMap" type="json">
10                 <param name="root">bankroot</param>              
11             </result>
12         </action>
13 </struts>