hi martin,

instead of resolving the path to your resource manually you can
use the asset service (useful when thinking of portlets)

i wrote a stylesheet component myself that works like your
script component :) ... and i enjoyed writing it.


public class Script {

    @Inject
    private AssetSource assetSource; 


    @BeginRender
    boolean renderMessage( MarkupWriter writer ) {
       ....
       Asset script = assetSource.findAsset(null, _src, null);
       ....
       writer.element( "script",
                "type", _type,
                "src", script.toClientUrl())
}

@Component(parameters={"src=context:js/mainFunction.js"})
Script script;
 

g,
kris
 







Martin Grotzke <[EMAIL PROTECTED]> 
26.05.2007 14:40
Bitte antworten an
"Tapestry users" <users@tapestry.apache.org>


An
Tapestry users <users@tapestry.apache.org>
Kopie

Thema
T5 Script component [WAS: Re: T5 page lifecycle]






thanx, good to know that.

Although, I prefer having a template that can be further developed by
page designers, so I wrote a Script component that can be used like
this:

<script type="text/javascript" t:type="script" 
src="js/main_functions.js"/>

The script component class:

public class Script {
 
    @Inject
    private Request _request;
    @Parameter(required = true, defaultPrefix="literal")
    private String _type;
    @Parameter(required = true, defaultPrefix="literal")
    private String _src;

