Tapestry 怎样从数据库中保存的图片显示出来。

传个ID给Service让Service输出图片。

类似JSP或SERVELT。。

我仿照TAPESTRY中的AssetService写了个 PhotoService.

java代码: 


package outsourcing. tapestry. utils;

import java. io. IOException;

import javax. servlet. ServletContext;
import javax. servlet. ServletException;

import org. apache. tapestry. ApplicationRuntimeException;
import org. apache. tapestry. IComponent;
import org. apache. tapestry. IRequestCycle;
import org. apache. tapestry. Tapestry;
import org. apache. tapestry. engine. AbstractService;
import org. apache. tapestry. engine. IEngineServiceView;
import org. apache. tapestry. engine. ILink;
import org. apache. tapestry. request. ResponseOutputStream;
import org. springframework. context. ApplicationContext;

import outsourcing. databean. AgentInformation;
import outsourcing. facade. OutSourcingFacade;
import outsourcing. tapestry. Engine;

/**
* @author yin.deng
*
* TODO 要更改此生成的类型注释的模板,请转至 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/

public class PhotoService extends AbstractService {

    public static final String SERVICE_NAME = "photo";

    public ILink getLink (IRequestCycle cycle, IComponent component,
            Object [ ] parameters ) {
        if (Tapestry. size (parameters ) != 1 )
            throw new ApplicationRuntimeException (Tapestry. format (
                    "service-single-parameter", getName ( ) ) );

        // Service is stateless

        return constructLink (cycle, getName ( ), null, parameters, false );
    }

    /*
     * (非 Javadoc)
     *
     * @see org.apache.tapestry.engine.AbstractService#getName()
     */

    public String getName ( ) {
        // TODO 自动生成方法存根
        return SERVICE_NAME;
    }

    public void service (IEngineServiceView engine, IRequestCycle cycle,
            ResponseOutputStream output ) throws ServletException, IOException {
        Object [ ] parameters = getParameters (cycle );

        if (Tapestry. size (parameters ) != 1 )
            throw new ApplicationRuntimeException (Tapestry. format (
                    "service-single-parameter", getName ( ) ) );

        String agentid = ( String ) parameters [ 0 ];

        ServletContext servletContext = cycle. getRequestContext ( ). getServlet ( )
                . getServletContext ( );

        writeAssetContent (engine, cycle, output, agentid, servletContext );
    }

    /** @since 2.2 * */

    private void writeAssetContent (IEngineServiceView engine,
            IRequestCycle cycle, ResponseOutputStream output, String agentid,

            ServletContext servletContext ) {
        // Getting the content type and length is very dependant
        // on support from the application server (represented
        // here by the servletContext).

        String contentType = "image/jpeg";
        AgentInformation agentInformation = getOutSourcingFacade (cycle )
                . findAgentById ( Long. valueOf (agentid ) );

        output. setContentType (contentType );

        // Disable any further buffering inside the ResponseOutputStream

        try {
            output. forceFlush ( );
            output. write (agentInformation. getAiTopo ( ) );
        } catch ( IOException e ) {
            // TODO 自动生成 catch 块
            e. printStackTrace ( );
        }

    }

    private OutSourcingFacade getOutSourcingFacade (IRequestCycle cycle ) {
        ApplicationContext applicationContext = this
                . getApplicationContext (cycle );
        return (OutSourcingFacade ) applicationContext
                . getBean ("outSourcingFacade" );
    }

    public ApplicationContext getApplicationContext (IRequestCycle cycle ) {
        Engine engine = (Engine ) cycle. getEngine ( );
        return engine. getApplicationContext ( );
    }
}



然后注册到.application中。

java代码: 


<?xml version=" 1. 0" encoding="UTF- 8"?>
<!DOCTYPE application PUBLIC
  "- //Apache Software Foundation//Tapestry Specification 3.0//EN"
  "http: //jakarta.apache.org/tapestry/dtd/Tapestry_3_0.dtd">

<application name="outsourcing"
    engine- class="outsourcing. tapestry. Engine">

    <property name="org. apache. tapestry. output-encoding" value="gb2312"/>
    <property name="org. apache. tapestry. template-encoding" value="gb2312"/>
    <property name="org. apache. tapestry. visit- class"
        value="outsourcing. tapestry. Visit"/>
        <service name="report" class="outsourcing. report. ReportService"/>
    <service name="photo" class="outsourcing. tapestry. utils. PhotoService"/>
    <library id="contrib" specification-path="/org/apache/tapestry/contrib/Contrib. library"/>
    <library id="ext" specification-path="/outsourcing/tapestry/jwc/ext. library"/>
</application>




在tapestry页面的调用

java代码: 


                <input jwcid="@ Any" element="img" src= 'ognl:"/galileo/app?service=photo&sp=S" + item.aiId' width=" 600" height=" 400"/>



可以自己再扩展功能。。

你可能感兴趣的:(数据库,service,application,tapestry,Parameters,import)