Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Tomcat (https://github.com/bastillion-io/lmvc/issues/1) #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>loophole.mvc</groupId>
<artifactId>lmvc</artifactId>
<version>1.06.00</version>
<version>1.06.01-SNAPSHOT</version>
<packaging>jar</packaging>
<name>lmvc</name>
<properties>
Expand Down
55 changes: 31 additions & 24 deletions src/main/java/loophole/mvc/base/TemplateServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,42 +27,49 @@
*/
package loophole.mvc.base;

import loophole.mvc.config.TemplateConfig;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.WebContext;
import java.io.IOException;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import org.apache.commons.lang3.StringUtils;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.WebContext;

import loophole.mvc.config.TemplateConfig;

@WebServlet("*" + TemplateServlet.VIEW_EXT)
public class TemplateServlet extends HttpServlet {

public static final String VIEW_EXT = ".html";
private static final long serialVersionUID = 411L;
public static final String VIEW_EXT = ".html";
private static final long serialVersionUID = 411L;

public TemplateServlet() {
super();
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {

public TemplateServlet() {
super();
}
TemplateEngine engine = TemplateConfig.getTemplateEngine(request.getServletContext());
WebContext context = new WebContext(request, response, request.getServletContext());
context.getResponse().setContentType("text/html; charset=UTF-8");
String uri = request.getRequestURI()
.replaceAll("\\" + TemplateServlet.VIEW_EXT + ".*", TemplateServlet.VIEW_EXT)
.replaceAll("^" + request.getContextPath(), "");

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException {
if (StringUtils.equals("/", uri)) {
uri = "/index.html";
}

TemplateEngine engine = TemplateConfig.getTemplateEngine(request.getServletContext());
WebContext context = new WebContext(request, response, request.getServletContext());
context.getResponse().setContentType("text/html; charset=UTF-8");
String uri = request.getRequestURI().replaceAll("\\" + TemplateServlet.VIEW_EXT + ".*", TemplateServlet.VIEW_EXT)
.replaceAll("^" + request.getContextPath(), "");
engine.process(uri, context, response.getWriter());
}
engine.process(uri, context, response.getWriter());
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {

doGet(request, response);
}
doGet(request, response);
}
}