【发布时间】:2016-06-09 16:33:46
【问题描述】:
我有一个 Spark MVC 应用程序,非常简单。
根据spark documentation,这应该足以运行应用程序:
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);
}
}
我将该类放在我的项目中的一个包中,并在 Apache Tomcat 服务器上运行 Web 应用程序(动态 Web 应用程序)。
运行 Apache Tomcat 时没有出现 print 语句,说明这个类没有被调用。 我知道这是有道理的。这就是我要问的原因。请问如何让 Apache Tomcat 运行我的 spark 应用程序?
更新
在@mlk answer之后,我做了以下事情:
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);
}
}
在我的 web.xml 中我做了:
<?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>
我的 SparkServer 在包中的位置:
com.recommender 存在于源文件夹中:src/main/java
我运行 apache tomcat,但是当我从 spark 调用任何路径时,它仍然返回 404。
HITE 我可以从 main 方法运行 spark 并调用我的页面,它们正在工作。所以我配置 spark 在 apache tomcat 中运行的方式存在问题
更新 2
这是如何设置视图的路径
public RecommendationController(RecommendationService service) {
get("/", (request, response) -> {
Map<String, Object> model = new HashMap<>();
model.put("data", "forza ROMA");
model.put("data2", "It's Rome, It's home");
return View("src/main/webapp/template/index.html", model);
});
【问题讨论】:
-
我认为这个问题有误:您应该使用apache-spark 而不是spark-java。
标签: java apache tomcat spark-java