I have a Spark MVC application that is very simple.
According to the spark documentation , this should be enough to run the application:
public class SparkServer { public static void main(String args[]) { Spark.staticFileLocation("src/main/webapp"); System.out .println("bla bla bla"); RService rService = new SparqlRService(); new RController(rService); } }
I put this class in a package inside my project and I run the web application (dynamic web application) on the Apache Tomcat server.
The print operation does not appear when Apache Tomcat starts, which means that this class is not called. I know that makes sense . that's why i ask. please, how can I let Apache Tomcat launch my spark application?
Update
After @mlk answer , I did the following:
public class SparkServer implements SparkApplication { @Override public void init() { Spark.staticFileLocation("src/main/webapp"); System.out .println("bla bla lba"); RService rService = new SparqlRService(); new RController(rService); } }
and in my web.xml I did:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>SRecommender</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>SparkFilter</filter-name> <filter-class>spark.servlet.SparkFilter</filter-class> <init-param> <param-name> applicationClass</param-name> <param-value>com.srecommender.SparkServer</param-value> </init-param> </filter> <filter-mapping> <filter-name>SparkFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
Where is my SparkServer located in the package: com.recommender
, which exists in the source folder: src/main/java
I run apache tomcat, but still, when I call any path from the spark, it returns 404.
HITE I can start a spark from the main method and call my pages, they work. so the problem is with the way I configured the spark to run in apache tomcat
Update 2
Here's how to set the path to the view.
public RecommendationController(RecommendationService service) { get("/", (request, response) -> { Map<String, Object> model = new HashMap<>(); model.put("data", "forza ROMA"); model.put("data2", "It Rome, It home"); return View("src/main/webapp/template/index.html", model); });