forcemvc2 0.3.8

  • Readme
  • Changelog
  • Example
  • Installing
  • 21

VERSION! Build Status

Dart Force MVC #


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

Introduction #

Dart ForceMVC is a dartlang web framework, with a lot of similarities to java spring mvc

First of all you need to setup a web server:

library x;
import 'package:wired2/wired2.dart';
import 'package:forcemvc2/forcemvc2.dart';

part 'controllers/x_controller.dart';

main() {
  WebApplication app = new WebApplication(startPage: "start.html");

And then of course we need to create our controller.

part of x;

class XController {

  @RequestMapping(value: "/home", method: "GET")
  String home(Locale locale, Model model) {
    model.addAttribute("someprop", "value" );
    return "home";

In the web folder of your application you can create a home.html file. That will be your view.

Walkthrough #

Use a Web Application Server with dart very easily, create controllers with annotations ... similar to java spring mvc.

First you will setup a new web application.

WebApplication app = new WebApplication(wsPath: wsPath, port: port, host: host, buildPath: buildPath);

Then you use the 'use' method to handle http requests.

app.use(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 WebApplication object.


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

class SomeObject {


Starting your web application #

You can do this as follow!


It is also possible to start a web application 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 asynchronous 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. This will make it necessary to for a user to authenticate before accessing these resources.

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 web application.

app.strategy = new SessionStrategy();

Roles #

You can also define authorize roles. This can be done as follow.

@PreAuthorizeRoles(const ["ADMIN"])
class AdminController {
ExceptionHandler #

This helps in defining methods that will be executed when an error or exception occured.

String error_catch(req, Model model) {

You can also specify a type, only when an error or exception happend of that Type, that method will be executed.

@ExceptionHandler(type: DoorLockedError)
String doorLockedError(req, Model model) {
	model.addAttribute("explanation", "This is a specific error!");
	return "error";  

Logging #

You can easily boostrap logging.


Wired #

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

LocaleResolver #

In ForceMVC you have a locale resolver to handle locale. The implementation that is been used by default is the AcceptHeaderLocale Resolver, this resolver looks at the request header accept-language.

You can choose for a fixed locale resolver implementation or a cookie locale resolver or just implement your own handling if need.

View / Templating #

In forcemvc you can define view templates. ForceMvc will look into the viewfolder and in the client 'build' folder for a .html file with the viewname that you provide the system in the controller.

@RequestMapping(value: "/hello/")
String redirect(req, Model model) {
	// do something
	model.addAttribute("text", "greetings");
	return "hello";

So in the example about we are returning a string with the value 'hello'. So the system will search in the view & build folder for a hello.html file.

The default implementation in ForceMVC for templating is mustache.

In the html file {{text}} will be replaced by greetings.

More info about creating your own viewrender implementation here

Development trick #

Following the next steps will make it easier for you to develop, this allows you to adapt clientside files and immidiatly see results with doing a pub build.

pub serve web --hostname --port 7777 &&
export DART_PUB_SERVE="http://localhost:7777" &&
pub run bin/server.dart


You can now easily run your Force apps on a Google App Engine infrastructure by the following code! The rest is the same as a normal dart force app.

WebApplication app = new WebApplication();

runAppEngine(app.requestHandler).then((_) { // Server running. and you can do all the stuff you want! });

You don't need to start WebApplication anymore, the start of the server will be done by AppEngine!

More info about GAE overall

Rest API's #

If you want to learn more about how to build Rest api's with ForceMVC go here

Example #

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

Or visit Github issue mover here

Youtube #

You can watch a simple youtube video to get you started, have an idea between the transformation of java spring mvc and force mvc here


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

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 & wired 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.8.2 & version 0.8.5

  • Update version of wired to v0.4.5
  • improve @ResponseBody & httpmessageconverter logic

Pub version 0.8.1 #

  • Introducing @RestController
  • improve @ResponseBody
  • solve path variables with dash, issue #32

Pub version 0.8.0 #

  • Updating dependencies on logging
  • Clean up code
  • introduce @ResponseBody
  • HttpMessageConverters and HttpMessageRegulator are also available in this release with a JsonHttpMessageConverter implementation

Pub version 0.7.3 #

  • Introducing a new annotation @ResponseStatus, so we can indicate status code of the response

Pub version 0.7.2 #

  • Adopt code to async/await
  • Extract locale to an other dependency

Pub version 0.7.1+1 #

  • Improve the flow of client serving
  • Add more logging

Pub version 0.7.1 #

  • Allow completely handling the HttpResponse

Pub version 0.7.0 #

  • update forcemvc to wired 4.3
  • add web config and an easy change towards core components with di
    • SecurityContextHolder
    • HandlerExceptionResolver
    • LocaleResolver
    • ServingAssistent

Pub version 0.6.1+1 #

  • add improvement on static handling internally

Pub version 0.6.1 #

  • remove start page and use this instead!

app.static("/", "index.html");

Pub Version 0.6.0+1 #

  • Tighter version constraint on exported package

Pub Version 0.6.0 #

  • WebServer becomes WebApplication dart class
  • The .on method becomes .use method
  • app.static will skip the view rendering logic, ideal for angular and polymer applications
  • simplify code on some parts

Pub Version 0.5.10 #

  • Extending the possibilities of a Rest interface.

Pub Version 0.5.9+1 #

  • Fix a bug so that @Authentication works, and takes up BASIC as a role.

Pub Version 0.5.9 #

  • Improve the startup functionality, by adding startup fallback mechanism

Pub Version 0.5.8 #

  • Make the default value to startpage empty. It is better to use:

    app.use('/', (req, model) { return "index"; });

    then startPage: "index.html"

    Be aware that then it becomes a template and that when you use polymer or angular you need to use another delimiter.

Pub version 0.5.7+1 #

  • Deprecate the method on, it is too confusing with the on method of dart force, the method 'use' is now the prefered way.
  • Fixing a crash when the startPage is not found.

Pub version 0.5.7 #

  • Changing the main class of WebServer to WebApplication.
  • Making WebServer Deprecated

Pub version 0.5.6+1 #

  • Fix issue serving without pub serve
  • put more control in accept_header_locale

Pub version 0.5.6 #

  • Apply pub serve trick also to view render logic.
  • Fallbacks / error handling when a resource is not been found.

Pub version 0.5.5 #

  • Add locale resolver mechanics into the framework
  • Easy use of the locale in the controller

Pub version 0.5.4 #

  • Add required boolean into @RequestParam(required: true)
  • Pub serve trick, issue 23 is been solved.

Pub version 0.5.3+1 #

  • Small adjustments so dart force can also work on appengine.

Pub version 0.5.3 #

  • Add better 404 handling
  • Serving the Client files from their URL relative to the script's location
  • Make dart-forcemvc WebServer compatible with App Engine Dart

Pub version 0.5.2+1 #

  • Fixing 'Replace only the first occurrence of /build'

Pub version 0.5.2 #

  • package 'Force IT' is been renamed to 'wired'

Pub version 0.5.1 #

  • Add Type checking in @ExceptionHandler

Pub version 0.5.0+1 #

  • Add getAuthentication in MVCAnnotationHelper

Pub version 0.5.0 #

  • Add PreauthorizeRoles and PreauthorizeFunc Annotations
  • Add possibility to define authorization roles
  • Define and extend the way security works in forcemvc
  • Upgrade to the latest version of 'force it'

Pub version 0.4.0+1 & 0.4.0+2

  • Adding MVCAnnotationHelper, with the method hasAuthentication(obj) to the package

Pub version 0.4.0 #

  • Remove parentheses from annotations.
  • Update to the latest version (0.2.0) of force_it
  • Add example folder to the git repository

Pub version 0.3.8 #

  • Introducing @ControllerAdvice so that you can make methods with @ModelAttribute & @ExceptionHandler that has inpact to all the controller

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.


library example;

import "package:forcemvc2/forcemvc2.dart";
import 'package:example/controllers.dart';

main() {
  print("Opening localhost:8080 ...");
  WebApplication app = new WebApplication(views: 'views/');

Use this package as a library

1. Depend on it

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

  forcemvc2: ^0.3.8

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:forcemvc2/forcemvc2.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Nov 22, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.6.1
  • pana: 0.12.21


Detected platforms: other

Primary library: package:forcemvc2/forcemvc2.dart with components: io, mirrors.

Health issues and suggestions

Fix lib/server/force_request.dart. (-29.38 points)

Analysis of lib/server/force_request.dart failed with 1 error, 12 hints, including:

line 42 col 35: The argument type 'AsciiDecoder' can't be assigned to the parameter type 'StreamTransformer<Uint8List, dynamic>'.

line 12 col 22: Unnecessary new keyword.

line 13 col 27: Unnecessary new keyword.

line 19 col 7: Use isNotEmpty instead of length

line 50 col 12: Unnecessary new keyword.

Fix lib/test.dart. (-29.02 points)

Analysis of lib/test.dart failed with 1 error, 11 hints, including:

line 41 col 35: The argument type 'AsciiDecoder' can't be assigned to the parameter type 'StreamTransformer<Uint8List, dynamic>'.

line 13 col 36: Unnecessary new keyword.

line 14 col 30: Unnecessary new keyword.

line 22 col 22: Unnecessary new keyword.

line 23 col 27: Unnecessary new keyword.

Fix lib/http/media_type.dart. (-25.97 points)

Analysis of lib/http/media_type.dart reported 60 hints, including:

line 3 col 1: Prefer using /// for doc comments.

line 11 col 3: Prefer using /// for doc comments.

line 14 col 32: Unnecessary new keyword.

line 16 col 3: Prefer using /// for doc comments.

line 21 col 3: Prefer using /// for doc comments.

Fix additional 33 files with analysis or formatting issues. (-118.69 points)

Additional issues in the following files:

  • lib/server/registry.dart (29 hints)
  • lib/server/web_application.dart (25 hints)
  • lib/annotations/metadata.dart (21 hints)
  • lib/server/serving_assistent.dart (20 hints)
  • lib/server/simple_web_server.dart (20 hints)
  • lib/http/http_headers_wrapper.dart (16 hints)
  • lib/converters/abstract_message_converter.dart (14 hints)
  • lib/http/mime_type.dart (14 hints)
  • lib/utils/mime_type_utils.dart (10 hints)
  • lib/i18n/cookie_locale_resolver.dart (8 hints)
  • lib/manager/cookie_holder_manager.dart (7 hints)
  • lib/render/view_render.dart (7 hints)
  • lib/server/serving_files.dart (7 hints)
  • lib/i18n/accept_header_locale_resolver.dart (6 hints)
  • lib/i18n/fixed_locale_resolver.dart (4 hints)
  • lib/server/model.dart (4 hints)
  • lib/error/handler_exception_resolver.dart (3 hints)
  • lib/error/simple_exception_resolver.dart (3 hints)
  • lib/http/http_input_message.dart (3 hints)
  • lib/http/http_message_regulator.dart (3 hints)
  • lib/http/http_method.dart (3 hints)
  • lib/http/http_output_message.dart (3 hints)
  • lib/i18n/locale_resolver.dart (3 hints)
  • lib/annotations/helpers.dart (2 hints)
  • lib/i18n/default_locale_resolver.dart (2 hints)
  • lib/security/security_context_holder.dart (2 hints)
  • lib/converters/http_message_converter.dart (1 hint)
  • lib/forcemvc2.dart (1 hint)
  • lib/http/http_message.dart (1 hint)
  • lib/render/mustache_render.dart (1 hint)
  • lib/server/http_request_streamer.dart (1 hint)
  • lib/server/interceptors_collection.dart (1 hint)
  • lib/server/path_analyzer.dart (1 hint)

Maintenance issues and suggestions

Support latest dependencies. (-10 points)

The version constraint in pubspec.yaml does not support the latest published versions for 1 dependency (intl).


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
http_server >=0.9.0 <0.10.0 0.9.8+3
intl ^0.15.8 0.15.8 0.16.0
locales ^0.1.0 0.1.0
logging >=0.11.0 <0.12.0 0.11.3+2
mockito ^4.0.0 4.1.1
mustache4dart2 ^0.1.0 0.1.0
path ^1.6.2 1.6.4
route2 ^0.1.1 0.1.1
wired2 ^0.1.0 0.1.0
Transitive dependencies
async 2.4.0
boolean_selector 1.0.5
charcode 1.1.2
collection 1.14.12
matcher 0.12.6
meta 1.1.8
mime 0.9.6+3
mirrorme2 0.1.0
pedantic 1.8.0+1
source_span 1.5.5
stack_trace 1.9.3
stream_channel 2.0.0
string_scanner 1.0.5
term_glyph 1.1.0
test_api 0.2.11
yaml 2.2.0
Dev dependencies
test ^1.0.0