gradle + jetty + Spring MVC 无法启动项目

2018-10-08 18:26:37 +08:00
 chuanqirenwu

目前正在看 《 Spring in action 》第四版,想跟着书中第五章开始的实战项目做一下,但在使用 gradle 和 jetty 启动项目时遇到了一些问题。

代码基本抄书上的,贴几个关键的配置文件:

这是 sevelet 的配置文件

package spittr.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import spittr.web.WebConfig;

public class SpitterWebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[]{RootConfig.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[]{WebConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }

}

这是根配置文件:

package spittr.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@ComponentScan(
        basePackages = {"spittr"},
        excludeFilters = {
                @Filter(type = FilterType.ANNOTATION, value = EnableWebMvc.class)
        })
public class RootConfig {
}

注意全部使用基于 java 的配置,没有 xml 文件。

build.gradle 如下:

apply plugin: 'war'
apply plugin: 'idea'
apply plugin: 'java'
apply from: 'gretty.plugin'

dependencies {
    compile "org.springframework:spring-webmvc:4.0.7.RELEASE"
    compile "javax.servlet:jstl:1.2"
//    providedCompile "javax.servlet.jsp:jsp-api:2.1"
//    providedCompile "javax.el:javax.el-api:2.2.4"
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
    maven { url 'http://maven.springframework.org/release' }
    maven { url 'http://maven.springframework.org/milestone' }
    maven { url 'http://maven.springframework.org/snapshot' }
    maven { url 'http://download.java.net/maven/2' }
    mavenCentral()

}

war {
    baseName = 'spittr'
    version = '0.1.0'
}

当我运行 gradle appRun 命令启动服务器时,得到如下的信息:

YangdeMBP:Spittr yangxg$ ./gradlew appRun
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.codehaus.groovy.reflection.CachedClass (file:/Users/yangxg/.gradle/caches/modules-2/files-2.1/org.codehaus.groovy/groovy/2.4.15/74b7e0b99526c569e3a59cb84dbcc6204d601ee6/groovy-2.4.15.jar) to method java.lang.Object.finalize()
WARNING: Please consider reporting this to the maintainers of org.codehaus.groovy.reflection.CachedClass
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
18:17:47 INFO  Spring WebApplicationInitializers detected on classpath: [spittr.config.SpitterWebInitializer@760cf594]
18:17:47 INFO  Initializing Spring root WebApplicationContext
10 月 08, 2018 6:17:47 下午 org.springframework.web.context.ContextLoader initWebApplicationContext
信息: Root WebApplicationContext: initialization started
10 月 08, 2018 6:17:48 下午 org.springframework.web.context.support.AnnotationConfigWebApplicationContext prepareRefresh
信息: Refreshing Root WebApplicationContext: startup date [Mon Oct 08 18:17:48 CST 2018]; root of context hierarchy
10 月 08, 2018 6:17:48 下午 org.springframework.web.context.support.AnnotationConfigWebApplicationContext loadBeanDefinitions
信息: Registering annotated classes: [class spittr.config.RootConfig]
10 月 08, 2018 6:17:48 下午 org.springframework.web.context.ContextLoader initWebApplicationContext
信息: Root WebApplicationContext: initialization completed in 301 ms
18:17:48 INFO  Initializing Spring FrameworkServlet 'dispatcher'
10 月 08, 2018 6:17:48 下午 org.springframework.web.servlet.DispatcherServlet initServletBean
信息: FrameworkServlet 'dispatcher': initialization started
10 月 08, 2018 6:17:48 下午 org.springframework.web.context.support.AnnotationConfigWebApplicationContext prepareRefresh
信息: Refreshing WebApplicationContext for namespace 'dispatcher-servlet': startup date [Mon Oct 08 18:17:48 CST 2018]; parent: Root WebApplicationContext
10 月 08, 2018 6:17:48 下午 org.springframework.web.context.support.AnnotationConfigWebApplicationContext loadBeanDefinitions
信息: Registering annotated classes: [class spittr.web.WebConfig]
10 月 08, 2018 6:17:48 下午 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping registerHandlerMethod
信息: Mapped "{[/],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String spittr.web.HomeController.home()
10 月 08, 2018 6:17:48 下午 org.springframework.web.servlet.handler.SimpleUrlHandlerMapping registerHandler
信息: Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler]
10 月 08, 2018 6:17:48 下午 org.springframework.web.servlet.DispatcherServlet initServletBean
信息: FrameworkServlet 'dispatcher': initialization completed in 510 ms
18:17:48 INFO  Jetty 9.2.24.v20180105 started and listening on port 8080
18:17:48 INFO  Spittr runs at:
18:17:48 INFO    http://localhost:8080/Spittr

