簡體   English   中英

Spring JavaConfig和Tomcat 8

[英]Spring JavaConfig and Tomcat 8

我有一個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部署時沒有web.xml,但實際上並沒有提供解決方案。

我的項目的詳細信息如下:

資料來源

在此處輸入圖片說明

配置文件

@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";
  }

}

對不起,我太匆忙了

似乎根本沒有加載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來指定在哪里找到spring配置類。

無論如何,您都可以在這里找到一個有效的示例,我已將其成功部署到Tomcat 8.0.0.39和8.5.4

希望它有用

安傑洛

原來我沒有在代碼中做任何錯誤。 Tomcat服務器被配置為使用嚴格的servlet合規性。

org.apache.catalina.STRICT_SERVLET_COMPLIANCE=true 

此設置會影響其他多個屬性( 請參見此處 )。 其中之一是“任何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。

pom.xml finalName添加為ROOT 這將在目標文件夾中創建ROOT.war文件。

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

之后,如果您在雄貓中部署ROOT.war文件。 然后您的訪問網址將是http:// localhost:8080

希望它能解決您的問題。

要進行進一步檢查,請通過http:// localhost:8080 / manager / html進行操作 ,它將要求輸入用戶名和密碼。 檢查是否已設置。

如果未設置,請完成本教程: 無法按照E-Riz的建議訪問Tomcat 8 Manager App

有關完整的示例,請閱讀本教程: http : //websystique.com/springmvc/spring-4-mvc-helloworld-tutorial-annotation-javaconfig-full-example/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM