0

Метод генерации model

Код создания модели выведен в отдельный метод WebApp.getDefaultModel()
This commit is contained in:
2015-12-03 17:15:49 +00:00
parent 7cf5b252a3
commit e7f6ecd779
3 changed files with 16 additions and 11 deletions

View File

@@ -1,10 +1,14 @@
package kinosearch.webapp; package kinosearch.webapp;
import kinosearch.core.Tools;
import kinosearch.webapp.template.FreemakerProcessor; import kinosearch.webapp.template.FreemakerProcessor;
import kinosearch.webapp.template.TemplateProcessor; import kinosearch.webapp.template.TemplateProcessor;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener; import javax.servlet.ServletContextListener;
import java.util.HashMap;
import java.util.Map;
public class WebApp implements ServletContextListener { public class WebApp implements ServletContextListener {
private static boolean _init = false; private static boolean _init = false;
@@ -14,6 +18,16 @@ public class WebApp implements ServletContextListener {
return (_init ? templateProcessor : null); return (_init ? templateProcessor : null);
} }
public static Map<String, Object> getDefaultModel(ServletContext context) {
Map<String, Object> model = new HashMap<>();
model.put(".templatedir", context.getRealPath("WEB-INF"));
model.put("basedir", context.getContextPath());
model.put("version", Tools.VERSION);
model.put("rutext", "Поиск кино по пиратским кинотеатрам");
return model;
}
@Override @Override
public void contextInitialized(ServletContextEvent sce) { public void contextInitialized(ServletContextEvent sce) {
templateProcessor = new FreemakerProcessor(); templateProcessor = new FreemakerProcessor();
@@ -25,5 +39,4 @@ public class WebApp implements ServletContextListener {
_init = false; _init = false;
} }
} }

View File

@@ -26,11 +26,7 @@ public class IndexServlet extends HttpServlet {
} }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Map<String, Object> model = new HashMap<>(); Map<String, Object> model = WebApp.getDefaultModel(getServletContext());
model.put(".templatedir", getServletContext().getRealPath("WEB-INF"));
model.put("basedir", getServletContext().getContextPath());
model.put("version", Tools.VERSION);
model.put("rutext", "Поиск кино по пиратским кинотеатрам");
if (request.getParameter("search") != null && !request.getParameter("search").trim().isEmpty()) { if (request.getParameter("search") != null && !request.getParameter("search").trim().isEmpty()) {
boolean strong = false; boolean strong = false;

View File

@@ -32,11 +32,7 @@ public class TemplateServlet extends HttpServlet {
String fileName = Tools.getFileFromURI(req.getRequestURI(), req.getContextPath()); String fileName = Tools.getFileFromURI(req.getRequestURI(), req.getContextPath());
Path path = Paths.get(getServletContext().getRealPath("WEB-INF/simple_template" + fileName)); Path path = Paths.get(getServletContext().getRealPath("WEB-INF/simple_template" + fileName));
if (Files.exists(path)) { if (Files.exists(path)) {
Map<String, Object> model = new HashMap<>(); Map<String, Object> model = WebApp.getDefaultModel(getServletContext());
model.put(".templatedir", getServletContext().getRealPath("WEB-INF"));
model.put("basedir", getServletContext().getContextPath());
model.put("version", Tools.VERSION);
model.put("rutext", "Поиск кино по пиратским кинотеатрам");
resp.setCharacterEncoding("UTF-8"); resp.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=UTF-8"); resp.setContentType("text/html;charset=UTF-8");