Spring JavaConfig和Tomcat 8

时间:2016-06-21 20:47:33

标签: java eclipse spring spring-mvc tomcat

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

enter image description here

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

enter image description here

我验证了正确的主机和端口。日志中没有错误(/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是我本地计算机的IP,我尝试在浏览器中访问该网站。

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

以下项目详情:

来源

enter image description here

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

}

3 个答案:

答案 0 :(得分:4)

对不起以前我太仓促了

似乎春天的情境根本没有加载。

我想问题出现在这段代码中:

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上成功部署它的工作示例

希望它有用

安吉洛

答案 1 :(得分:0)

原来我在代码中没有做错任何事。 Tomcat服务器配置为使用严格的servlet兼容性。

org.apache.catalina.STRICT_SERVLET_COMPLIANCE=true 

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

  

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

答案 2 :(得分:-1)

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

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

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

希望它能解决您的问题。

如需进一步检查,请查看http://localhost:8080/manager/html 它会调用用户名和密码。检查是否已设置。

如果未设置,请按照Can't access Tomcat 8 Manager App

的建议完成本教程:E-Riz

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