无法重定向到控制器,总是获得404

时间:2018-01-27 16:24:34

标签: java spring spring-mvc spring-java-config

我正在使用javaconfig来设置这个项目,但我不知道我做错了什么。我打开localhost:8080,显示"hello world ",但是当我打开localhost:8080/homelocalhost:8080/blog/home时,显示404未找到。调试器没有相关信息。

  1. ide:idea 2017
  2. 服务器:tomcat 9
  3. os:mac 10.13
  4. no web.xml only java config
  5. 项目结构: structure

      

    的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/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.xvnyata</groupId>
      <artifactId>blog</artifactId>
      <packaging>war</packaging>
      <version>1.0-SNAPSHOT</version>
      <name>blog Maven Webapp</name>
      <url>http://maven.apache.org</url>
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
    
        <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-commons -->
        <dependency>
          <groupId>org.springframework.data</groupId>
          <artifactId>spring-data-commons</artifactId>
          <version>2.0.2.RELEASE</version>
        </dependency>
    
        <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa -->
        <dependency>
          <groupId>org.springframework.data</groupId>
          <artifactId>spring-data-jpa</artifactId>
          <version>2.0.2.RELEASE</version>
        </dependency>
    
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>5.0.3.RELEASE</version>
        </dependency>
    
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-beans</artifactId>
          <version>5.0.3.RELEASE</version>
        </dependency>
    
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>5.0.3.RELEASE</version>
        </dependency>
    
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-aop</artifactId>
          <version>5.0.3.RELEASE</version>
        </dependency>
    
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-core</artifactId>
          <version>5.0.3.RELEASE</version>
        </dependency>
    
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-aspects</artifactId>
          <version>5.0.3.RELEASE</version>
        </dependency>
    
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context-support -->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context-support</artifactId>
          <version>5.0.3.RELEASE</version>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-expression</artifactId>
          <version>5.0.3.RELEASE</version>
        </dependency>
    
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-orm</artifactId>
          <version>5.0.3.RELEASE</version>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-tx</artifactId>
          <version>5.0.3.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-web</artifactId>
          <version>5.0.3.RELEASE</version>
        </dependency>
    
        <!--spring需要的aop支持-->
        <dependency>
          <groupId>org.aspectj</groupId>
          <artifactId>aspectjrt</artifactId>
          <version>1.8.9</version>
        </dependency>
        <dependency>
          <groupId>org.aspectj</groupId>
          <artifactId>aspectjtools</artifactId>
          <version>1.8.9</version>
        </dependency>
    
        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
        <dependency>
          <groupId>org.mybatis</groupId>
          <artifactId>mybatis</artifactId>
          <version>3.4.5</version>
        </dependency>
    
        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
        <dependency>
          <groupId>org.mybatis</groupId>
          <artifactId>mybatis-spring</artifactId>
          <version>1.3.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
        <dependency>
          <groupId>org.mybatis.generator</groupId>
          <artifactId>mybatis-generator-core</artifactId>
          <version>1.3.6</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
          <version>6.0.6</version>
        </dependency>
    
        <dependency>
          <groupId>org.mybatis.generator</groupId>
          <artifactId>mybatis-generator-maven-plugin</artifactId>
          <version>1.3.6</version>
        </dependency>
        <!--日志注释删除删除清楚删掉-->
        <!-- https://mvnrepository.com/artifact/log4j/log4j -->
        <dependency>
          <groupId>log4j</groupId>
          <artifactId>log4j</artifactId>
          <version>1.2.17</version>
        </dependency>
    
        <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
        <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-log4j12</artifactId>
          <version>1.8.0-beta0</version>
          <scope>test</scope>
        </dependency>
    
        <dependency>
          <groupId>org.junit.jupiter</groupId>
          <artifactId>junit-jupiter-api</artifactId>
          <version>5.0.2</version>
        </dependency>
    
        <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
        <dependency>
          <groupId>com.alibaba</groupId>
          <artifactId>druid</artifactId>
          <version>1.1.6</version>
        </dependency>
    
    
        <!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus -->
        <dependency>
          <groupId>com.baomidou</groupId>
          <artifactId>mybatis-plus</artifactId>
          <version>2.1.8</version>
        </dependency>
    
        <!-- https://mvnrepository.com/artifact/com.ibeetl/beetl -->
        <dependency>
          <groupId>com.ibeetl</groupId>
          <artifactId>beetl</artifactId>
          <version>2.7.26</version>
        </dependency>
    
        <dependency>
          <groupId>com.ibeetl</groupId>
          <artifactId>beetlsql</artifactId>
          <version>2.10.4</version>
        </dependency>
    
        <dependency>
          <groupId>javax</groupId>
          <artifactId>javaee-api</artifactId>
          <version>7.0</version>
        </dependency>
      </dependencies>
      <build>
    
        <finalName>blog</finalName>
        <plugins>
          <!-- Mybatis自动生成插件 -->
          <plugin>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-maven-plugin</artifactId>
            <version>1.3.6</version>
          </plugin>
        </plugins>
      </build>
    
      <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
      </properties>
    
    </project>
    
        enter code here
    
    > BeeTemplateConfig
    
    
    
            package config;
    
        import org.beetl.ext.spring.BeetlGroupUtilConfiguration;
        import org.beetl.ext.spring.BeetlSpringViewResolver;
        import org.springframework.context.annotation.Bean;
        import org.springframework.context.annotation.Configuration;
        import org.springframework.core.io.ClassPathResource;
    
        @Configuration
    
        public class BeeTemplateConfig {
    
            @Bean(initMethod = "init")
            public BeetlGroupUtilConfiguration beetlConfig() {
                BeetlGroupUtilConfiguration beetlGroupUtilConfiguration = new BeetlGroupUtilConfiguration();
                // 如果需要配置 beetl 相关功能,就传入beetl的配置文件
                // 模板的位置就在 beetl.properties 中配置
                // 如 RESOURCE.root=/WEB-INF/templates/beetl/
                beetlGroupUtilConfiguration.setConfigFileResource(new ClassPathResource("/beetl.properties"));
                // 或简单的方式就是使用默认配置文件,用下面的代码设置模板位置
                //beetlGroupUtilConfiguration.setRoot("/WEB-INF/templates/beetl/");
                return beetlGroupUtilConfiguration;
            }
    
            @Bean
            public BeetlSpringViewResolver beetlViewResolver(BeetlGroupUtilConfiguration beetlConfig) {
                BeetlSpringViewResolver beetlSpringViewResolver = new BeetlSpringViewResolver();
                // 设置编码
                beetlSpringViewResolver.setContentType("text/html;charset=UTF-8");
                beetlSpringViewResolver.setSuffix(".btl");
                beetlSpringViewResolver.setConfig(beetlConfig);
                return beetlSpringViewResolver;
            }
        }
    
      

    RootConfig

        package config;
    
    import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Import;
    import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
    
    @Configuration
    @ComponentScan(basePackages = {"config","dao","service","controller"})
    @Import(DruidDatabase.class)
    public class RootConfig {
    
        @Bean
        public BeanNameAutoProxyCreator proxycreate(){
            BeanNameAutoProxyCreator proxycreate = new BeanNameAutoProxyCreator();
            proxycreate.setProxyTargetClass(true);
            proxycreate.setBeanNames("*ServiceImpl");
            proxycreate.setInterceptorNames("transactionInterceptor");
            return proxycreate;
        }
    
    
    
    }
    
    enter code here
    
      

    WebAppInitializer

    enter code here
    
    
      package config;
    
    import org.apache.log4j.Logger;
    import org.springframework.web.filter.CharacterEncodingFilter;
    import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
    
    
    import static org.aspectj.ajde.core.JavaOptions.CHARACTER_ENCODING;
    
    public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    
        private final static Logger LOG = Logger.getLogger(WebAppInitializer.class);
    
        @Override
        protected Class<?>[] getRootConfigClasses() {
            LOG.info("------root配置类初始化------");
            return new Class<?>[] { RootConfig.class };
        }
    
        @Override
        protected Class<?>[] getServletConfigClasses() {
            LOG.info("------web配置类初始化------");
            return new Class<?>[] { WebConfig.class };
        }
    
        @Override
        protected String[] getServletMappings() {
            LOG.info("------映射根路径初始化------");
            return new String[]{ "/" };//请求路径映射,根路径
        }
    
    
    }
    enter code here
    
      

    WebConfig

    package config;
    
    import org.beetl.ext.spring.BeetlGroupUtilConfiguration;
    import org.beetl.ext.spring.BeetlSpringViewResolver;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Import;
    import org.springframework.web.servlet.ViewResolver;
    import org.springframework.web.servlet.config.annotation.*;
    import org.springframework.web.servlet.view.InternalResourceViewResolver;
    
        enter code here
    
    @Configuration
    @EnableWebMvc
    @ComponentScan("controller")
    public class WebConfig extends WebMvcConfigurationSupport{
    
        @Bean
        public ViewResolver viewResolver(){
            InternalResourceViewResolver resolver = new InternalResourceViewResolver();
            resolver.setPrefix("/WEB-INF/view/");
            resolver.setSuffix(".btl");
            return resolver;
        }
    
        //静态资源的处理
        @Override
        public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
            configurer.enable();
        }
    }
    
    enter code here
    

    感谢您的回答。

      

    的HomeController

    package controller;
    
    import config.WebAppInitializer;
    import org.apache.log4j.Logger;
    import org.beetl.core.Configuration;
    import org.beetl.core.GroupTemplate;
    import org.beetl.core.Template;
    import org.beetl.core.resource.StringTemplateResourceLoader;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.servlet.ModelAndView;
    
    import java.io.IOException;
    
    import static org.springframework.web.bind.annotation.RequestMethod.GET;
    
    
    @Controller
    public class homeController {
    
        private final static Logger LOG = Logger.getLogger(WebAppInitializer.class);
    
        @RequestMapping(value = "/console", method = RequestMethod.GET)
        public ModelAndView park() {
            ModelAndView view = new ModelAndView("redirect:/park/index.html");
            return view;
        }
    
        @RequestMapping(value = "/mobile", method = RequestMethod.GET)
        public ModelAndView mobile() {
            ModelAndView view = new ModelAndView("/mobile/login.btl");
            return view;
        }
    
        @RequestMapping(value = "/home", method = RequestMethod.GET)
        public ModelAndView test() {
            ModelAndView view = new ModelAndView("/WEB-INF/view/index.btl");
            return view;
        }
    
    }
    

0 个答案:

没有答案