stapler HTTP请求处理引擎

开源网站开源项目 24-04-19 14:10:54

Stapler is a library that "staples" your application objects to URLs, making it easier to write web applications. The core idea of Stapler is to automatically assign URLs for your objects, creating an intuitive URL hierarchy.

Normally, most of your JSP and servlet goes like this; first, you use parameters and extra path info to locate the object to work with, then use some more parameters and such to perform some operations against it (or perhaps render HTML). Stapler takes care of this first portion by dispatching requests to that object you'll be working with.

Suppose you are building a web application like the system of java.net, where you got projects, mailing lists, documents and files section, and etc. The following figure shows how Stapler works.

The left half shows your code. OOP lets you model those concepts straight-forwardly into classes that refer to each other. Through reflection, Stapler assigns URLs to your application objects. The root of your application object gets the root URL/. The object you can access withroot.getProject("stapler")would be assigned to the URL/project/stapler. In this way, your object model directly turns into the URL hierarchy, as partially shown in purple arrows.

JSPs and servlets

Most of the times JSPs are used to show an object in your application. Stapler allows you to associate JSPs to your application classes, as shown in green circles in the above figure. Stapler weaves those JSPs into the URL hierarchy, and when they get requested, Stapler sets up the environment such that they can access the corresponding application object very easily, by the "it" variable (like ${it.name}).

Similarly, often you want to run some Java code when a certain URL is requested, as shown in red circles in the above figure. For example, when/project/stapler/deleteis requested, you'd like to delete the project and redirect the user to/. With Stapler, this is as easy as defining a method on your application objects. Stapler invokes the method on the right object.

Problems That Stapler Solves

Servlet (and technologies like JSP/JSF that builds on top of it) can only bind request URLs into your static resources, JSPs, and/or servlets in a very limited way. You can even tell that from the java.net system. For example, the URL to view the archive of a mailing list is/servlets/SummarizeList?listName=announce, the URL to search the archive if/servlets/SearchList?listName=announce, and the URL to manage it is/servlets/MailingListEdit?list=announce.

This has some problems.

Relative URLs are useless

Ordinary servlet/JSP prevents you from using the URL hierarchy to organize your application. In the java.net system, if you want to generate a link from the archive to the search page, you have to put the whole URL including all the parameters. You'll be writing something like/servlets/SearchList?list=${listName}. As your application gets more complicated, the number of parameters will increase, making your application harder and harder to maintain.

Stapler allows you to use URLs likelist/announce/summarize,list/announce/search, andlist/announce/edit. Thus a link from the summary page to the search is simplysearch.

A lot of boiler-plate code

Most of the times, the first thing your JSPs and servlets need to do is to locate the object that you'll be working with. This is tedious. Even worse, it needs to be repeated in many places.

Stapler takes care of this for you. When your JSP is invoked, you can access the target object by the variable "it". Or when your action method (the equivalent of the servlet) is invoked, it is invoked on the target object. This cuts down a lot of boring boiler-plate code in your application.

Recursive Structure

Servlets can't handle recursive directory structure very well. In the above figure, I have recursiveFolderclass that has two JSPs each. The only way to make this work in servlet is to assign unique IDs to each folder and define those JSPs at the top-level. The actual URLs will be very different from the natural tree structure.

Stapler maps this very naturally to URLs.

[stapler HTTP请求处理引擎]相关推荐

Faker 生成假数据的 PHP 库

Faker 是一个为你生成假数据的PHP库。当你需要填充一个测试数据库,或为你的web应用生成测试数据时,它能……...

FastAPI Python 高性能 Web 框架

FastAPI 是一个高性能 Web 框架,用于构建 API。 主要特性: 快速:非常高的性能,与 NodeJS 和 Go 相当 ……...

kvs key-value存储系统

kvs是一个基于内存的key-value存储系统。 kvs与memcahce的功能很相似,但是相对memcache来说更容易使用。……...

Linaro ARM芯片优化的系统

Linaro,一间非营利性质的开放源代码软件工程公司,主要的目标在于开发不同半导体公司系统单芯片(SoC)……...

bedrock 现代化 WordPress 堆栈

Bedrock 是现代化 WordPress 堆栈,帮助用户使用相关的开发工具和项目架构来构建 WordPress。 特性 使用 ……...

今日开源
  1. LayaAir HTML5 开源游戏引擎

    LayaAir 是 HTML5 开源引擎。 提供 Canvas 和 Webgl 同时渲染,如果 Webgl 不可用,则可自动切换到 Canvas 模式。引擎为高性能游戏设计,支持 AS,TS,JS 三种语言开发,一套代码三端齐发(Flash,HTML5,APP)。 La……

    开源软件 2024-05-02

  2. 深度翻译

    深度翻译

    简介深度翻译(英文名:Deepin Translator)是deepin 团队为开发的一款强大的翻译软件。深度翻译提供数十种语言互译,支持划出和取词,同时还支持图片取词,简洁的外观和操作,为您提供从所未有的翻译体验。深度翻译……

    开源软件 2024-05-02

  3. Spoon Android 测试工具

    Spoon Android 测试工具

    Spoon 是一个 Android 平台的分布式仪表 (Instrumentation)测试项目。

    开源软件 2024-05-02

返回顶部小火箭