Maven部署:请求的资源不可用

时间:2014-02-25 10:06:15

标签: java spring maven tomcat spring-java-config

说明 我在Apache Tomcat7上使用Maven部署我的Java Config Spring应用程序时遇到问题。如果我在Eclipse IDE中使用tomcat7:run目标,则应用程序可以正常运行。但是当我切换到tomcat7:deploy时,我总是面对“请求的资源(/appName-1.0/login)不可用。” - 消息。

此外,Spring无法访问与JS或IMG文件夹位于同一级别的CSS文件。 tomcat7:run目标也会出现此问题。

分析: 似乎servlet正常启动,因为我总是被重定向到/ login,它在Security Config中定义为loginPage()。

来源:

Eclipse中的目录结构:

appName
|- src/main/java (all java classes)
|- src/main/resources (several property files)
|- src/main/webapp
   |- img 
   |- js
   |- less
   |-WEB-INF
       |- pages (several jsp files in folders)
       |- tiles.xml
|- target

WAR文件中的目录结构:

war
|- css
|- img
|- js
|- less
|- META-INF
|- WEB-INF
    |- classes
        |- com (java classes)
        |- img
        |- js
        |- less
        |- META-INF
        |- WEB-INF
        |- database.properties
        |- log4j.properties
        |- webapplication.properties
    |- lib
    |- pages
    |- tiles.xml

的pom.xml

<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>

    <name>appName</name>
    <groupId>com.test.appName</groupId>
    <artifactId>appName</artifactId>
    <version>1.0</version>
    <packaging>war</packaging>

    <properties> ... </properties>

    <repositories> ... </repositories>

    <dependencies> ... </dependencies>

    <profiles> ... </profiles>

    <build>
        <!-- General Build Settings -->
        <finalName>${project.artifactId}-${project.version}</finalName>
        <directory>${project.basedir}/target</directory>
        <sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
        <outputDirectory>${project.build.directory}/classes</outputDirectory>
        <testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
        <testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>
        <resources>
            <resource>
                <directory>${project.basedir}/src/main/resources</directory>
            </resource>
        </resources>
        <testResources>
            <testResource>
                <directory>${project.basedir}/src/test/resources</directory>
            </testResource>
        </testResources>

        <!-- PLUGINS -->
        <plugins>
            <!-- Less CSS Plugin -->
            <plugin>
                <groupId>org.lesscss</groupId>
                <artifactId>lesscss-maven-plugin</artifactId>
                <version>${maven.less.compiler.plugin.version}</version>
                <configuration>
                    <sourceDirectory>${project.basedir}/src/main/webapp/less</sourceDirectory>
                    <outputDirectory>${project.build.directory}/${project.build.finalName}/css</outputDirectory>
                    <compress>true</compress>
                    <force>true</force>
                    <includes>
                        <include>main.less</include>
                    </includes>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!-- Maven WAR Plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>${maven.war.plugin.version}</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <!-- Maven Tomcat Plugin -->
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>${maven.tomcat.plugin.version}</version>
                <configuration>
                    <url>${tomcat-url}</url>
                    <username>${tomcat-username}</username>
                    <password>${tomcat-password}</password>
                    <path>/${project.build.finalName}</path>
                    <update>true</update>
                </configuration>
            </plugin>
            <!-- Maven Resources Plugin - Copy properties -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>${maven.resources.plugin.version}</version>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${basedir}/target/classes</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>${environment-resources}</directory>
                                    <filtering>true</filtering>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <!-- Maven Compiler -->
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven.compiler.plugin.version}</version>
                <configuration>
                    <source>${compiler.version}</source>
                    <target>${compiler.version}</target>
                </configuration>
            </plugin>
            <!-- Maven AspectJ Plugin - Autowire new keyword instances -->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>${maven.aspectj.plugin.version}</version>
                <configuration>
                    <complianceLevel>${compiler.version}</complianceLevel>
                    <encoding>${project.build.sourceEncoding}</encoding>
                    <aspectLibraries>
                        <aspectLibrary>
                            <groupId>org.springframework</groupId>
                            <artifactId>spring-aspects</artifactId>
                        </aspectLibrary>
                    </aspectLibraries>
                    <!-- Xlint set to warning alleviate some issues, such as SPR-6819. Please 
                        consider it as optional. https://jira.springsource.org/browse/SPR-6819 -->
                    <Xlint>warning</Xlint>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

WebAppConfig:

@Configuration
@EnableWebMvc
@EnableScheduling
@EnableSpringConfigured
@PropertySource("classpath:webapplication.properties")
@ComponentScan("com.test.appName")
public class WebAppConfig extends WebMvcConfigurerAdapter {

    private static final String PROPERTY_NAME_MESSAGESOURCE_BASENAME = "message.source.basename";
    private static final String PROPERTY_NAME_MESSAGESOURCE_USE_CODE_AS_DEFAULT_MESSAGE = "message.source.use.code.as.default.message";
    private static final String PROPERTY_NAME_MESSAGESOURCE_DEFAULT_ENCODING = "message.source.default.encoding";

    @Resource
    private Environment environment;
    @Autowired
    private LocalSessionFactoryBean sessionFactory;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/js/**").addResourceLocations("/js/");
        registry.addResourceHandler("/css/**").addResourceLocations("/css/");
        registry.addResourceHandler("/img/**").addResourceLocations("/img/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("/webjars/");
        registry.addResourceHandler("/pages/**").addResourceLocations("/WEB-INF/pages/");
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Bean
    public UrlBasedViewResolver viewResolver() {
        UrlBasedViewResolver viewResolver = new UrlBasedViewResolver();
        viewResolver.setViewClass(TilesView.class);
        return viewResolver;
    }

    @Bean
    public TilesConfigurer tilesConfigurer() {
        TilesConfigurer tilesConfigurer = new TilesConfigurer();
        tilesConfigurer.setDefinitions(new String[] { "/WEB-INF/tiles.xml" });
        tilesConfigurer.setCheckRefresh(true);
        return tilesConfigurer;
    }
}

安全配置:

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled=true, prePostEnabled=true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired private UserDetailsService userDetailService;
    @Autowired private DataSource dataSource;

    @Override
    public void configure(WebSecurity web) throws Exception {
        web
            .ignoring()
                .antMatchers("/js/**","/css/**","/img/**","/webjars/**","/pages/**");
    }
}

Layout.jsp

<!-- custom css includes-->
<link rel="stylesheet" href="css/main.css">

0 个答案:

没有答案