@@ -22,36 +22,36 @@ Bellow you see a snipet code that put an error message in Flash object:
2222
2323``` java
2424POST (" /login" , routeContext - > {
25- String username = routeContext. getParameter(" username" ). toString();
26- String password = routeContext. getParameter(" password" ). toString();
27- if (authenticate(username, password)) {
28- routeContext. resetSession();
29-
30- routeContext. setSession(" username" , username);
31- routeContext. redirect(" /contacts" );
32- } else {
33- routeContext. flashError(" Authentication failed" ); // <-- here
34- routeContext. redirect(" /login" );
35- }
25+ String username = routeContext. getParameter(" username" ). toString();
26+ String password = routeContext. getParameter(" password" ). toString();
27+ if (authenticate(username, password)) {
28+ routeContext. resetSession();
29+
30+ routeContext. setSession(" username" , username);
31+ routeContext. redirect(" /contacts" );
32+ } else {
33+ routeContext. flashError(" Authentication failed" ); // <-- here
34+ routeContext. redirect(" /login" );
35+ }
3636});
3737
3838GET (" /login" , routeContext - > {
39- // we already have the flash object in the template model (injected by framework via Response.locals)
40- routeContext. render(" login" );
39+ // we already have the flash object in the template model (injected by framework via Response.locals)
40+ routeContext. render(" login" );
4141});
4242```
4343
4444The snippet for login template (freemarker engine) can be:
4545
4646``` html
4747<#if flash.hasError()>
48- <div class =" alert alert-danger" >
49- ${flash.getError()}
50- </div >
48+ <div class =" alert alert-danger" >
49+ ${flash.getError()}
50+ </div >
5151</#if>
5252
5353<form >
54- <!-- login form components here -->
54+ <!-- login form components here -->
5555</form >
5656```
5757
@@ -69,21 +69,21 @@ Are situation when we want to put a flash message directly in incoming (see Vali
6969
7070``` java
7171POST (" /" , routeContext - > {
72- Contact contact = routeContext. createEntityFromParameters(Contact . class);
73-
74- // check for validation
75- Set<ConstraintViolation<Contact > > violations = validator. validate(contact);
76- if (violations. isEmpty()) {
77- routeContext. send(contact. toString());
78- } else {
79- // makes violations available to template via flash (response.getLocals().get("flash"))
80- Flash flash = routeContext. getResponse(). getFlash();
81- for (ConstraintViolation<Contact > violation : violations) {
82- flash. error(violation. getPropertyPath() + " " + violation. getMessage());
83- }
84-
85- routeContext. setLocal(" contact" , contact); // <-- here
86- routeContext. render(" contact" );
87- }
72+ Contact contact = routeContext. createEntityFromParameters(Contact . class);
73+
74+ // check for validation
75+ Set<ConstraintViolation<Contact > > violations = validator. validate(contact);
76+ if (violations. isEmpty()) {
77+ routeContext. send(contact. toString());
78+ } else {
79+ // makes violations available to template via flash (response.getLocals().get("flash"))
80+ Flash flash = routeContext. getResponse(). getFlash();
81+ for (ConstraintViolation<Contact > violation : violations) {
82+ flash. error(violation. getPropertyPath() + " " + violation. getMessage());
83+ }
84+
85+ routeContext. setLocal(" contact" , contact); // <-- here
86+ routeContext. render(" contact" );
87+ }
8888});
8989```
0 commit comments