forcemvc 0.3.7+1

Dart Force MVC #


Part of the Dart Force Framework.

Serverside MVC based implementation for Dart. Easy to setup and part of the dart force framework!

Walkthrough #

Use a server with dart very easily, create controllers with annotations ... similar to spring mvc.

First you will setup a new server.

WebServer server = new WebServer(wsPath: wsPath, port: port, host: host, buildPath: buildPath);

Then you use the on method to handle http requests.

server.on(url, (ForceRequest req, Model model) { /* logic */ }, method: "GET");

You can also use the annotation RequestMapping in a dart object

@RequestMapping(value: "/someurl", method: "GET")
void index(ForceRequest req, Model model)

You can also use the annotation @ModelAttribute to add an object to all the scopes in the methods. An @ModelAttribute on a method argument indicates the argument should be retrieved from the model. If not present in the model, the argument should be instantiated first and then added to the model. Once present in the model, the argument's fields should be populated from all request parameters that have matching names.

String someName() {
	return mv.getValue();

Then you register that object on the WebServer object.


Or you can annotate a class with @Controller and then it will be registered automatically in the force server.

class SomeObject {


Starting your web server #

You can do this as follow!


It is also possible to start a server with SSL possibilities.


ForceRequest #

ForceRequest is an extension for HttpRequest

forceRequest.postData().then((data) => print(data));

Interceptors #

You can define inteceptors as follow, the framework will pick up all the HandlerInterceptor classes or implementations.

class RandomInterceptor implements HandlerInterceptor {

  bool preHandle(ForceRequest req, Model model, Object handler) { return true; }
  void postHandle(ForceRequest req, Model model, Object handler) {}
  void afterCompletion(ForceRequest req, Model model, Object handler) {}

Path variables #

You can now use path variables in force mvc.

@RequestMapping(value: "/var/{var1}/other/{var2}/", method: "GET")
void pathvariable(ForceRequest req, Model model, String var1, String var2)

This is an alternative way how you can access path variables.


You can also use the annotation @PathVariable("name") to match the pathvariable, like below:

  @RequestMapping(value: "/var/{var1}/", method: "GET")
  String multivariable(req, Model model, @PathVariable("var1") variable) {}

Redirect #

You can instead of returning a view name, performing a redirect as follow:

@RequestMapping(value: "/redirect/")
String redirect(req, Model model) {
	return "redirect:/viewable/";

Asynchronous Controller #

In the controller you can have asynchronous methods to handle for example POST methods much easier.

On the ForceRequest object you have a method .async and his value is the return value that matters for the req.

When a method is asynchrounous you must return req.asyncFuture.

This is an example how you can use it.

@RequestMapping(value: "/post/", method: "POST")
Future countMethod(req, Model model) {
     req.getPostParams().then((map) {
       model.addAttribute("email", map["email"]);
     model.addAttribute("status", "ok");
     return req.asyncFuture;

Authentication #

You can now add the annotation @Authentication() to a controller class.

An authentication in force is following a strategy. You can set a strategy by extending the class SecurityStrategy.

class SessionStrategy extends SecurityStrategy {
  bool checkAuthorization(HttpRequest req, {data: null}) {
    HttpSession session = req.session;
    return (session["user"]!=null);
  Uri getRedirectUri(HttpRequest req) {
    var referer = req.uri.toString();
    return Uri.parse("/login/?referer=$referer");

And then add this strategy to the webserver.

server.strategy = new SessionStrategy();

Logging #

You can easily boostrap logging.


Force it #

Force it is a dependency injection package. You can use @Autowired and @bean and more in forcemvc find info here

Example #

You can find a simple example with a page counter implementation here - live demo


  • get more annotations and options for sending the response back
  • writing tests

Api documents #

You can find them here

Notes to Contributors #

Fork Dart Force MVC #

If you'd like to contribute back to the core, you can fork this repository and send us a pull request, when it is ready.

If you are new to Git or GitHub, please read this guide first.

Dart Force #

Realtime web framework for dart that uses force MVC source code

Twitter #

Follow us on twitter

Google+ #

Follow us on google+

Join our discussion group #

Google group

Changelog #

This file contains highlights of what changes on each version of the forcemvc package.

Pub version 0.3.7 #

  • General error handling with a HandlerExceptionResolver
  • Introducing the annotation @ExceptionHandler so you can use that in your @Controller classes when an error is happening.

Pub version 0.3.6+1 #

  • First steps in error handling

Pub version 0.3.6 #

  • Adding startSecure method, so you can start your webserver over SSL.

Pub version 0.3.5+1 #

  • Update package configuration

Pub version 0.3.5 #

  • Provide a class MockForceRequest to mock more easily ForceRequest in unittests
  • Upgrade of the unittest package and introducing of the mock package

Pub version 0.3.4+1 & 0.3.4+2 & 0.3.4+3

  • update rules for mustache rendering the view

Pub version 0.3.4 #

  • Add @RequestMapping on an object level possible

Pub version 0.3.3+1 #

  • Make @RequestParam working without a value

Pub version 0.3.3 #

  • Error logging when the server don't start
  • Add responseHooks into the web_server
  • Using variable name of the annotated @RequestParam when value is empty

Pub version 0.3.2+1 & 0.3.2+2

  • improve serving code of the start page.

Pub version 0.3.2 #

  • load values into your webserver from yaml files
  • cors
  • small improvement on the model object, it allows you to put anything in it

Pub version 0.3.1 #

Fix a problem with serving dart files.

Pub version 0.3.0+1 #

Fix a bug with passing by the startPage.

Pub version 0.3.0 #

Add 'force it' package into this new version of the forcemvc so you can use @Autowired in the @Controller classes.

Pub version 0.2.4 #

Add easier bootstrapping of the logging functionality serverside.

Pub version 0.2.3 #

Implemented changes to the security api. Add an optional parameter {data} in the security strategy checkAuthorization class.

Pub version 0.2.2 #

Refactoring of the use of webSockets, also provide httpRequest in the flow, so you get access to httpsession.

Pub version 0.2.1+2 #

Make the security rules more general.

Pub version 0.2.1+1 #

Make the securityContextHolder object in webserver public accessible.

Pub version 0.2.1 #

New structure!

Pub version 0.2.0 #

Adding security and authentication functionality

Pub version 0.1.13+5 #

Move transformation method to the view renderer!

Pub version 0.1.13+4 #

More logging!

Pub version 0.1.13+1 & 0.1.13+2

Adding slashes in src="...", it will become src="/..." when needed of the template rendering is been performed!

Pub version 0.1.13 #

Depend upon mustache4dart and not upon mustache anymore!

Pub version 0.1.12+2 #

Small improvement on the view rendering part!

Pub version 0.1.12+1 #

Changed ../build/ folder to ../build/web/

Pub version 0.1.12 #

Check also view, template files in the build folder, so after pub build in the web folder!

Pub version 0.1.11+3 #

Better logging for view rendering!

Pub version 0.1.11+2 #

Implement getPostRawData, so you can get the raw posted data!

Pub version 0.1.11+1 #

Adapt getPostData to disable jsonifying the data!

Pub version 0.1.11 #

Adding asynchronous requests to the possibilities now!

Pub version 0.1.10 #

Fixing in @RequestMapping, the method part!

Pub version 0.1.9+4 #

Adding redirect functionality.

Pub version 0.1.9+3 #

Removing dependency on Uuid.

Pub version 0.1.9+2 #

Add HttpSession & HttpHeaders in possible arguments in a controller method

Pub version 0.1.9+1 #

Solve an issue with the code, please update!

Pub version 0.1.9 #

Adding @RequestParam, so you can add this annotation to the controller class if you want to have easy access to querystring parameters.

Pub version 0.1.8+3 #

Make static folder configurable and some small improvements on the regex expression.

Pub version 0.1.8+2 #

Static folder for serving static files to the client!

Pub version 0.1.8+1 #

Improved accesability of path variables.

Pub version 0.1.8 #

New ways of view rendering and getting the templates.

Pub version 0.1.7+2 & 0.1.7+3

Cleanup code webserver. Solving an issue!

Pub version 0.1.7+1 #

Updated this buildPath: '../build/web' to get it working in Dart 1.2

Pub version 0.1.7 #

Extend ForceRequest with getPostData and getPostParams to support post methods.

Pub version 0.1.6 #

Adding path variables to the mvc part.

Pub version 0.1.5 #

Serving .dart files much easier, putting it in the framework itself.

Pub version 0.1.4 #

Adding interceptors to the game, so you can write interceptor classes to intercept a request.

Pub version 0.1.3 #

Updating dependency on force mirrors, it solves a bug with invocation. Controller scanning, so you don't need to register a class, just annotate it with @Controller.

Pub version 0.1.2+4 & 0.1.2+5

Adding an improvement of force mirrors invoke.

Pub version 0.1.2+3 #

Solving issue with dependency management

Pub version 0.1.2+2 #

Add dependency to forcemirrors.

Pub version 0.1.2+1 #

Adding tests for mirrorhelpers and refactor code to improve annotation handling.

Pub version 0.1.2 #

Adding the ModelAttribute annotation into the mvc framework.

Pub version 0.1.0 #

Adding an abstraction ForceRequest, a wrapper around httprequest.

Pub version 0.1.0 #

Adding renderer and model to the mvc part of it. Adding mustache as a new dependency for the rendering part.

Pub version 0.0.6 & 0.0.7

Fixing issue with annotations of RequestMapping

Pub version 0.0.5 #

Introducing Simple Web Server

Pub version 0.0.5 #

Introduction of the model

Pub version 0.0.4 #

Update version of uuid

Pub version 0.0.3 #

Adding RequestMethod class into the project

Pub version 0.0.2 #

Adding documentation and solving issues.

Pub version 0.0.1 #

Setup of the project, moved basic_server.dart to this package and make it usable in the dart force main package.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  forcemvc: ^0.3.7+1

2. Install it

You can install packages from the command line:

with pub:

$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:forcemvc/annotations/metadata.dart';
import 'package:forcemvc/error/handler_exception_resolver.dart';
import 'package:forcemvc/error/simple_exception_resolver.dart';
import 'package:forcemvc/force_mvc.dart';
import 'package:forcemvc/render/mustache_render.dart';
import 'package:forcemvc/render/view_render.dart';
import 'package:forcemvc/security/no_security_strategy.dart';
import 'package:forcemvc/security/security_context_holder.dart';
import 'package:forcemvc/security/security_strategy.dart';
import 'package:forcemvc/server/force_request.dart';
import 'package:forcemvc/server/handler_interceptor.dart';
import 'package:forcemvc/server/interceptors_collection.dart';
import 'package:forcemvc/server/model.dart';
import 'package:forcemvc/server/mvc_typedefs.dart';
import 'package:forcemvc/server/path_analyzer.dart';
import 'package:forcemvc/server/registry.dart';
import 'package:forcemvc/server/request_method.dart';
import 'package:forcemvc/server/response_hooks.dart';
import 'package:forcemvc/server/serving_files.dart';
import 'package:forcemvc/server/simple_web_server.dart';
import 'package:forcemvc/server/web_server.dart';
import 'package:forcemvc/test.dart';
Awaiting analysis to complete.