【问题标题】:Spring JavaConfig and Tomcat 8Spring JavaConfig 和 Tomcat 8
【发布时间】:2016-10-23 14:02:18
【问题描述】:

我有一个 Spring 4 Web 应用程序 (webapp-module.war) 在 Eclipse 中使用 Java 8、tomcat 8 和 JavaConfig(无 web.xml)在本地工作和运行:

但是当我在远程 Ubuntu 服务器上部署到 tomcat 8(我在 eclipse 中本地使用的相同版本)时,我得到:

我验证了正确的主机和端口。日志中没有错误(/var/lib/tomcat8/logs/catalina.out)

Jun 21, 2016 10:32:44 PM org.apache.catalina.startup.HostConfig undeploy
INFO: Undeploying context [/webapp-module]
Jun 21, 2016 10:32:44 PM org.apache.catalina.startup.HostConfig deployWAR
INFO: Deploying web application archive /var/lib/tomcat8/webapps/webapp-module.war
Jun 21, 2016 10:32:46 PM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Jun 21, 2016 10:32:46 PM org.apache.catalina.startup.HostConfig deployWAR
INFO: Deployment of web application archive /var/lib/tomcat8/webapps/webapp-module.war has finished in 1,870 ms
root@vmi63860:/var/lib/tomcat8/logs# 

访问日志包含:

root@vmi63860:/var/log/tomcat8# cat localhost_access_log.2016-06-22.txt 
xx.xxx.xxx.xx - - [22/Jun/2016:22:36:00 +0200] "GET /webapp-module/ HTTP/1.1" 404 1040
xx.xxx.xxx.xx - - [22/Jun/2016:22:36:00 +0200] "GET /favicon.ico HTTP/1.1" 404 1034
xx.xxx.xxx.xx - - [22/Jun/2016:22:36:50 +0200] "GET /webapp-module/hello HTTP/1.1" 404 1050

其中 xx.xxx.xxx.xx 是我尝试在浏览器中访问 Web 应用程序的本地计算机的 IP。

我看了看: Spring Java Config: Tomcat deploy without web.xml 但它并没有真正提供解决方案。

我的项目详情如下:

来源

Config.java

@Configuration // Marks this class as configuration
// Specifies which package to scan
@ComponentScan("com.samples")
// Enables Spring's annotations
@EnableWebMvc
public class Config {

  @Bean
  public UrlBasedViewResolver setupViewResolver() {
    UrlBasedViewResolver resolver = new UrlBasedViewResolver();
    resolver.setPrefix("/WEB-INF/jsp/");
    resolver.setSuffix(".jsp");
    resolver.setViewClass(JstlView.class);
    return resolver;
  }

}

WebInitializer.java

public class WebInitializer implements WebApplicationInitializer {

  @Override
  public void onStartup(ServletContext servletContext) throws ServletException {

    AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
    ctx.register(Config.class);
    ctx.setServletContext(servletContext);

    Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
    servlet.addMapping("/");
    servlet.setLoadOnStartup(1);

  }

}

HelloController.java

@Controller
public class HelloController {

  @RequestMapping("/")
  public String home() {
    return "index";
  }

  @RequestMapping("/hello")
  public String showhello(ModelMap model) {
    model.addAttribute("message", "Hello Spring MVC Framework!");
    return "hello";
  }

}

【问题讨论】:

  • 检查是否在 localhost_access_log.*.txt 内的 tomcat logs 文件夹
  • 不确定您的意思,但在尝试从浏览器访问 Web 应用程序后,我使用服务器上最新访问日志中的内容更新了问题。虽然没有显示任何错误
  • 它们可能看起来像愚蠢的问题,但是名为 webapp-module 的战争文件吗?我想你在 Eclipse 中也在 tomcat 实例中运行它,tomcat 版本是否匹配?
  • 你能检查远程服务器上的 Tomcat 管理器应用程序 (/manager) 吗?如果是这样,您的应用程序/webapp-module 是否显示为已部署?如果您无法访问/manager,请参阅stackoverflow.com/a/38794114/639520
  • 您可以发布您的应用程序日志文件吗?可能是您在启动时遇到错误,因此应用程序从未启动。

