首页 文章

Spring 4带注释Tomcat部署404错误

提问于
浏览
1

我正在为我的项目使用Spring 4,Servlet 3 API和Tomcat 8 . 我有部署问题 . 我正在尝试在我的VPS中部署WAR包 . 我正在使用Intellij IDEA .

我将我的WAR更改为/ opt / tomcat / webapps路径 . 我可以从Tomcat的应用程序页面看到WAR . 但是当我尝试浏览URL时,我得到404 Not Found错误 .

我使用Spring注释和空web.xml,我也使用Spring Security .

WepAppInitializer.java

public class WepAppInitializer implements WebApplicationInitializer {

@Override
public void onStartup(final ServletContext servletContext) throws ServletException {
    AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
    ctx.register(WebConfig.class);
    ctx.setServletContext(servletContext);
    ServletRegistration.Dynamic dynamic = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
    dynamic.addMapping("/acentecilik");
    dynamic.setLoadOnStartup(1);
    }
}

WebConfig.java

@Configuration
@EnableWebMvc
@EnableTransactionManagement
@ComponentScan("com.decimatech.acentecilik")
@PropertySource("classpath:application.properties")
public class WebConfig extends WebMvcConfigurerAdapter{

@Override
public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(new ThymeleafLayoutInterceptor());
}


@Override
public void addResourceHandlers(ResourceHandlerRegistry registry){
    registry.addResourceHandler("/resources/**").addResourceLocations("/static/");
}

@Bean
@Description("Thymeleaf template resolver serving HTML 5")
public ServletContextTemplateResolver templateResolver() {
    ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
    templateResolver.setPrefix("/WEB-INF/html/");
    templateResolver.setSuffix(".html");
    templateResolver.setTemplateMode("LEGACYHTML5");
    templateResolver.setCharacterEncoding("UTF-8");
    templateResolver.setCacheable(false);

    return templateResolver;
}

@Bean
@Description("Thymeleaf template engine with Spring integration")
public SpringTemplateEngine templateEngine() {
    SpringTemplateEngine templateEngine = new SpringTemplateEngine();
    templateEngine.setTemplateResolver(templateResolver());

    return templateEngine;
}

@Bean
@Description("Thymeleaf view resolver")
public ThymeleafViewResolver viewResolver() {
    ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
    viewResolver.setTemplateEngine(templateEngine());
    viewResolver.setContentType("text/html;charset=UTF-8");
    viewResolver.setCharacterEncoding("utf-8");

    return viewResolver;
}

@Value("${spring.datasource.driver-class-name}")
private String driverClassName;

@Value("${spring.datasource.url}")
private String datasourceUrl;

@Value("${spring.datasource.username}")
private String datasourceUsername;

@Value("${spring.datasource.password}")
private String datasourcePassword;

@Bean(name = "dataSource")
public DataSource getDataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName(driverClassName);
    dataSource.setUrl(datasourceUrl);
    dataSource.setUsername(datasourceUsername);
    dataSource.setPassword(datasourcePassword);

    return dataSource;
}

@Autowired
@Bean(name = "transactionManager")
public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {
    HibernateTransactionManager transactionManager = new HibernateTransactionManager(sessionFactory);

    return transactionManager;
}

@Autowired
@Bean(name = "sessionFactory")
public SessionFactory getSessionFactory(DataSource dataSource) {

    LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);

    sessionBuilder.scanPackages("com.decimatech.acentecilik.model");
    sessionBuilder.addProperties(getHibernateProperties());

    return sessionBuilder.buildSessionFactory();
}

private Properties getHibernateProperties() {
    Properties properties = new Properties();
    properties.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
    properties.put("hibernate.hbm2ddl.auto", "update");
    properties.put("hibernate.show_sql", "true");
    properties.put("hibernate.format_sql", "true");
    properties.put("hibernate.use_sql_comments", "true");
    properties.put("hibernate.enable_lazy_load_no_trans", "true");
    return properties;
}

@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
}
}

web.xml

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                  http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
     version="3.1"
     metadata-complete="true">

<display-name>Acentecilik</display-name>
<description>
    Acentecilik
</description>
</web-app>

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.decimatech</groupId>
<artifactId>acentecilik</artifactId>
<version>1.0-SNAPSHOT</version>

//Some package dependencies

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.7</version>
            <configuration>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
    </plugins>
</build>

</project>

以下是我使用的设置的截图 .

IDEA WAR Settings
IDEA WAR Settings

Project Structures

Project Structures

Tomcat Manage Apps Page

Tomcat Manage Apps Page

And 404 Error
404 Error

我怎么解决这个问题?

这是我的catalina日志文件的输出 . 我将WAR的所有权从root更改为tomcat用户 . 但还是同样的问题 .

02-Oct-2015 16:03:23.800 SEVERE [localhost-startStop-10] org.apache.catalina.startup.ContextConfig.beforeStart异常修复docBase for context [/ acentecilik] java.io.IOException:无法创建目录org.apache.catalina.startup.ExpandWar.expand(ExpandWar.java:115)的org.apache.catalina.startup.ContextConfig.fixDocBase(ContextConfig.java:618)org上的[/ opt / tomcat / webapps / acentecilik] .apache.catalina.startup.ContextConfig.beforeStart(ContextConfig.java:744)org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:307)org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport) .java:95)org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)位于org.apache.catalina的org.apache.catalina.util.LifecycleBase.setStateInternal(LifecycleBase.java:402) . util.LifecycleBase.start(LifecycleBase.java:147)位于org.apache.catalina.c的org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:725) ore.ContainerBase.addChild(ContainerBase.java:701)org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:945) at org.apache.catalina.startup.HostConfig $ DeployWar.run(HostConfig.java:1798)at java.util.concurrent.Executors $ RunnableAdapter.call(Executors.java:471)at java.util.concurrent.FutureTask.run (FutureTask.java:262)java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145),位于java.lang.Thread的java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:615) . run(Thread.java:745)02-Oct-2015 16:03:58.561 INFO [localhost-startStop-10] org.apache.jasper.servlet.TldScanner.scanJars至少有一个JAR被扫描用于尚未包含TLD的TLD . 为此 Logger 启用调试日志记录,以获取已扫描但未在其中找到TLD的完整JAR列表 . 在扫描期间跳过不需要的JAR可以缩短启动时间和JSP编译时间 . 02-Oct-2015 16:03:58.649 INFO [localhost-startStop-10] org.apache.catalina.startup.HostConfig.deployWAR Web应用程序存档/opt/tomcat/webapps/acentecilik.war的部署已完成34,860 ms

Edit 我将/ opt / tomcat / webapps所有权更改为tomcat,现在我没有收到404错误 . 但现在我有了这个 .

INFO [http-nio-8080-exec-17] org.apache.catalina.core.ApplicationContext.log没有在类路径上检测到Spring WebApplicationInitializer类型

1 回答

  • 3

    例外说

    org.apache.catalina.startup.ContextConfig.beforeStart异常修复docBase for context [/ acentecilik] java.io.IOException: Unable to create the directory

    你已经说过tomcat在 /opt 之下 . 所以我认为tomcat没有足够的权限在自己的目录 /opt/tomcat 上创建(写权限) .

    如果您为启动tomcat的用户(很可能是您自己的用户)提供写入权限,那么问题就会消失 .

    sudo chmod -R 0744 /opt/tomcat

相关问题