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;
import kinosearch.core.Tools;
import kinosearch.webapp.template.FreemakerProcessor;
import kinosearch.webapp.template.TemplateProcessor;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.HashMap;
import java.util.Map;
public class WebApp implements ServletContextListener {
private static boolean _init = false;
@@ -14,6 +18,16 @@ public class WebApp implements ServletContextListener {
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
public void contextInitialized(ServletContextEvent sce) {
templateProcessor = new FreemakerProcessor();
@@ -25,5 +39,4 @@ public class WebApp implements ServletContextListener {
_init = false;
}
}

View File

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

View File

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