V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
chuanqirenwu
V2EX  ›  Java

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

  •  
  •   chuanqirenwu · 2018-10-08 18:26:37 +08:00 · 1293 次点击
    这是一个创建于 2272 天前的主题,其中的信息可能已经有所发展或是发生改变。

    目前正在看 《 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,这是为什么呢?

    第 1 条附言  ·  2018-10-09 10:06:16 +08:00

    home.jsp 的代码如下:

    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <%@ page session="false" %>
    <html>
    <head>
        <title>Spitter</title>
        <link rel="stylesheet"
              type="text/css"
              href="<c:url value="/resources/style.css" />">
    </head>
    <body>
    <h1>Welcome to Spitter</h1>
    
    <a href="<c:url value="/spittles" />">Spittles</a> |
    <a href="<c:url value="/spitter/register" />">Register</a>
    </body>
    </html>
    
    5 条回复    2018-10-09 13:40:16 +08:00
    mysticzt123
        1
    mysticzt123  
       2018-10-08 22:25:20 +08:00
    你怎么搞 java 了
    GTim
        2
    GTim  
       2018-10-09 07:06:00 +08:00 via iPhone
    看第一句,贴出你的 home.jsp
    chuanqirenwu
        3
    chuanqirenwu  
    OP
       2018-10-09 10:06:33 +08:00
    @GTim 已附上
    GTim
        4
    GTim  
       2018-10-09 12:18:36 +08:00 via iPhone
    忘记加载 jstl 包
    chuanqirenwu
        5
    chuanqirenwu  
    OP
       2018-10-09 13:40:16 +08:00
    @GTim 指的是哪个地方忘记加载了呢?没有 import 还是?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2830 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 06:02 · PVG 14:02 · LAX 22:02 · JFK 01:02
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.