Wicket Dynamic Image URL

后端 未结 3 1854
耶瑟儿~
耶瑟儿~ 2020-12-14 14:11

Short question: I need to turn a dynamic image pulled from a database into a URL without adding a component to the displaying page (such as using a NonCachingImage) using Wi

3条回答
  •  伪装坚强ぢ
    2020-12-14 14:14

    Here's my example that does the same for a dynamically compiled list of identifiers, served up as a shared resource with a static URL..

    public class WicketApplication extends WebApplication {
        ...snip...
        @Override
        protected void init() {
            //Spring
            addComponentInstantiationListener(new SpringComponentInjector(this));
    
            //Register export lists as shared resources
            getSharedResources().putClassAlias(ListInitializer.class, "list");
            new ListInitializer().init(this);
        }
    

    And my ListInitializer that registers the resources as DBNAME_SUBSELECTION1(2/3/..)

    public class ListInitializer implements IInitializer {
        public ListInitializer() {
            InjectorHolder.getInjector().inject(this);
        }
    
        @SpringBean
        private DatabankDAO dbdao;
    
        @Override
        public void init(Application application) {
            //For each databank
            for (Databank db : dbdao.getAll()) {
                String dbname = db.getName();
                //and all collection types
                for (CollectionType ct : CollectionType.values()) {
                    //create a resource
                    Resource resource = getResource(dbname, ct);
                    //and register it with shared resources
                    application.getSharedResources().add(this.getClass(), dbname + '_' + ct, null, null, resource);
                }
            }
        }
    
        @SpringBean
        private MyApp   MyApp;
    
        public Resource getResource(final String db, final CollectionType collectionType) {
            return new WebResource() {
                @Override
                public IResourceStream getResourceStream() {
                    List entries = MyApp.getEntries(db, collectionType.toString());
                    StringBuilder sb = new StringBuilder();
                    for (String entry : entries) {
                        sb.append(entry.toString());
                        sb.append('\n');
                    }
                    return new StringResourceStream(sb, "text/plain");
                }
    
                @Override
                protected void setHeaders(WebResponse response) {
                    super.setHeaders(response);
                    response.setAttachmentHeader(db + '_' + collectionType);
                }
            }.setCacheable(false);
        }
    }
    

    I'm sorry but I can't seem to find the tutorial I used to set this up anymore, but it should be evident how this relates to the above example and can be adjusted to do the same for images.. (Sorry for the sparse explanation, if it's still unclear I could come back and edit my answer)

提交回复
热议问题