springboot把jsp页面放在resources下,为什么访问不到

 我来答
Mexico卡卡
2017-11-19 · TA获得超过1万个赞
知道大有可为答主
回答量:1.3万
采纳率:98%
帮助的人:1866万
展开全部
举例说明:
Project Structure
└─main
├─java
│ └─com
│ └─henry
│ └─jsp
│ SampleWebJspController.java

└─resources
│ application.properties

└─META-INF
└─resources
└─WEB-INF
└─jsp
welcome.jsp

pom file
<拆兆dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>8.0.28</version>
</dependency>

java code
@Controller
@EnableAutoConfiguration
public class SampleWebJspController extends SpringBootServletInitializer {

@Value("${application.message:Hello World}")
private String message = "Hello World";

@RequestMapping("/")
public String welcome(Map<String, Object> model) {
model.put("time", new Date());
model.put("message", this.message);
return "welcome";
}

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

welcome.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
${message}
<div>${time}</div>
</body>
</html>

Start the project with main method and access localhost:8080 you can see the page.
Notice
At first I make a directory in src/main like webapp/WEB-INF/jsp and put all jsp in it. When I try to access root path with main method I got 404 page. I found there is no jsp file in jar. I changed the pom file like following
<packaging>war</packaging>
<dependencies>
<!-- dependency here -->
</dependencies>

<build>
<plugins>旅激租
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>铅颤${spring.boot.version}</version>
</plugin>
</plugins>
</build>
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式