Spring boot boot jsp as txt?
I am using spring boot to create 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 / home / 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 am running a project from Application.java, and put http: // localhost: 8080 / test in the browser, but the browser just shows the test.jsp source code. It seems spring boot to process jsp as txt file ... How to fix this?
+3
source to share
1 answer
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>
+1
source to share