Spring Boot-无法使用控制器和配置设置查看网页

时间:2018-10-23 02:33:12

标签: java spring spring-mvc spring-boot

我无法在Spring Boot应用程序中查看任何文件。我让Spring Boot Servlet Initializer类正确地加载了我的类,并且有一个映射到该URL的控制器。我知道,因为我可以在日志中看到它。

WebConfig.java

package com.project.LNUProject.config;

import com.project.LNUProject.utils.ViewNames;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.UrlBasedViewResolver;

@EnableWebMvc
@Configuration
@ComponentScan(basePackages = "com.project.LNUProject.config")
@Slf4j
public class WebConfig implements WebMvcConfigurer {

    // == constants ==
    public static final String RESOLVER_PREFIX = "/WEB-INF/view/";
    public static final String RESOLVER_SUFFIX =".jsp";

    // == bean methods
    @Bean
    public ViewResolver viewResolver() {
        UrlBasedViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix(RESOLVER_PREFIX);
        viewResolver.setSuffix(RESOLVER_SUFFIX);
        return viewResolver;
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        log.info("registry add properly");
        registry.addViewController("/").setViewName(ViewNames.HOME);
    }
}

DevTrainingController.java

package com.project.LNUProject.Controller;

import com.project.LNUProject.utils.Mappings;
import com.project.LNUProject.utils.ViewNames;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

    @Slf4j
    @Controller
    public class DevTrainingController implements CRUDController{
        @Override
        @GetMapping(Mappings.DEV_TRAINING_TABLE)
        public String table() {
            return ViewNames.DEV_TRAINING_TABLE;
        }

        @Override
        public String edit() {
            return "";
        }

        @Override
        public String delete() {
            return "";
        }

        @GetMapping("/")
        public String home() {
            log.info("Home template map");
            return ViewNames.HOME;
        }
    }

日志说

2018-10-22 21:09:32,974 [main] [INFO] oswsmmaRequestMappingHandlerMapping-将“ {[/ DevTrainingTable],methods = [GET]}”映射到公共java.lang.String com.project.LNUProject。 Controller.DevTrainingController.table() 2018-10-22 21:09:32,977 [main] [INFO] o.s.w.s.m.m.a.RequestMappingHandlerMapping-映射的“ {[/],methods = [GET]}”  到公共java.lang.String com.project.LNUProject.Controller.DevTrainingController.home()

...

2018-10-22 21:10:19,837 [main] [INFO] com.project.LNUProject.config.WebConfig-注册表正确添加

但是,当我转到http://localhost:8080/LNU-Project/时,这是页面404错误。这是我在github

上的代码的链接

1 个答案:

答案 0 :(得分:0)

该模块的名称称为WEB,因此已映射到http://localhost:8080/WEB/