[英]How to read jsp file with spring boot?
I'm trying to read my jsp file using spring-boot so i start the project and when i check my localhost it actually return the actual string "index" but not reading it as index.jsp (as jsp file) 我正在尝试使用spring-boot读取我的jsp文件,因此我启动了项目,当我检查本地主机时,它实际上返回了实际的字符串“ index”,但没有将其读取为index.jsp(作为jsp文件)
I've created Controller Class "HomeController" and implement a method named homePage which will have @RequestMapping then it will return "index" 我创建了控制器类“ HomeController”,并实现了一个名为homePage的方法,该方法将具有@RequestMapping,然后将返回“ index”
HomeController 家庭控制器
@RestController
public class HomeController {
@RequestMapping("/showHome")
public String homePage(){
return "index";
}
}
index.jsp file index.jsp文件
<!DOCTYPE html>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<html lang="en">
<body>
<div>
<div>
<h1>Spring Boot JSP Example</h1>
<h2>Hello</h2>
</div>
</div>
</body>
</html>
pom.xml pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</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>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties application.properties
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
I've got "index" string in my browser was excpecting the jsp file to load NB: when i replace @ResController with @Controller i get Whitelabel Error Page. 我的浏览器中出现了“索引”字符串,该jsp文件希望加载NB:当我将@ResController替换为@Controller时,我得到了Whitelabel错误页面。
you should use @Controller if you want to return jsp file. 如果要返回jsp文件,则应使用@Controller。 @RestController returns a string @RestController返回一个字符串
@Controller
public class HomeController {
@RequestMapping("/showHome")
public String homePage(){
return "index";
}
}
for configurations create WebConfig.java file and write in these: 对于配置,请创建WebConfig.java文件并在其中写入以下内容:
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {
"com.example.demo"
})
public class WebConfig implements WebMvcConfigurer {
@Bean
public InternalResourceViewResolver getInternalResourceViewResolver(){
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
}
Advise: see this link for more: Hello Horld in Spring Boot 建议:有关更多信息,请参见此链接: Spring Boot中的Hello Horld
Had to change my pom.xml file dependecies where i import tomcat-embed and javax.servelet to 必须更改我将tomcat-embed和javax.servelet导入到的pom.xml文件依赖项
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>8.5.20</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
Remove this two line from application.properties file 从application.properties文件中删除这两行
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
and Delete templates folder from resource package. 和从资源包中删除模板文件夹。
Then add this class in your com.example.demo package. 然后将此类添加到com.example.demo包中。
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages= {"com.example"})
public class WebConfig implements WebMvcConfigurer {
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.jsp().prefix("/WEB-INF/views/").suffix(".jsp");
}
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
}
In controller use: 在控制器中使用:
@Controller not @RestController
Like: 喜欢:
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class TestController {
@RequestMapping(value = "/showHome", method = RequestMethod.GET)
public String homePage() {
return "index";
}
}
Sample Blog site application (git source code ) with Spring Boot and Hibernate you may get help from here. 带有Spring Boot和Hibernate的示例Blog站点应用程序(git源代码),您可以从这里获得帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.