上次的JqGrid教學只有教了基本的配置與如何在local端加入資料

 

所以這次要做的,就是要跟資料庫做連結

講到資料庫連結當然是用ssh的架構

所以還沒有看過的人可以先去看這幾篇喔Struts2+Spring架構配置教學(Spring)Struts2+Spring+Hibernate架構配置教學(Hibernate)上Struts2+Spring+Hibernate架構配置教學(Hibernate+JNDI)下

配置完ssh環境後,如果還沒有配jqGrid環境的話,先去看JqGrid 教學(基礎配置)

都好了以後,就可以開始接下來的教學了

 

在開始前要記得放struts2-json-plugin-2.2.3.jar這個jar檔

因為我們要用到json格式了

 

而連結資料庫在jsp上要做的事情不多

跟在local端差不多,不過因為要連結資料庫的話勢必要用到分頁

在jqGrid中已經有了分頁的css了,但是分頁的實作要靠自己

不過也不會很難就是了,所以這篇的重點就在於server端的實作

不過沒有分頁的實作

 

 

照慣例跳個舞直接把jsp貼上來給各位客倌看

其實跟JqGrid 教學(基礎配置)這篇差不多

只不過只有幾個地方修改了,請看紅字部分

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="../css/jquery-ui-1.8.19.custom.css" >
<link rel="stylesheet" type="text/css" href="../css/ui.jqgrid.css" >
<script type="text/javascript" src="../js/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="../js/jquery.jqGrid.min.js"></script>
<script type="text/javascript" src="../js/grid.locale-cn.js"></script>
<title>grid首頁</title>

<script type="text/javascript">
    $(document).ready(function() {
        $("#grid").jqGrid({
            url:"<s:url action="doQueryGameList" namespace="/grid"/>",
            datatype: "json",
            height: "auto",
            colNames:['序號','遊戲', '好玩程度'],
            colModel:[
                {name:'gameId',index:'gameId', width:60 },
                {name:'gameName',index:'gameName', width:90 },
                {name:'desc',index:'desc', width:100},
            ],
            jsonReader: {
                root:"dataRows",             
                page: "curPage",           
                total: "totalPages",    
                records: "totalRecords",
                repeatitems : false                
                },
            caption: "討厭鬼grid"
        });
    });
</script>
</head>
<body >
    <table id="grid"></table>
</body>
</html>

 

url:"<s:url action="doQueryGameList" namespace="/grid"/>" 這一行指的是資料的網址

討厭鬼在這邊用的是struts tag 會自動轉換成http://xxx.xxx.xxx:xxx/grid/doQueryGameList.action

datatype: "json" 這一行指的是資料的型態為json型態

jsonReader: {
                root:"dataRows",             
                page: "curPage",           
                total: "totalPages",    
                records: "totalRecords",
                repeatitems : false                
                },

這裡的意思是指root會去讀回傳回來的json裡key為dataRows的資料

而root表示為grid的資料

page則為當前的頁碼

total則為資料總頁數

records則為資料的總筆數

repeatitems則為..........討厭鬼忘記了,請大家去google一下吧


而在struts的設定檔方面

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">

<struts>
    <package name="default" extends="struts-default" namespace="/grid">
        <result-types>
            <result-type name="json" class="org.apache.struts2.json.JSONResult"/>
        </result-types>
        <action name="index" class="gridAction" method="index">
            <result name="success">/grid/index.jsp</result>
        </action>
        
        <action name="doQueryGameList" class="gridAction" method="doQueryGameList">
            <result name="success" type="json">
                <param name="root">result</param>
            </result>
        </action>
    </package>
</struts>

struts-default這個部分呢,因為有時會用到tiles所以討厭鬼都用struts-default

所以要加上下面這一段

         <result-types>
            <result-type name="json" class="org.apache.struts2.json.JSONResult"/>
        </result-types>

如果比較懶的話可以將struts-default換成json-default

這樣的話就可以不用加上result-types那一段了

<action name="doQueryGameList" class="gridAction" method="doQueryGameList">
    <result name="success" type="json">
        <param name="root">result</param>
    </result>
</action>


先來覆習一下

name="doQueryGameList" 這裡表示說 action的名稱

class="gridAction" 表示說 這裡會讀spring設定檔裡bean id為gridAction的設定

doQueryGameList 表示 class裡面的method名稱

 

<result name="success" type="json">
        <param name="root">result</param>
</result>

type="json"表示說回傳的資料型態為json格式

<param name="root">result</param>這裡root是表示說主要的回傳資料,後面的result為回傳主要資料的物件名稱

