spring boot加载jsp为txt?

时间:2015-06-04 10:05:48

标签: java spring-boot

我使用spring boot来做一个简单的网页。 以下是代码:

的src /主/爪哇/ Application.java

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

的src /绵/ JAVA / Config.java

@Configuration
@EnableWebMvc
public class Config extends WebMvcConfigurerAdapter
{
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer)
    {
        configurer.enable();
    }

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

的src /主/爪哇/ TestController.java

@Controller
public class TestController
{
    String name = "Peter!";

    @RequestMapping("/test")
    public String admin(ModelAndView mv)
    {
        System.out.println("in controller");

        mv.addObject("name", name);
        return "test";
    }
}

的src /主/ web应用/ WEB-INF /视图/ test.jsp的

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Just for test</title>
</head>
<body>
    ${name}
</body>
</html>

我从Application.java启动项目, 并将http://localhost:8080/test放入浏览器中, 但浏览器只显示test.jsp的源代码。 似乎spring boot将jsp视为txt文件... 如何解决?

1 个答案:

答案 0 :(得分:1)

将此依赖项添加到您的pom.xml

<div class="container">
    <div class="row">  
        <div class="block col-sm-4">
            Column 1 Elements
        </div>
        <div class="block col-sm-4">
            Column 2 Elements
        </div>
        <div class="block col-sm-4">
            Column 3 Elements
        </div>          
    </div> 
</div>