Wt (web toolkit)
| Original author(s) | Emweb | 
|---|---|
| Initial release | 1.0.0 / Released December 2005 | 
| Stable release | 3.3.6
   / July 15, 2016[1] | 
| Operating system | Any | 
| Type | Web application framework | 
| License | Dual License: GNU General Public License or Commercial License | 
| Website | www | 
Wt (pronounced "witty") is an open source widget-centric web-application framework for the "C++" programming language, developed by "Emweb". It has an API (application programming interface) that resembles the "C++" desktop-application library "Qt", also using a widget-tree and an event-driven "Signal / Slot" programming model.
The goal of the library is to benefit from the stateful component model used in desktop-applications APIs, applied to web-development,—instead of the traditional MVC-model (model–view–controller): rather than using MVC at the level of a web-page—MVC is pushed to the level of individual components.
While the library uses a desktop-application development model, it does support web-specific features—including:
- semantic URLs (uniform (web)-resource locator);
- Browser history navigation support.
A unique feature of the library is its abstraction layer of the browser rendering model. The library uses "Ajax" for communicating with "Ajax"-capable browsers, while using plain "HTML"-form post-backs for other user agents. Using a progressive bootstrap-method, the user interface is initially rendered as a plain "HTML"-document; and for "Ajax"-capable browsers—it is automatically upgraded to use "Ajax" for increased interactivity. In this way, it is:
- The only server-side framework implementing the strategy of progressive enhancement automatically;
- The only "Ajax"-framework with search engine optimization (SEO) qualities.
Because of the popularity of "C"/"C++" in embedded system environments—the library is often used in such devices, and has been highly optimized for performance (as a consequence).
Major features
- Automatic graceful degradation and progressive enhancement;
- Supports server-initiated events ("Comet");
- A unified rendering API ("SVG" / "HTML5" canvas / "VML");
- Both client-side and server-side validation;
- Various security features to avoid Cross-site scripting and CSRF (cross-site request forgery) vulnerabilities;
- Includes a compact, modern "C++" ORM-layer ("Wt::Dbo");
- Uses a "WebSockets" networking protocol if available, for communication between client and server, with fallbacks to "Ajax" or plain web-pages.
You can see a complete feature list on the project homepage, for a more detailed overview.
Code example
The "Hello World!" example—full source code:
#include <Wt/WApplication>
#include <Wt/WBreak>
#include <Wt/WContainerWidget>
#include <Wt/WLineEdit>
#include <Wt/WPushButton>
#include <Wt/WText>
// c++0x only, for std::bind
// #include <functional>
using namespace Wt;
/*
 * A simple "Hello, World!" application-class demonstrates: 
 * How to 
 * react to events, read input, give feed-back?
 */
class HelloApplication : public WApplication
{
public:
  HelloApplication(const WEnvironment& env);
private:
  WLineEdit *nameEdit_;
  WText *greeting_;
  void greet();
};
/*
 * The "env"-argument contains information about 
 * a new session and an initial request. It must be passed 
 * to the "WApplication"-constructor,--
 * so it is typically also an argument for your custom
 * application-constructor.
*/
HelloApplication::HelloApplication(const WEnvironment& env)
  : WApplication(env)
{
  setTitle("Hello world");                               // application title
  root()->addWidget(new WText("Your name, please? "));   // show some text
  nameEdit_ = new WLineEdit(root());                     // allow text input
  nameEdit_->setFocus();                                 // give focus
  WPushButton *button
    = new WPushButton("Greet me.", root());              // create a button
  button->setMargin(5, Left);                            // add 5 pixels margin
  root()->addWidget(new WBreak());                       // insert a line-break
  greeting_ = new WText(root());                         // empty text
  /*
   * Connect signals with slots
   *
   * - simple "Wt"-way
   */
  button->clicked().connect(this, &HelloApplication::greet);
  /*
   * - using an arbitrary function object (binding values with boost::bind())
   */
  nameEdit_->enterPressed().connect
    (boost::bind(&HelloApplication::greet, this));
  /*
   * - using a c++0x lambda:
   */
  // b->clicked().connect(std::bind([=]() { 
  //       greeting_->setText("Hello there, " + nameEdit_->text());
  // }));
}
void HelloApplication::greet()
{
  /*
   * Update the text, using text-input into the "nameEdit_"-field.
   */
  greeting_->setText("Hello there, " + nameEdit_->text() + "!");
}
WApplication *createApplication(const WEnvironment& env)
{
  /*
   * You could read information from the environment to decide: 
   * Whether the User has a permission to start a new application?
   */
  return new HelloApplication(env);
}
int main(int argc, char **argv)
{
  /* The method may set up some shared resources, 
   * but should then start a server-application ("FastCGI" or "httpd"), to: 
   * 1. Listen for requests; 
   * 2. Handle all of the application's life-cycles.
   *
   * The last "WRun"-argument specifies 
   * a new application-objects instantiating function--
   * to execute when 
   * a new user surfs to the "Wt"-application 
   * and the library has a negotiated browser support. 
   * (This function should return a newly instantiated application-object.)
   */
  return WRun(argc, argv, &createApplication);
}
See also
- Comparison of web-frameworks;
- "JWt" ("Java" web-toolkit) ("JWt"-homepage), a native "Java"-version obtained using automated "C++"-to-"Java" translation;
- "Tntnet".
References
- Article in Dr Dobb's Journal, Feb 2008
- Introductory article at CodeGuru, Jun 2008
External links
- Wt Project Homepage;
- Saetta Web Server—for web-sites in "C" / "C++", with and without "Wt".