英文:
Internal resource view resolver not working in spring boot
问题
我已经在src/main/webapp/WEB-INF/view/fancy-login.jsp中创建了我的“fancy-login.jsp”,但是当我访问localhost:8080/book/login时,我的JSP页面没有显示出来,只显示了页面名称fancy-login,这意味着字符串按原样打印出来,而不是显示我的JSP页面。
请帮助我解决这个问题。
Bean
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/view/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
Controller文件
@RestController
@RequestMapping("/book")
public class BookController {
@GetMapping("/login")
public String getLoggedIn() {
return "fancy-login";
}
}
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.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.demo</groupId>
<artifactId>book</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>book</name>
<description>Demo project for Spring Boot</description>
<packaging>war</packaging>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!-- 其他依赖项... -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
文件夹结构
英文:
I have created my "fancy-login.jsp" in src/main/webapp/WEB-INF/view/fancy-login.jsp but my jsp page is not shown when I access localhost:8080/book/login its shown just the page name fancy-login means the string is printed as it is rather than my jsp page.
Please help me resolve it.
> Bean
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/view/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
> Controller file
@RestController
@RequestMapping("/book")
public class BookController {
@GetMapping("/login")
public String getLoggedIn() {
return "fancy-login";
}
}
> pom.xml file
<?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.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.demo</groupId>
<artifactId>book</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>book</name>
<description>Demo project for Spring Boot</description>
<packaging>war</packaging>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
> folder structure
答案1
得分: 3
将@RestController
替换为@Controller
,因为REST控制器不会解析视图,它只会返回数据。
@Controller
@RequestMapping("/book")
public class BookController {
@GetMapping("/login")
public String getLoggedIn() {
return "fancy-login";
}
}
由于您正在使用Spring Boot的JSP,请确保使用war
打包,而不是默认的jar
。JSP与jar打包不兼容。
您还可以通过在application.properties
中提供以下值来配置JSP视图解析器:
# 视图解析器
spring.mvc.view.prefix=/WEB-INF/view/
spring.mvc.view.suffix=.jsp
如果您使用的是Maven,那么在您的IDE中找到pom.xml
,然后在版本标签下添加以下内容:
<packaging>war</packaging>
另外,如果您正在使用嵌入式Tomcat,请在pom文件的依赖部分添加以下内容:
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
如果您在JSP页面中使用了JSP Servlet和JSTL支持,则还需要添加相应的依赖。
英文:
Replace @RestController
with @Controller
as a rest controller will not resolve a view , it will only return the data.
@Controller
@RequestMapping("/book")
public class BookController {
@GetMapping("/login")
public String getLoggedIn() {
return "fancy-login";
}
}
Since you are using jsp with springboot make sure that you are using a war
packaging instead of the default jar
. Jsp does not work well with the jar as packaging.
You could also configure the jsp view resolver by just providing the values in application.properties
like :
#ViwResolver
spring.mvc.view.prefix=/WEB-INF/view/
spring.mvc.view.suffix=.jsp
In your case if you are using maven then find pom.xml
in your ide and then add the following under version tag :
<packaging>war</packaging>
Also if you are using embedded tomcat add the following under dependency secton of your pom :
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
And also add JSP Servlet and JSTL support if used in your JSP page then only
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论