./: pom.xml ./javascript: user.js ./pages: index.html layout.html login.html ./sql: 1.sql ./src/main/java/com/igumnov/common/example: App.java ExampleUser.java
<dependencies> <!-- --> <dependency> <groupId>com.igumnov</groupId> <artifactId>common</artifactId> <version>3.15</version> </dependency> <!-- --> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>1.4.187</version> </dependency> <!-- Bootstrap, AnglularJS webjars --> <dependency> <groupId>org.webjars</groupId> <artifactId>angular-ui-bootstrap</artifactId> <version>0.12.0</version> </dependency> <dependency> <groupId>org.webjars</groupId> <artifactId>angularjs</artifactId> <version>1.3.8</version> </dependency> <dependency> <groupId>org.webjars</groupId> <artifactId>bootstrap</artifactId> <version>3.3.1</version> </dependency> </dependencies>
public class App { public static void main(String[] args) throws Exception { ORM.connectionPool("org.h2.Driver", "jdbc:h2:mem:test", "SA", "", 1, 3); // ( 3 ) ORM.applyDDL("sql"); // WebServer.init("localhost", 8181); // - WebServer.security("/login", "/login", "/logout"); // URL- WebServer.addRestrictRule("/*", new String[]{"user_role"}); // user_role WebServer.addAllowRule("/static/*"); // WebServer.addClassPathHandler("/static", "META-INF/resources/webjars"); // classpath webjars WebServer.addAllowRule("/js/*"); // Java Script- WebServer.addStaticContentHandler("/js", "javascript"); // Java Script WebServer.addTemplates("pages",0); // // "/", , index.html WebServer.addController("/", (request, model) -> { model.put("time", new Date()); return "index"; }); // "/login", , login.html WebServer.addController("/login", (request, model) -> { return "login"; }); // REST- "/rest/user" POST/PUT JSON- ExampleUser.class WebServer.addRestController("/rest/user", ExampleUser.class, (request, postObj) -> { switch (request.getMethod()) { case (WebServer.METHOD_GET): // GET ArrayList<Object> users; try { users = ORM.findAll(ExampleUser.class); // } catch (Exception e) { throw new WebServerException(e.getMessage()); // , JSON } return users; // JSON case (WebServer.METHOD_POST): // POST ExampleUser ret = null; try { ret = (ExampleUser) ORM.insert(postObj); // } catch (Exception e) { throw new WebServerException(e.getMessage()); } return ret; // JSON case (WebServer.METHOD_DELETE): // DELETE ExampleUser user; try { user = (ExampleUser) ORM.findOne(ExampleUser.class, request.getParameter("userName")); // if(user.getUserName().equals("demo")) { // demo throw new WebServerException("You cant delete user demo"); } else { ORM.delete(user); // delete- } } catch (Exception e) { throw new WebServerException(e.getMessage()); // , JSON } return user; // JSON , default: throw new WebServerException("Unsupported method"); // , PUT } }); ArrayList<Object> users = ORM.findAll(ExampleUser.class); // if (users.size() == 0) { // ExampleUser user = new ExampleUser(); user.setUserName("demo"); user.setUserPassword("demo"); ORM.insert(user); // demo/demo WebServer.addUser("demo", "demo", new String[]{"user_role"}); // - demo/demo user_role } users.stream().forEach((user) -> { // ExampleUser u = (ExampleUser) user; WebServer.addUser(u.getUserName(), u.getUserPassword(), new String[]{"user_role"}); // - user_role }); WebServer.start(); // Exception, - :) } }
// JSON public class ExampleUser { @Id(autoIncremental = false) // ORM Primary Key insert private String userName; private String userPassword; ... }
# ORM ExampleUser.class CREATE TABLE ExampleUser (userName VARCHAR(255) PRIMARY KEY, userPassword VARCHAR(255))
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd"> <!-- layout layout.html --> <html xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorator="layout"> <body> <!-- layout.html --> <div layout:fragment="content"> <form name="form" action="/j_security_check" method="POST"> <div class="modal-header"> <h3 class="modal-title">Login</h3> </div> <div class="modal-body"> <div class="form-group"> <input type="text" name="j_username" class="form-control" value="" placeholder="Login"/> </div> <div class="form-group"> <input type="password" name="j_password" class="form-control" placeholder="Password"/> </div> <div class="form-group"> <button type="submit" id="login" class="btn btn-primary">OK</button> </div> </div> </form> </div> </body> </html>
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd"> <!-- layout layout.html --> <html xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorator="layout"> <body> <!-- layout.html --> <div layout:fragment="content"> <!-- AngularJS--> <script src="/js/user.js"></script> <h1 th:text="${time}"></h1> // <!-- UserCtrl --> <div ng-controller="UserCtrl"> <table class="table"> <thead> <tr> <th>Name</th> <th>Password</th> <th></th> </tr> </thead> <tbody> <!-- --> <tr ng-repeat="user in users"> <td>{{user.userName}}</td> <td>{{user.userPassword}}</td> <!-- --> <td><a href="#"><span class="glyphicon glyphicon-remove" tooltip="Delete" ng-click="deleteUser(user)"/></a></td> </tr> </tbody> </table> <div ng-model="user"> <!-- --> <div class="form-group"> <input type="text" class="form-control" ng-model="user.userName" placeholder="Login"/> </div> <div class="form-group"> <input type="password" class="form-control" ng-model="user.userPassword" placeholder="Password"/> </div> <div class="form-group"> <!-- --> <button class="btn btn-primary" ng-click="addUser(user)">Add</button> </div> </div> </div> </div> </body> </html>
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd"> <!-- AngularJS --> <html ng-app="com.igumnov.common.example"> <head> <title>Title</title> <link rel="stylesheet" href="/static/bootstrap/3.3.1/css/bootstrap.min.css" /> <meta charset="utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> </head> <body> <script src="/static/angularjs/1.3.8/angular.min.js"></script> <script src="/static/angularjs/1.3.8/angular-resource.min.js"></script> <script src="/static/angular-ui-bootstrap/0.12.0/ui-bootstrap-tpls.min.js"></script> <div class="container"> <!-- --> <div layout:fragment="content"></div> </div> </body> </html>
angular.module('com.igumnov.common.example', ['ui.bootstrap', 'ngResource']) .factory('User', ['$resource', function ($resource) { // REST- User return $resource('/rest/user', {}, { list: { // method: 'GET', cache: false, isArray: true // }, add: { // method: 'POST', cache: false, isArray: false // }, delete: { // method: 'DELETE', cache: false, isArray: false // } }); }]) .controller('UserCtrl', function ($scope, User) { // UserCtrl $scope.users = User.list({}); // $scope.addUser = function (user) { // User.add({},user,function (data) { // REST- $scope.users = User.list({}); // , }, function (err) { alert(err.data.message); // , }); } $scope.deleteUser = function (user) { // User.delete({"userName" : user.userName},user,function (data) { // REST- $scope.users = User.list({}); // , }, function (err) { alert(err.data.message); // , }); } });
Source: https://habr.com/ru/post/259005/
All Articles