简体   繁体   English

如何从maven项目返回网页

[英]how to return a web page from a maven project

I am trying to return web pages using a maven project. 我正在尝试使用maven项目返回网页。 This works for html pages, but I need JSP. 这适用于html页面,但我需要JSP。 When I try to load a JSP page on http://localhost:8080/home the console says that the initialization of the dispatcherservlet has started an completed, but the webpage is not returned to the browser. 当我尝试在http:// localhost:8080 / home上加载JSP页面时,控制台会说dispatcherservlet的初始化已经开始完成,但是网页没有返回到浏览器。

here's the code: 这是代码:

the main class: 主要课程:

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
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.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@SpringBootApplication
@Configuration
@ComponentScan(basePackages="com.example")
@EnableWebMvc
public class DemoApplication extends WebMvcConfigurerAdapter{

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Bean
    public ViewResolver getViewResolver(){
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("static/templates/");
        resolver.setSuffix(".jsp");
        return resolver
   }



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

and my controller class: 和我的控制器类:

package com.example.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HomeController {

     @RequestMapping(value="/home")
        public String home(){
            return "home";
        }

}

it would be good if you provide what response you got. 如果你提供你得到的回应会很好。 you can try to add @ResponseBody to controller 您可以尝试将@ResponseBody添加到控制器

@Controller
public class HomeController {
     @RequestMapping(value="/home")
     @ResponseBody
        public String home(){
            return "home";
        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM