doctype html html head block headsection body header div#main block content footer
extends layout block headsection title , ! block content h1 Hello, habr!
package com.vagga.pojo; import com.j256.ormlite.field.DatabaseField; import com.j256.ormlite.table.DatabaseTable; @DatabaseTable(tableName = "users") public class User { @DatabaseField(columnName = "user_id", generatedId = true) private int userId; @DatabaseField(columnName = "username", unique = true, canBeNull = false) private String userName; @DatabaseField(columnName = "user_pass") private String password; public User() { } /* * - */ }
package com.vagga.utils; import com.j256.ormlite.dao.Dao; import com.j256.ormlite.dao.DaoManager; import com.j256.ormlite.jdbc.JdbcConnectionSource; import com.vagga.pojo.Category; import com.vagga.pojo.Comment; import com.vagga.pojo.Post; import com.vagga.pojo.User; import java.sql.SQLException; public class DbUtil { private static DbUtil ourInstance = new DbUtil(); private JdbcConnectionSource dbSource; private Dao<User, Integer> userDao; private Dao<Post, Integer> postDao; private Dao<Category, Integer> categoryDao; private Dao<Comment, Integer> commentDao; public static DbUtil getInstance() { return ourInstance; } private DbUtil() { try { dbSource = new JdbcConnectionSource("jdbc:mysql://localhost:3306/vagga?user=root&characterEncoding=utf8"); userDao = DaoManager.createDao(dbSource, User.class); postDao = DaoManager.createDao(dbSource, Post.class); categoryDao = DaoManager.createDao(dbSource, Category.class); commentDao = DaoManager.createDao(dbSource, Comment.class); } catch (SQLException e) { e.printStackTrace(); System.out.println("Cannot establish DB connection " + e.getMessage() + " " + e.getCause()); } } public Dao<User, Integer> getUserDao() { return userDao; } public Dao<Post, Integer> getPostDao() { return postDao; } public Dao<Category, Integer> getCategoryDao() { return categoryDao; } public Dao<Comment, Integer> getCommentDao() { return commentDao; } }
package com.vagga.utils; import de.neuland.jade4j.JadeConfiguration; import de.neuland.jade4j.exceptions.JadeException; import de.neuland.jade4j.model.JadeModel; import de.neuland.jade4j.template.FileTemplateLoader; import de.neuland.jade4j.template.JadeTemplate; import de.neuland.jade4j.template.TemplateLoader; import spark.ModelAndView; import spark.TemplateEngine; import java.io.File; import java.io.IOException; import java.io.StringWriter; import java.util.Map; public class JadeEngine extends TemplateEngine { private JadeConfiguration configuration; private String directory = new File(".").getCanonicalPath(); public JadeEngine() throws IOException { this.configuration = new JadeConfiguration(); this.directory = this.directory + "/src/main/resources/templates/"; TemplateLoader loader = new FileTemplateLoader(directory, "UTF-8"); configuration.setTemplateLoader(loader); } @SuppressWarnings("unchecked") @Override public String render(ModelAndView modelAndView) { StringWriter stringWriter = new StringWriter(); try { JadeTemplate template = this.configuration.getTemplate(modelAndView.getViewName()); JadeModel jadeModel = new JadeModel((Map<String, Object>) modelAndView.getModel()); template.process(jadeModel, stringWriter); } catch (JadeException | IOException e) { e.getCause(); } return stringWriter.toString(); } }
package com.vagga.routes; public interface BaseRoute { public void initBeforeAction(); public void initActions(); public void initAfterActions(); }
package com.vagga.routes; import com.vagga.utils.JadeEngine; import com.vagga.utils.JsonTransformer; import java.io.IOException; public class Route { protected JadeEngine templateEngine; protected JsonTransformer jsonTransformer; protected Route() throws IOException { templateEngine = new JadeEngine(); jsonTransformer = new JsonTransformer(); } }
package com.vagga.routes; import com.vagga.pojo.Category; import com.vagga.pojo.User; import com.vagga.utils.DbUtil; import spark.ModelAndView; import java.io.IOException; import java.sql.SQLException; import java.util.HashMap; import java.util.List; import java.util.Map; import static spark.Spark.*; public class HomeRoute extends Route implements BaseRoute { public HomeRoute() throws IOException { super(); } @Override public void initBeforeAction() { } @Override public void initActions() { get("/", (req, res) -> { Map<String, Object> model = new HashMap<String, Object>(); try { List<Category> categories = DbUtil.getInstance().getCategoryDao().queryForAll(); User user = DbUtil.getInstance().getUserDao().queryForId(1); model.put("user", user); model.put("categories", categories); model.put("title", ""); } catch (SQLException e) { e.printStackTrace(); } return new ModelAndView(model, "home/index.jade"); }, this.templateEngine); } @Override public void initAfterActions() { } }
public class VaggaMain { public static void main(String[] args) { try { ArrayList<BaseRoute> routes = new ArrayList<>(); routes.add(new HomeRoute()); routes.add(new AdminRoute()); routes.add(new ApiRoute()); routes.forEach((route) -> { route.initBeforeAction(); route.initActions(); route.initAfterActions(); }); } catch (IOException e) { e.printStackTrace(); } } }
Source: https://habr.com/ru/post/250853/
All Articles