在這邊討厭鬼主要是回傳result這個物件,其它的都不要

 

下面為action-context的設定檔

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.1.xsd ">
    

    <bean id="gridAction" class="grid.com.action.GridAction" scope="prototype"/>

</beans>

簡單來說用到一個bean 都會new 一個新的物件出來

就像 new grid.com.action.GridAction() 這樣

scope="prototype "這邊的意思就是說,每次呼叫這個bean都會重新new一個物件

沒有加這段的話,就只會new一次

 

下列為action的java檔

package grid.com.action;

import grid.com.entity.Game;
import grid.com.service.GridService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.opensymphony.xwork2.ActionSupport;

public class GridAction extends ActionSupport{
    @Resource
    GridService gridService;
    
    private Map<String,Object> result = new HashMap<String, Object>();
    

    public String index(){
        return SUCCESS;
    }
    
    public String doQueryGameList(){
        result.put("dataRows",gridService.doQueryGameList());
        return SUCCESS;
    }
    
    public Map<String, Object> getResult() {
        return result;
    }
}

 

@Resource
 GridService gridService  這邊為自動注入,需在service-context的spring設定檔裡設定

 

 

public String doQueryGameList(){
        result.put("dataRows",gridService.doQueryGameList());
        return SUCCESS;
}

這邊這個method就是在struts.xml裡設定的method

result.put("dataRows",gridService.doQueryGameList()); 這裡的result map 為什麼要put dataRows呢?

dataRows就是在jqGrid裡的要讀的取的key值

 

 

下列為service-context的spring設定檔

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.1.xsd ">

    <context:annotation-config/>

    <bean id="gridService" class="grid.com.service.impl.GridServiceImpl"/>


    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="doQuery" read-only="true"/>
            <tx:method name="*" rollback-for="java.lang.Exception"/>
        </tx:attributes>
    </tx:advice>
    
     <aop:config>
         <aop:advisor advice-ref="txAdvice" pointcut="execution(* grid.com.service.impl.GridServiceImpl.*(..))"/>
    </aop:config>
</beans>

 

<context:annotation-config/> 為要使用自動注入時需宣告的字串

其它的到Spring+transaction manager 配置教學看吧.....

有點太多了討厭鬼吃不消法....

 

 

下列為service的interface這邊就不多說了

package grid.com.service;

import grid.com.entity.Game;

import java.util.List;

public interface GridService {
    public List<Game> doQueryGameList();
}

 

 

下列為service的實作

package grid.com.service.impl;

import grid.com.entity.Game;
import grid.com.entity.dao.GameDao;
import grid.com.service.GridService;

import java.util.List;

import javax.annotation.Resource;

public class GridServiceImpl implements GridService{
    @Resource
    private GameDao gameDao;
    public List<Game> doQueryGameList() {
        return gameDao.doQueryGameList();
    }

}

在這邊只有重點,因為這個method沒什麼商業邏輯

所以只有用一個dao做查詢

在程式中dao是可以對任何的資料表做增刪改查

不過為了方便管理,方便維護,一個dao請對一個資料表做事

也就是說如果有a資料表,就會有一個aDao,有b資料表就會有一個bDao

在aDao只對a資料表做事,在bDao就只對b資料表做事

 

下列為dao的java檔,而在這邊也不多說了,就只是hibernate的用法

package grid.com.entity.dao;

import grid.com.entity.Game;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;

public class GameDao {
    @Resource
    private SessionFactory sessionFactory;

    public List<Game> doQueryGameList() {
        Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Game.class);
        return criteria.list();
    }

}

 

 

下列為dao-context的spring設定檔,這邊也不多說,可以直接去看Struts2+Spring+Hibernate架構配置教學(Hibernate+JNDI)下這篇喔

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.1.xsd ">



    <bean id="gameDao" class="grid.com.entity.dao.GameDao"/>


    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="hibernateProperties">
            <value>
                hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
                hibernate.show_sql=true
            </value>
        </property>
        <property name="annotatedClasses">
            <list>
                <value>grid.com.entity.Game</value>
            </list>
        </property>
    </bean>
    
    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean" >
        <property name="jndiName" value="java:comp/env/jdbc/grid"></property>
    </bean>
</beans>

 

 

 原本沒有要打這麼多的,因為最近很多人問,SSH的用法,所以在這邊再講一次

如果有不懂的話,歡迎發問

下次預計發有分頁功能的jqGrid

 

 

 

 

arrow
arrow
    全站熱搜

    討厭鬼 發表在 痞客邦 留言(4) 人氣()