> Task :appRun
Press any key to stop the server.
<===========--> 87% EXECUTING [35s]
> :appRun

比较奇怪的一个地方是进度指示器卡在 87% 不在动,访问 http://localhost:8080/Spittr 时报错信息如下:

org.apache.jasper.JasperException: /WEB-INF/views/home.jsp(1,63) PWC6188: The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar files deployed with this application
	at org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:92)
	at org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:378)
	at org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:172)
	at org.apache.jasper.compiler.TagLibraryInfoImpl.generateTLDLocation(TagLibraryInfoImpl.java:431)
	at org.apache.jasper.compiler.TagLibraryInfoImpl.<init>(TagLibraryInfoImpl.java:240)
	at org.apache.jasper.compiler.Parser.parseTaglibDirective(Parser.java:502)
	at org.apache.jasper.compiler.Parser.parseDirective(Parser.java:582)
	at org.apache.jasper.compiler.Parser.parseElements(Parser.java:1657)
	at org.apache.jasper.compiler.Parser.parse(Parser.java:185)
	at org.apache.jasper.compiler.ParserController.doParse(ParserController.java:244)
	at org.apache.jasper.compiler.ParserController.parse(ParserController.java:145)
	at org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:212)
	at org.apache.jasper.compiler.Compiler.compile(Compiler.java:451)
	at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:625)
	at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:375)
	at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:473)
	at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:377)
	at org.eclipse.jetty.jsp.JettyJspServlet.service(JettyJspServlet.java:103)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
	at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:812)
	at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:587)
	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
	at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:595)
	at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:223)
	at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1127)
	at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:515)
	at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185)
	at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1061)
	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
	at org.eclipse.jetty.server.Dispatcher.forward(Dispatcher.java:191)
	at org.eclipse.jetty.server.Dispatcher.forward(Dispatcher.java:72)
	at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:209)
	at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:267)
	at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1221)
	at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1005)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:952)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
	at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:812)
	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1669)
	at org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter.doFilter(WebSocketUpgradeFilter.java:201)
	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652)
	at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:585)
	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
	at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:577)
	at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:223)
	at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1127)
	at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:515)
	at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185)
	at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1061)
	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
	at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:215)
	at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
	at org.eclipse.jetty.server.Server.handle(Server.java:499)
	at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:311)
	at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:258)
	at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:544)
	at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635)
	at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555)
	at java.base/java.lang.Thread.run(Thread.java:844)

但显然 gradle 在 build 项目时已经为我安装了 jstl 1.2,这是为什么呢?

1252 次点击
所在节点    Java
5 条回复
mysticzt123
2018-10-08 22:25:20 +08:00
你怎么搞 java 了
GTim
2018-10-09 07:06:00 +08:00
看第一句,贴出你的 home.jsp
chuanqirenwu
2018-10-09 10:06:33 +08:00
@GTim 已附上
GTim
2018-10-09 12:18:36 +08:00
忘记加载 jstl 包
chuanqirenwu
2018-10-09 13:40:16 +08:00
@GTim 指的是哪个地方忘记加载了呢?没有 import 还是?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/495690

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX