使用Spring Boot MVC无法查看JSP文件

时间:2019-01-31 10:40:35

标签: spring-boot

我使用jsp创建了spring mvc模式,但是当我调用jsp包含的路由时却不显示

我的项目jsp文件路径为

 src/main/resources/META-INF/webapp/WEB-INF.jsp/jsp/Helloworld.jsp

我的索引控制器

@Controller
public class IndexController {
@GetMapping("/helloworld")
String getView(Model model) {
model.addAttribute("msg", "Hello there");
 System.out.printf("test hello world ..................");
 return "Helloworld";
}
}

我的Helloworld.jsp

<!DOCTYPE html>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html lang="en">

<body>
    <h2>${msg}</h2>
</body>

</html>

application.properties

spring.mvc.view.prefix:/WEB-INF/jsp/
spring.mvc.view.suffix:.jsp
welcome.message:helloworld

pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.15.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.3.0</version>
</dependency>
<dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
</dependency>
<dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
</dependency>

当我呼叫路线

localhost:8000/hellworld 


然后system.out.println消息出现在终端中,但在Web上却没有任何显示。
你能帮我吗?

1 个答案:

答案 0 :(得分:0)

添加此依赖项:

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>

然后通过将以下内容添加到server.port = 8000application.properties

中,以确保您是正确的端口

最后运行http://localhost:8000/helloworld而不是localhost:8000/hellworld

您的jsp页面应如下所示:

<!DOCTYPE html> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%> 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Hello ${msg}!</title>

</head>
<body>
   <h2 >Hello ${msg}!</h2>
</body>
</html>