我正在尝试使用maven项目返回网页。这适用于html页面,但我需要JSP。当我尝试在http://localhost:8080/home上加载JSP页面时,控制台表示dispatcherservlet的初始化已经开始完成,但是网页没有返回到浏览器。
这是代码:
主要班级:
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();
}
}
和我的控制器类:
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";
}
}
答案 0 :(得分:0)
@ResponseBody
添加到控制器
@Controller
public class HomeController {
@RequestMapping(value="/home")
@ResponseBody
public String home(){
return "home";
}
}