Статус HTTP 404 - /WEB-INF/jsp/welcome.html - PullRequest
0 голосов
/ 26 февраля 2020

Невозможно разрешить JSP страницу, хотя она отлично работает с простой строкой

Вот моя структура папок проекта enter image description here

Я использую следующий деп. запустить приложение

compile ('org. apache .tomcat.embed: tomcat-embed-jasper: 8.0.47')

@Configuration
@EnableWebMvc
@ComponentScan
public class SpringAppConfig extends WebMvcConfigurerAdapter implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext container) {
        // Create the 'root' Spring application context
        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        rootContext.register(SpringAppConfig.class);

        // Manage the lifecycle of the root application context
        container.addListener(new ContextLoaderListener(rootContext));

        // Create the dispatcher servlet's Spring application context
        AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
        dispatcherContext.register(SpringAppConfig.class);

        ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher",
                new DispatcherServlet(dispatcherContext));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");

    }

    @Bean
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
        internalResourceViewResolver.setPrefix("/WEB-INF/jsp/");
        internalResourceViewResolver.setSuffix(".jsp");
        return internalResourceViewResolver;
    }

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

}
@RestController
@RequestMapping("/app")
public class StudentController {

    @RequestMapping("/hello")    // this is working
    public String sayHello() {
        return "Hello from Spring 5 and embedded Tomcat 8!";
    }


    @RequestMapping(value = "/page", method = RequestMethod.GET)
    public ModelAndView getPage() {
        ModelAndView mav = new ModelAndView("welcome");
        mav.addObject("firstname", "Amit");
        mav.addObject("lastname", "Shah");
        return mav;
    }

}

, получив 404 со следующими подробности, когда я нажимаю http://localhost: 8080 / app / page

ошибка: сообщение /WEB-INF/jsp/welcome.jsp description Запрошенный ресурс недоступен. Томакат

public class MyMvcApp {
    private static final int PORT = 8080;

    public static void main(String[] args) throws ServletException, LifecycleException {
        String appBase = ".";
        Tomcat tomcat = new Tomcat();
        tomcat.setBaseDir(createTempDir());
        tomcat.setPort(PORT);
        tomcat.getHost().setAppBase(appBase);
        tomcat.addWebapp("", appBase);
        tomcat.start();
        tomcat.getServer().await();
    }

    private static String createTempDir() {
        try {
            File tempDir = File.createTempFile("tomcat.", "." + PORT);
            tempDir.delete();
            tempDir.mkdir();
            tempDir.deleteOnExit();
            return tempDir.getAbsolutePath();
        } catch (IOException ex) {
            throw new RuntimeException(
                    "Unable to create tempDir. java.io.tmpdir is set to " + System.getProperty("java.io.tmpdir"),
                    ex
            );
        }
    }

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...