    @BeginRender
    boolean renderMessage( MarkupWriter writer ) {
        writer.element( "script",
                "type", _type,
                "src", _request.getContextPath() + "/" + _src );
        writer.end();
        return false;
    }
 
}

Is there anything that could be improved?

Btw: I really love how easy it is to write a component in T5, you
just have to do what you want, nothing more - really, really nice!!

Cheers,
Martin



On Fri, 2007-05-25 at 10:13 -0700, Howard Lewis Ship wrote:
> Yes, you can.  The AssetSource service is public, so you can ask it for 
a
> dynamically determined service.
> 
> In 5.0.5 snapshot, you can do the following:
> 
> @Inject
> private Request _request;
> 
> public Request getRequest() { return _request; }
> 
> public String getLibraryPath() { return ... }
> 
> And in the template ...
> 
> <body>
> <script type="text/javascript" 
src="${request.contextPath}/${libraryPath}"/>
>  ...
> 
> 
> 5.0.5-SNAPSHOT supports expansions inside attributes, even of 
non-component
> elements, and you can do some simple string-assembly inline.  What this
> doesn't do is ensure that the library exists, or handle localization of 
the
> library (perhaps more relevant for an image than a JavaScript library).
> 
> 
> 
> On 5/25/07, Martin Grotzke <[EMAIL PROTECTED]> wrote:
> >
> > On Fri, 2007-05-25 at 07:54 -0700, Howard Lewis Ship wrote:
> > > There isn't a component, but you can in your page or component 
class:
> > >
> > > @Inject @Path("context:js/main_functions.js")
> > > private Asset _library;
> > is it possible to set the "js/main_functions.js" dynamically
> > via java, or retrieve it from a properties file (setting it via
> > java would be preferred)?
> >
> > thx && cheers,
> > martin
> >
> >
> > >
> > > @Environmental
> > > private PageRenderSupport _renderSupport;
> > >
> > > void beginRender() {
> > >   _renderSupport.addScriptLink(_library);
> > > }
> > >
> > >
> > > ... yes this can/should be wrapped up into a more convienient 
component.
> > > I've also added a JIRA suggestion for an annotation to take care of 
this
> > as
> > > well.
> > >
> > > On 5/25/07, Martin Grotzke <[EMAIL PROTECTED]> wrote:
> > > >
> > > > On Thu, 2007-05-24 at 16:36 -0700, Howard Lewis Ship wrote:
> > > > > Need an ls -lR of src/main/webapp
> > > > >
> > > > > I suspect you have a link to a .js file that doesn't exist.  If 
it
> > did
> > > > > exist, the request would be passed off to the servlet container.
> > Since
> > > > it
> > > > > doesn't, and it looks like a Tapestry page request, it's being
> > passed
> > > > into
> > > > > Tapestry.
> > > > Howard, that was the case. The url was /myapp/search/ipod (page
> > Search)
> > > > and the template contained
> > > > <script type="text/javascript" 
src="js/main_functions.js"></script>
> > > >
> > > > Changing this to
> > > > <script type="text/javascript"
> > src="/myapp/js/main_functions.js"></script>
> > > >
> > > > fixes the problem.
> > > >
> > > > Is there a component that can produce this script tag with a 
correctly
> > > > prefixed src attribute?
> > > >
> > > > Thx && cheers,
> > > > Martin
> > > >
> > > >
> > > > >
> > > > > On 5/24/07, Martin Grotzke <[EMAIL PROTECTED]> wrote:
> > > > > >
> > > > > > On Thu, 2007-05-24 at 10:42 -0700, Howard Lewis Ship wrote:
> > > > > > > I suspect there's a conflict between your page name, and a
> > folder of
> > > > > > your
> > > > > > > web application.  How about an ls -lR of your context 
folder?
> > > > > > Is the context folder what is specified with the context-param
> > > > > > "tapestry.app-package"?
> > > > > >
> > > > > > Then here it is:
> > > > > >
> > > > > >
> > > > > >
> > > >
> > 
===============================================================================
> > > > > > [EMAIL PROTECTED] stealthshop-tapestry5]$ ls -lR
> > > > > > src/main/java/org/company/app/
> > > > > > src/main/java/org/company/app/:
> > > > > > total 24
> > > > > > drwxrwxr-x 3 grotzke grotzke 4096 May 22 01:17 business
> > > > > > drwxrwxr-x 3 grotzke grotzke 4096 May 22 01:17 model
> > > > > > drwxrwxr-x 2 grotzke grotzke 4096 May 22 01:19 pages
> > > > > > drwxrwxr-x 2 grotzke grotzke 4096 May 21 18:46 services
> > > > > > drwxrwxr-x 3 grotzke grotzke 4096 May 22 03:33 tapestry
> > > > > > drwxrwxr-x 3 grotzke grotzke 4096 May 22 01:28 util
> > > > > >
> > > > > > src/main/java/org/company/app/business:
> > > > > > total 28
> > > > > > -rw-rw-r-- 1 grotzke grotzke  2598 May 22 01:17
> > > > > > SearchServiceDummyImpl.java
> > > > > > -rw-rw-r-- 1 grotzke grotzke  1487 May 22 01:17 
SearchService.java
> > > > > > -rw-rw-r-- 1 grotzke grotzke  2261 May 22 01:17
> > > > > > SearchServiceSolrImpl2.java
> > > > > > -rw-rw-r-- 1 grotzke grotzke 14338 May 24 01:21
> > > > SearchServiceSolrImpl.java
> > > > > >
> > > > > > src/main/java/org/company/app/model:
> > > > > > total 20
> > > > > > -rw-rw-r-- 1 grotzke grotzke 2679 May 22 01:17
> > FacetConstraint.java
> > > > > > -rw-rw-r-- 1 grotzke grotzke 1572 May 22 01:17 FacetItem.java
> > > > > > -rw-rw-r-- 1 grotzke grotzke 1829 May 22 01:17 Facet.java
> > > > > > -rw-rw-r-- 1 grotzke grotzke 3088 May 22 03:34 Product.java
> > > > > > -rw-rw-r-- 1 grotzke grotzke  733 May 22 01:17 
SearchResult.java
> > > > > >
> > > > > > src/main/java/org/company/app/pages:
> > > > > > total 12
> > > > > > -rw-rw-r-- 1 grotzke grotzke 7035 May 24 18:47 Search.java
> > > > > > -rw-rw-r-- 1 grotzke grotzke  805 May 24 00:57 Start.java
> > > > > >
> > > > > > src/main/java/org/company/app/services:
> > > > > > total 4
> > > > > > -rw-rw-r-- 1 grotzke grotzke 3455 May 21 18:46 AppModule.java
> > > > > >
> > > > > > src/main/java/org/company/app/tapestry:
> > > > > > total 4
> > > > > > drwxrwxr-x 2 grotzke grotzke 4096 May 22 03:33 util
> > > > > >
> > > > > > src/main/java/org/company/app/tapestry/util:
> > > > > > total 4
> > > > > > -rw-rw-r-- 1 grotzke grotzke 1185 May 22 03:38 AssetImpl.java
> > > > > >
> > > > > > src/main/java/org/company/app/util:
> > > > > > total 20
> > > > > > -rw-rw-r-- 1 grotzke grotzke 14839 May 22 01:28
> > CollectionUtil.java
> > > > > > -rw-rw-r-- 1 grotzke grotzke  2522 May 22 01:28 Pair.java
> > > > > >
> > > > > >
> > > >
> > 
===============================================================================
> > > > > >
> > > > > >
> > > > > > And this is the output of "ls -lR src/main/webapp/WEB-INF/":
> > > > > > src/main/webapp/WEB-INF/:
> > > > > > total 20
> > > > > > -rw-rw-r-- 1 grotzke grotzke 2113 May 22 01:16
> > applicationContext.xml
> > > > > > -rw-rw-r-- 1 grotzke grotzke 5076 May 24 02:27 Search.html
> > > > > > -rw-rw-r-- 1 grotzke grotzke 1689 May 22 01:42 Start.html
> > > > > > -rw-rw-r-- 1 grotzke grotzke 1584 May 22 02:13 web.xml
> > > > > >
> > > > > >
> > > > > > Thanx,
> > > > > > Martin
> > > > > >
> > > > > >
> > > > > > >
> > > > > > > Does main_functions.js exist and if so, where?
> > > > > > >
> > > > > > > On 5/24/07, Martin Grotzke <[EMAIL PROTECTED]> 
wrote:
> > > > > > > >
> > > > > > > > Hello,
> > > > > > > >
> > > > > > > > I have a simple search page with an input field (query) 
and a
> > > > submit
> > > > > > > > button.
> > > > > > > >
> > > > > > > > The submit implementation returns a page link to receive a
> > > > > > bookmarkable
> > > > > > > > page (with pagename and query param), and the
> > onActivate(String)
> > > > then
> > > > > > > > performs a search for the query.
> > > > > > > >
> > > > > > > > Although, the onActivate(String) is called twice, and 
another
> > > > > > onActivate
> > > > > > > > method that I do have, is also invoked afterwards (this
> > intended
> > > > for
> > > > > > > > other links on the page, and is also performing a search).
> > > > > > > > The other onActivate method is called with 
"main_functions.js"
> > as
> > > > an
> > > > > > > > argument, which is simply a <script type=... element in 
the
> > header
> > > > of
> > > > > > > > the page template...
> > > > > > > >
> > > > > > > > Am I using T5 wrong, or is this a problem of T5?
> > > > > > > >
> > > > > > > > See the following log, the page class and the template for
> > more
> > > > info.
> > > > > > > >
> > > > > > > > Thanx in advance,
> > > > > > > > cheers,
> > > > > > > > Martin
> > > > > > > >
> > > > > > > >
> > > > > > > > ==== log output for a submit
> > > > > > ==========================================
> > > > > > > >
> > > > > > > > [INFO ] 2007-05-24 18:30:04,727 btpool0-2
> > > > > > org.comp.app.pages.Search.submit
> > > > > > > > :
> > > > > > > > Received submit with query ipod, returning pageLink now.
> > > > > > > >
> > > > > > > > [INFO ] 2007-05-24 18:30:04,895 btpool0-2
> > > > > > > > org.comp.app.pages.Search.onActivate:
> > > > > > > > [1] Got invoked with query ipod
> > > > > > > >
> > > > > > > > [INFO ] 2007-05-24 18:30:04,896 btpool0-2
> > > > > > > > org.comp.app.pages.Search.performSearch:
> > > > > > > > Starting search...
> > > > > > > >
> > > > > > > > [INFO ] 2007-05-24 18:30:05,519 btpool0-2
> > > > > > > > org.comp.app.pages.Search.onActivate:
> > > > > > > > [1] Got invoked with query ipod
> > > > > > > >
> > > > > > > > [INFO ] 2007-05-24 18:30:05,520 btpool0-2
> > > > > > > > org.comp.app.pages.Search.performSearch:
> > > > > > > > Starting search...
> > > > > > > >
> > > > > > > > [INFO ] 2007-05-24 18:30:05,616 btpool0-2
> > > > > > > > org.comp.app.pages.Search.onActivate:
> > > > > > > > [2] Got invoked with query js and facetConstraints
> > > > main_functions.js
> > > > > > > >
> > > > > > > > [INFO ] 2007-05-24 18:30:05,617 btpool0-2
> > > > > > > > org.comp.app.pages.Search.performSearch:
> > > > > > > > Starting search...
> > > > > > > >
> > > > > > > >
> > > > > > > > ==== page class (with relevant methods)
> > > > > > > > =========================================
> > > > > > > >
> > > > > > > > public class Search {
> > > > > > > >
> > > > > > > >     private static final Log LOG = LogFactory.getLog(
> > Search.class);
> > > > > > > >
> > > > > > > >     @Inject
> > > > > > > >     private ComponentResources _componentResources;
> > > > > > > >
> > > > > > > >     /* [snip, several properties] */
> > > > > > > >
> > > > > > > >     @Persist( "flash" )
> > > > > > > >     private String _query;
> > > > > > > >
> > > > > > > >     public void onActivate(String query) {
> > > > > > > >         LOG.info( "[1] Got invoked with query " + _query 
);
> > > > > > > >         if ( _query != null ) {
> > > > > > > >             _query = query;
> > > > > > > >             performSearch();
> > > > > > > >         }
> > > > > > > >     }
> > > > > > > >
> > > > > > > >     public void onActivate(String query, List<String>
> > > > > > facetConstraints) {
> > > > > > > >         LOG.info( "[2] Got invoked with query " + _query + 
"
> > and
> > > > > > > > facetConstraints " + CollectionUtil.toCSV( 
facetConstraints )
> > );
> > > > > > > >         _query = query;
> > > > > > > >         if ( facetConstraints != null ) {
> > > > > > > >             for( String facetConstraint : facetConstraints 
) {
> > > > > > > >                 /* [snip] */
> > > > > > > >             }
> > > > > > > >         }
> > > > > > > >         performSearch();
> > > > > > > >     }
> > > > > > > >
> > > > > > > >     @OnEvent(value="submit")
> > > > > > > >     public Link submit() {
> > > > > > > >         LOG.info( "Received submit with query " + _query + 
",
> > > > > > returning
> > > > > > > > pageLink now." );
> > > > > > > >         return _componentResources.createPageLink(
> > > > > > > > getClass().getSimpleName(), new Object[]{ _query } );
> > > > > > > >     }
> > > > > > > >
> > > > > > > >     public void performSearch() {
> > > > > > > >         LOG.info( "Starting search..." );
> > > > > > > >         final SearchResult result = _searchService.search(
> > > > getQuery(),
> > > > > > > > _selectedFacetConstraints,
> > > > > > > >                 null, 10, 5 );
> > > > > > > >
> > > > > > > >         _numFound = result.getNumFound();
> > > > > > > >
> > > > > > > >         _products = result.getProducts();
> > > > > > > >
> > > > > > > >         _facets = result.getFacets();
> > > > > > > >     }
> > > > > > > >
> > > > > > > >     /* [snip] */
> > > > > > > >
> > > > > > > > }
> > > > > > > >
> > > > > > > >
> > > > > > > > ==== page template (short version)
> > > > > > =====================================
> > > > > > > >
> > > > > > > > <html xmlns:t="
> > > > http://tapestry.apache.org/schema/tapestry_5_0_0.xsd";>
> > > > > > > > <head>
> > > > > > > >     <script type="text/javascript"
> > > > > > src="js/main_functions.js"></script>
> > > > > > > > </head>
> > > > > > > > <body>
> > > > > > > >     <t:form t:id="search_form">
> > > > > > > >         <t:textfield t:id="q" t:value="query" />
> > > > > > > >         <input type="submit" id="submit" value="suchen"/>
> > > > > > > >     </t:form>
> > > > > > > > </body>
> > > > > > > >
> > > > > > > >
> > > > > > > >
> > > > > > > > --
> > > > > > > > Martin Grotzke
> > > > > > > > Dipl.-Inf.
> > > > > > > >
> > > > > > > > freiheit.com technologies gmbh
> > > > > > > > Straßenbahnring 22 / 20251 Hamburg, Germany
> > > > > > > > fon       +49 (0)40 / 890584-0
> > > > > > > > fax       +49 (0)40 / 890584-20
> > > > > > > > HRB Hamburg 70814
> > > > > > > >
> > > > > > > > eb0e 645c 9730 c8a3 ee2f  1b9a 5de5 21cb c259 fe34
> > > > > > > > Geschäftsführer: Claudia Dietze, Stefan Richter, Jörg 
Kirchhof
> > > > > > > >
> > > > > > > >
> > > > > > >
> > > > > > >
> > > > > > --
> > > > > > Martin Grotzke
> > > > > > http://www.javakaffee.de/blog/
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > > --
> > > > Martin Grotzke
> > > > http://www.javakaffee.de/blog/
> > > >
> > > >
> > >
> > >
> > --
> > Martin Grotzke
> > http://www.javakaffee.de/blog/
> >
> >
> 
> 
-- 
Martin Grotzke
http://www.javakaffee.de/blog/

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to