partially updated for version 2.1.0

Inhaltsverzeichnis

Services#

Application programmers access ztemplates functionality through services.

The services are accessed over the class ZTemplates.

The services are per thread and are stored in a ThreadLocale.

The setters are public so you can mock up the services for unit-testing. Services are lazily created at most once per request. Costly services (like ZIClasspathService which scans the classpath) should cache the expensive data structures in a ServletContext attribute.

The following services are available in ztemplates-webapps:

See JavaDocs for details.

  • ZIServletService gives access to the servlet api objects, like request, response, as well as to render output to the response, create urls.
  • ZIRenderService is used to render other pojos, so you can manually include or process the rendered output of other pojos
  • ZIActionService is used to create refactoring safe Urls to action pojos
  • ZIFormService provides utilities to process forms
  • ZIClassPathService gives access to auto-discovery features, allows to search the classpath by annotation or by isAssignableFrom
  • SecurityService Adds J2EE role-based security to your webapp, in conjunction with the @ZSecure annotation.
  • EncryptionService gives you central access to strong encryption. Useful if you want to make your urls tamper-proof.
  • ExceptionService lets you handle exceptions in a custom manner

Customizing services#

Services are defined as interfaces. You can specify your own implementation or add your own service by adding your own service factory to the application.

You can do this in a ServletContextListener like follows:



public class YourContextListener implements ServletContextListener
{
  public void contextInitialized(ServletContextEvent ev)
  {
        ZIApplicationRepository applicationRepository = new ZApplicationRepository(ev.getServletContext());
        ZApplication application = applicationRepository.getApplication();

        ZServiceFactoryWrapper wrapper = new ZServiceFactoryWrapper(application.getServiceFactory())
        {
            @Override
            public ZIExceptionService createExceptionService(ZIServiceRepository repo, 
                                                             HttpServletRequest request, 
                                                             HttpServletResponse response)
            {
              return new YourExceptionService();
            }
        };
        application.setServiceFactory(wrapper);
   }
}

Install the listener by adding this to your web.xml. Be sure to put this after the ztemplates listeners, because they will initialize the application.


  <listener>
    <listener-class>
	com.yourcompany.YourContextListener 
    </listener-class>
  </listener>


Since 0.9.9.7 the signature has changed, so the request and response objects are passed to the errorhandler too.


  static final Logger log = Logger.getLogger(ExceptionService.class);


  @Override
  public void handle(HttpServletRequest req, HttpServletResponse resp, Throwable t)
  {
    try
    {
      Object errorView = ...:
      ZTemplates.getServletService().render(errorView);
    }
    catch (Exception e)
    {
      log.error("Error while processing error", e);
      try
      {
        resp.getWriter()
            .append(...);
      }
      catch (IOException e1)
      {
        log.error("", e);
         //give up...
      }
    }
  }

Neuen Anhang hinzuf�gen

Du bist nicht autorisiert, Anh�nge zu dieser Seite hochzuladen.
« Diese Seite (Version-) wurde zuletzt am 22-Dez-2013 13:18 von gerdziegler.de ge�ndert.