Vikings

          T3 里的圖片顯示

          例子參考:
          T3自帶的workbeanch里有
          1.ChartService class
          2.ChartAsset class
          3.IChartProvider的例子代碼

          思路:
          因為在DAO,service里返回的Info對象里已經(jīng)有圖片的屬性,
          類型是byte[]
          所以直接out.write(byte[])獲得圖片.

          題外:
          其中,AbstractService相比T4少了Webresponse屬性,
          不可以直接在IChartProvider里定義 
          public void sendDynaAsset(WebResponse response);
          然后在實現(xiàn)類里用這句 
          OutputStream out = response.getOutputStream(new ContentType("image/jpeg"));
          來操作out.write .
          T3里都在AbstractService里操作out, service()方法里就有參數(shù)ResponseOutputStream output

          代碼:
          1. ChartService
          public class ChartService extends AbstractService{

            
          public void service(
                  IEngineServiceView engine,
                  IRequestCycle cycle,
                  ResponseOutputStream output)
                  
          throws ServletException, IOException
              
          {

          .

            
          try
                  
          {
                      IChartProvider provider 
          = (IChartProvider) component;

                      
          byte[] bytes = provider.getImageBytes();

                      
          //默認規(guī)則,page頁面里若數(shù)據(jù)庫里沒有bytes,則返回null
                      if (bytes==null)
                          
          //顯示默認圖片
                          bytes = drawDefaultImage();         
                      
                      output.setContentType(
          "image/jpeg");

                      
          // I've seen a few bits of wierdness (including a JVM crash) inside this code.
                      
          // Hopefully, its a multi-threading problem that can be resolved
                      
          // by synchronizing.

                      
          synchronized (this)
                      
          {
                          output.write(bytes);
                      }

                  }


             }


          protected byte[] drawDefaultImage() {
                  
          int width = 102, height = 126;
                  BufferedImage image 
          = new BufferedImage(width, height,
                          BufferedImage.TYPE_INT_RGB);
                  Graphics g 
          = image.getGraphics();

                  g.setColor(Color.WHITE);
                  g.fillRect(
          00, width, height);

                  g.setColor(Color.RED);
                  g.setFont(
          new Font("Default", Font.PLAIN, 22));
                  g.drawString(
          "暫無照片"10, height / 2 - 10);

                  
          byte buffer[] = null;
                  
          try {
                      ByteArrayOutputStream out 
          = new ByteArrayOutputStream();
                      ImageIO.write(image, 
          "JPEG", out);
                      buffer 
          = out.toByteArray();
                  }
           catch (Exception e) {
                      log.info(
          "繪制默認圖片失敗");
                  }

                  
          return buffer;
              }

          }


          2. ChartAsset
          與workbeanch里的CharAsset一樣,沒有任何增刪
          public class ChartAsset extends AbstractAsset{
            
          private IEngineService _chartService;
            
          private IComponent _chartProvider;
           
          public ChartAsset(IRequestCycle cycle, IComponent chartProvider)
              
          {
                  
          super(nullnull);
                  
                  IEngine engine 
          = cycle.getEngine();

                  _chartService 
          = engine.getService(ChartService.SERVICE_NAME);
                  _chartProvider 
          = chartProvider;
              }


          }

          3. IChartProvider
          Service類回調(diào)接口
          public interface IChartProvider
          {
              
          public byte[] getImageBytes();
          }


          4.Page類
          需要實現(xiàn)IChartProvider回調(diào)接口的getImageBytes()方法
          public abstract SaryPage extends BasePage implements IChartProvider{
           
          private IAsset chartImageAsset;

              
          public IAsset getChartImageAsset() {
                  
          if (chartImageAsset == null)
                      chartImageAsset 
          = new ChartAsset(getRequestCycle(), this);

                  
          return chartImageAsset;
              }

           
          /**
               * This method is invoked by the service (in a seperate request cycle from
               * all the form handling stuff). The {
          @link #getChartImageAsset()} method
               * provides an {
          @link IAsset} that is handled by the {@link ChartService},
               * and the asset encodes the identity of this page.
               * 
               
          */

              
          public byte[] getImageBytes() {
                  
          if (getSary().getXp()!=null)
                      
          return getSary().getXp();
                  
          else
                      
          //默認規(guī)則,page頁面里若數(shù)據(jù)庫里沒有bytes,則返回null
                      return null;
              }


          }


          5.  Html頁面
          <img jwcid="@Image" width="102" height="126"  image="ognl:chartImageAsset"/>

          6. .application文件 注冊ChartService
          <application name="SaryProject" engine-class="cn.vikings.tapestry.common.SaryEngine">
              
           
          <service name="chart" class="cn.vikings.tapestry.common.chart.ChartService"/>

          </application>

          圖片顯示搞店. 就這么簡單.
          不用再很BT得傳一句HQL語句和圖片ID來,再用JDBC查一次數(shù)據(jù)庫.

          posted on 2007-10-29 16:56 Vikings 閱讀(696) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 岳西县| 兴隆县| 成都市| 于田县| 崇左市| 漾濞| 太谷县| 崇义县| 临泉县| 安阳县| 广丰县| 通榆县| 绥宁县| 义乌市| 英吉沙县| 日照市| 玉田县| 平武县| 漳平市| 阿荣旗| 天峻县| 松溪县| 奉节县| 连云港市| 伊吾县| 郸城县| 定边县| 涟水县| 大冶市| 兴仁县| 读书| 桐庐县| 福鼎市| 福建省| 山东省| 周宁县| 高州市| 洞口县| 贵溪市| 泗水县| 拜泉县|