diff --git a/ninja/pom.xml b/ninja/pom.xml
index 962dabf1bd..8ec2422d9f 100644
--- a/ninja/pom.xml
+++ b/ninja/pom.xml
@@ -4,10 +4,6 @@
4.0.0
ninja
-
-
-
-
jar
com.baeldung
1.0.0
@@ -172,16 +168,6 @@
h2
1.4.186
-
-
-
-
org.ninjaframework
ninja-standalone
diff --git a/ninja/src/main/java/META-INF/persistence.xml b/ninja/src/main/java/META-INF/persistence.xml
index bfdd6ad8e8..e57cd5ecc0 100644
--- a/ninja/src/main/java/META-INF/persistence.xml
+++ b/ninja/src/main/java/META-INF/persistence.xml
@@ -1,33 +1,29 @@
-
-
-
-
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
+ version="2.0">
+
+
+
org.hibernate.jpa.HibernatePersistenceProvider
-
-
+
-
-
-
-
+
+
+
-
-
+
-
+
\ No newline at end of file
diff --git a/ninja/src/main/java/conf/Routes.java b/ninja/src/main/java/conf/Routes.java
index 4f23fd37fe..a1727d55b0 100644
--- a/ninja/src/main/java/conf/Routes.java
+++ b/ninja/src/main/java/conf/Routes.java
@@ -10,10 +10,15 @@ public class Routes implements ApplicationRoutes {
@Override
public void init(Router router) {
- router.GET().route("/").with(ApplicationController::index);
+ router.GET().route("/index").with(ApplicationController::index);
+ router.GET().route("/home").with(ApplicationController::home);
router.GET().route("/hello").with(ApplicationController::helloWorld);
router.GET().route("/userJson").with(ApplicationController::userJson);
+ router.GET().route("/createUser").with(ApplicationController::createUser);
+ router.GET().route("/flash").with(ApplicationController::showFlashMsg);
+
router.GET().route("/users").with(ApplicationController::fetchUsers);
+ router.POST().route("/users").with(ApplicationController::insertUser);
//Assets
router.GET().route("/assets/webjars/{fileName: .*}").with(AssetsController::serveWebJars);
diff --git a/ninja/src/main/java/conf/application.conf b/ninja/src/main/java/conf/application.conf
index 3bfbc70e1a..0ae4c7ec40 100644
--- a/ninja/src/main/java/conf/application.conf
+++ b/ninja/src/main/java/conf/application.conf
@@ -1,20 +1,9 @@
-# The main properties file to configure your application
-#
-# Properties can be prefixed by "%".
-# "%"" matches a mode you can set as system property. For instance when you
-# are using maven you can do: mvn -Dmode=test jetty:run
-#
-# Using that the following property:
-# %test.myproperty=test property
-# would overwrite the property
-# myproperty=my property
-#
-# You can define as many environments as you want. Simply set them in the mode.
-application.name=baeldung ninja application
+application.name=baeldung ninja dev application
+%test.application.name=baeldung ninja test application
+%prod.application.name=baeldung ninja application
application.cookie.prefix=NINJA
-#ISO Language Code, optionally followed by a valid ISO Country Code.
application.languages=fr,en
application.session.expire_time_in_seconds=3600
@@ -22,8 +11,6 @@ application.session.send_only_if_changed=true
application.session.transferred_over_https_only=false
ninja.port=8000
-
-# enable ssl with self-signed cert in dev & test modes
ninja.ssl.port=8001
application.secret = fxSjSL9Q017BSL7gBnkyo2Prln7uXaXIT35gotXRIED8c46OSa8s4QdoIQdTsEtj
diff --git a/ninja/src/main/java/controllers/ApplicationController.java b/ninja/src/main/java/controllers/ApplicationController.java
index 65c95e9204..cd8ad9d160 100644
--- a/ninja/src/main/java/controllers/ApplicationController.java
+++ b/ninja/src/main/java/controllers/ApplicationController.java
@@ -1,14 +1,8 @@
package controllers;
-import ninja.Context;
-import ninja.Result;
-import ninja.Results;
-import ninja.i18n.Lang;
-import ninja.i18n.Messages;
-import ninja.jpa.UnitOfWork;
-import services.UserService;
-
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
import java.util.Optional;
import javax.inject.Inject;
@@ -20,9 +14,19 @@ import org.apache.commons.logging.LogFactory;
import com.google.inject.Provider;
import com.google.inject.Singleton;
+import com.google.inject.persist.Transactional;
import models.User;
-import com.google.inject.persist.Transactional;
+import ninja.Context;
+import ninja.Result;
+import ninja.Results;
+import ninja.i18n.Lang;
+import ninja.i18n.Messages;
+import ninja.jpa.UnitOfWork;
+import ninja.session.FlashScope;
+import ninja.validation.JSR303Validation;
+import ninja.validation.Validation;
+import services.UserService;
@Singleton
public class ApplicationController {
@@ -40,44 +44,59 @@ public class ApplicationController {
@Inject
UserService userService;
-
+
public Result index() {
- Result result = Results.html();
- lang.setLanguage("fr", result);
- return result;
-
+ return Results.html();
}
public Result userJson() {
- User user = userService.getUser();
- logger.info(user);
- return Results.json().render(user);
+ HashMap userMap = userService.getUserMap();
+ logger.info(userMap);
+ return Results.json().render(userMap);
}
public Result helloWorld(Context context) {
- Optional language = Optional.of("en");
+ Optional language = Optional.of("fr");
String helloMsg = msg.get("helloMsg", language).get();
return Results.text().render(helloMsg);
}
+ public Result showFlashMsg(FlashScope flashScope) {
+ flashScope.success("Success message");
+ flashScope.error("Error message");
+ return Results.redirect("/home");
+ }
+
+ public Result home() {
+ return Results.html();
+ }
+
+ public Result createUser() {
+ return Results.html();
+ }
+
@UnitOfWork
public Result fetchUsers() {
- insertUser();
EntityManager entityManager = entitiyManagerProvider.get();
Query q = entityManager.createQuery("SELECT x FROM User x");
List users = (List) q.getResultList();
- System.out.println(users);
- return Results.text().render("Done");
+ return Results.json().render(users);
}
@Transactional
- public Result insertUser() {
- EntityManager entityManager = entitiyManagerProvider.get();
- User user = userService.getUser();
- entityManager.persist(user);
- entityManager.flush();
+ public Result insertUser(FlashScope flashScope, @JSR303Validation User user, Validation validation) {
+ logger.info("Inserting User : " +user);
+
+ if (validation.getViolations().size() > 0) {
+ flashScope.error("Validation Error: User can't be created");
+ } else {
+ EntityManager entityManager = entitiyManagerProvider.get();
+ entityManager.persist(user);
+ entityManager.flush();
+ flashScope.success("User '" + user + "' is created successfully");
+ }
- return Results.text().render("Inserted");
+ return Results.redirect("/home");
}
}
diff --git a/ninja/src/main/java/models/User.java b/ninja/src/main/java/models/User.java
index d1b18cfa83..e021567bca 100644
--- a/ninja/src/main/java/models/User.java
+++ b/ninja/src/main/java/models/User.java
@@ -4,6 +4,7 @@ import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
+import javax.validation.constraints.NotNull;
@Entity
public class User {
@@ -12,7 +13,9 @@ public class User {
@GeneratedValue(strategy=GenerationType.AUTO)
Long id;
+ @NotNull
public String firstName;
+
public String email;
public String toString() {
diff --git a/ninja/src/main/java/services/UserService.java b/ninja/src/main/java/services/UserService.java
index a549b48e48..10d625c66f 100644
--- a/ninja/src/main/java/services/UserService.java
+++ b/ninja/src/main/java/services/UserService.java
@@ -1,9 +1,9 @@
package services;
-import models.User;
+import java.util.HashMap;
public interface UserService {
- User getUser();
+ HashMap getUserMap();
}
diff --git a/ninja/src/main/java/services/UserServiceImpl.java b/ninja/src/main/java/services/UserServiceImpl.java
index 3ffa53400f..0f8c2214cf 100644
--- a/ninja/src/main/java/services/UserServiceImpl.java
+++ b/ninja/src/main/java/services/UserServiceImpl.java
@@ -1,15 +1,15 @@
package services;
-import models.User;
+import java.util.HashMap;
public class UserServiceImpl implements UserService {
@Override
- public User getUser() {
- User user = new User();
- user.firstName = "Norman";
- user.email = "norman@email.com";
- return user;
+ public HashMap getUserMap() {
+ HashMap userMap = new HashMap<>();
+ userMap.put("name", "Norman Lewis");
+ userMap.put("email", "norman@email.com");
+ return userMap;
}
}
diff --git a/ninja/src/main/java/views/ApplicationController/createUser.ftl.html b/ninja/src/main/java/views/ApplicationController/createUser.ftl.html
new file mode 100644
index 0000000000..9156f7dbf2
--- /dev/null
+++ b/ninja/src/main/java/views/ApplicationController/createUser.ftl.html
@@ -0,0 +1,12 @@
+<#import "../layout/defaultLayout.ftl.html" as layout>
+<@layout.myLayout "Create User">
+
+