标签: java eclipse spring spring-mvc tomcat


【解决方案1】:

抱歉之前我太仓促了

似乎根本没有加载spring上下文。

我猜问题出在这段代码中:

public class WebInitializer implements WebApplicationInitializer {

  @Override
  public void onStartup(ServletContext servletContext) throws ServletException {

    AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
    ctx.register(Config.class);
    ctx.setServletContext(servletContext);

    Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
    servlet.addMapping("/");
    servlet.setLoadOnStartup(1);

  }

}

你用过ctx.register(Config.class);

无论如何我总是使用这种初始化:

public class AppInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        // Create the 'root' Spring application context
        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        rootContext.scan("com.spring");
        rootContext.setConfigLocations(new String[]{"com.spring.config.WebAppContextConfig", "com.spring.config.AppConfig"});
        // Manages the lifecycle of the root application context
        servletContext.addListener(new ContextLoaderListener(rootContext));

        // Declare dispatcher servlet. Handles requests into the application
        ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher",
                new DispatcherServlet(rootContext));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");

    }

}

如您所见,我使用rootContext.setConfigLocations 来指定在哪里可以找到弹簧配置类

在任何情况下Here 你都可以找到一个工作示例,我成功地将它部署在 tomcat 8.0.39 和 8.5.4 上

希望有用

安杰洛

【讨论】:

  • 能否在示例中包含 context.xml?
  • @sonance207 在这个例子中我没有 context.xml 文件...对不起
【解决方案2】:

事实证明我在代码中没有做错任何事情。 Tomcat 服务器配置为使用严格的 servlet 合规性。

org.apache.catalina.STRICT_SERVLET_COMPLIANCE=true 

此设置会影响多个其他属性 (see here)。其中之一是“任何 Context 元素的 resourceOnlyServlets 属性”。在应用程序的 context.xml 中将此值设置为“jsp”即可解决问题。

期望资源存在的 Servlet 名称的逗号分隔列表(如 /WEB-INF/web.xml 中所用)。确保在不存在资源时不使用与期望存在资源的 Servlet(例如 JSP Servlet)关联的欢迎文件。这可以防止由于 Servlet 3.0 规范第 10.10 节中对欢迎文件映射的澄清而引起的问题。如果 org.apache.catalina.STRICT_SERVLET_COMPLIANCE 系统属性设置为 true,则该属性的默认值为空字符串,否则默认值为 jsp。

【讨论】:

    【解决方案3】:

    在您的pom.xml 中添加finalName 作为ROOT。这将在您的目标文件夹中创建 ROOT.war 文件。

        <build>
            <plugins>
    <!--All plugins are here -->
            </plugins>
            <finalName>ROOT</finalName>
        </build>
    

    之后,如果您在 tomcat 中部署 ROOT.war 文件。然后您的访问网址将是 http://localhost:8080

    希望它能解决您的问题。

    如需进一步检查,请通过http://localhost:8080/manager/html 它将要求输入用户名和密码。检查是否已设置。

    如果未设置,请按照E-Riz的建议阅读本教程:Can't access Tomcat 8 Manager App

    如需完整示例,请阅读本教程:http://websystique.com/springmvc/spring-4-mvc-helloworld-tutorial-annotation-javaconfig-full-example/

    【讨论】:

    • 我已经尝试了 websystique 示例。它在 tomcat / Win 10 开发机器上运行,但在 tomcat /RHL 服务器上运行。两者都有 TC 8.0.18。 PS:出于安全原因,我们的组织政策不允许使用经理应用程序,因此这不是一个选项。 :(
    • @zendu 对于服务器部署,不允许管理应用程序。您是否尝试过使用 ROOT.war 作为服务器?请检查。如果存在错误,请分享日志文件。
    猜你喜欢
    • 1970-01-01
    • 2020-02-26
    • 1970-01-01
    • 1970-01-01
    • 2016-03-28
    • 2013-02-03
    • 2014-05-02
    • 2014-12-11
    相关资源
    最近更新 更多