我已经尝试了一段时间,我真的无法用嵌入式jetty运行SpringMVC(基于注释的MVC配置)项目,同时使用gradle管理依赖项。
我设法显示jsp页面,但是没有解析jsp标签?执行? 我的设置如下: 的build.gradle:
apply plugin: 'java'
apply plugin: 'eclipse'
repositories {
mavenCentral()
}
sourceSets.main.resources.srcDirs = ['src/main/resources', 'webapp']
sourceSets.main.java.srcDirs = ['src/main/java']
dependencies {
compile 'javax.servlet.jsp:jsp-api:2.2'
compile 'javax.servlet:javax.servlet-api:3.1.0'
compile 'javax.servlet.jsp.jstl:jstl-api:1.2'
compile 'org.springframework:spring-webmvc:4.1.6.RELEASE'
compile 'org.apache.logging.log4j:log4j-core:2.2'
compile 'org.eclipse.jetty:jetty-webapp:9.2.10.v20150310'
}
我的视图解析器配置如下:
@Bean
public InternalResourceViewResolver jspViewResolver() {
InternalResourceViewResolver bean = new InternalResourceViewResolver();
bean.setPrefix("/pages/");
bean.setSuffix(".jsp");
return bean;
}
我正在尝试显示这个jsp页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<c:set var="test" scope="session" value="test"/>
<c:out value="${test}"/>
</body>
</html>
@Edit: 用于设置Jetty和SpringMVC的AppInitializer:
public class AppInitializer {
private static final int DEFAULT_PORT = 8080;
private static final String CONTEXT_PATH = "/";
private static final String CONFIG_LOCATION = "pl.com.imralav.springmvc.config";
private static final String MAPPING_URL = "/*";
private static final String DEFAULT_PROFILE = "dev";
public static void main(String[] args) throws Exception {
new AppInitializer().startJetty(getPortFromArgs(args));
}
private static int getPortFromArgs(String[] args) {
if (args.length > 0) {
try {
return Integer.valueOf(args[0]);
} catch (NumberFormatException ignore) {
}
}
return DEFAULT_PORT;
}
private void startJetty(int port) throws Exception {
Server server = new Server(port);
server.setHandler(getServletContextHandler(getContext()));
server.start();
server.join();
}
private static ServletContextHandler getServletContextHandler(WebApplicationContext context) throws IOException {
ServletContextHandler contextHandler = new ServletContextHandler();
contextHandler.setErrorHandler(null);
contextHandler.setContextPath(CONTEXT_PATH);
contextHandler.addServlet(new ServletHolder(new DispatcherServlet(context)), MAPPING_URL);
contextHandler.addEventListener(new ContextLoaderListener(context));
contextHandler.setResourceBase(new ClassPathResource("WEB-INF").getURI().toString());
return contextHandler;
}
private static WebApplicationContext getContext() {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.setConfigLocation(CONFIG_LOCATION);
context.getEnvironment().setDefaultProfiles(DEFAULT_PROFILE);
return context;
}
}
不确定我可能错过什么?当将SpringMVC作为战争部署到tomcat时,servlet-api依赖性不是“编译”。 (类似于targetCompile或者类似的东西),但它现在已经嵌入了jetty,所以我相信这次编译是正常的。
答案 0 :(得分:1)
这样一个愚蠢的问题。 Jetty只是不支持jsp。 Jetty没有对JSP的OOTB支持。我插入Thymeleaf时一切都很完美。对@geoand和@Joakim的高度赞誉