3

I use spring boot to do a simple web page. Here are the codes:

src/main/java/Application.java

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

src/mian/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/main/java/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/main/webapp/WEB-INF/views/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>

I start the project from the Application.java, and put http://localhost:8080/test in the browser, but the browser just show the source code of test.jsp. It seems spring boot treat jsp as txt file... How to fix it?

Yichang Xu
  • 31
  • 3
  • Have you included the javax.servlet jstl dependency in your pom? – David Lavender Jun 04 '15 at 11:12
  • @MrSpoon No, I didn't. Spring boot already included it? If I change test.jsp to test.html, then html files display correctly. But jsp failed... – Yichang Xu Jun 04 '15 at 13:37
  • possible duplicate of [JSP file not rendering in Spring Boot web application](http://stackoverflow.com/questions/20602010/jsp-file-not-rendering-in-spring-boot-web-application) – jst Jun 04 '15 at 19:42
  • @jst Thank you for your link. It's the same. I have fixed it by add jasper and jstl. – Yichang Xu Jun 05 '15 at 04:22

1 Answers1

1

add this dependency to your pom.xml

    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <scope>provided</scope>
    </dependency>
ali akbar azizkhani
  • 2,213
  • 5
  • 31
  • 48
  • Please also add an explanation about how or why this helps. Just dumping code might be confusing to some future readers that have a similar problem – David Medenjak Apr 09 '16 at 16:47