内部资源视图解析器在Spring Boot中不起作用。

huangapple go评论116阅读模式
英文:

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>

文件夹结构

内部资源视图解析器在Spring Boot中不起作用。

英文:

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(&quot;/WEB-INF/view/&quot;);
		viewResolver.setSuffix(&quot;.jsp&quot;);
		
		return viewResolver;
	}

> Controller file

@RestController
@RequestMapping(&quot;/book&quot;)
public class BookController {

	@GetMapping(&quot;/login&quot;)
	public String getLoggedIn() {
		
		return &quot;fancy-login&quot;;
	}
}

> pom.xml file

    &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
	xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
	&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
	&lt;parent&gt;
		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
		&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
		&lt;version&gt;2.3.1.RELEASE&lt;/version&gt;
		&lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
	&lt;/parent&gt;
	&lt;groupId&gt;com.demo&lt;/groupId&gt;
	&lt;artifactId&gt;book&lt;/artifactId&gt;
	&lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
	&lt;name&gt;book&lt;/name&gt;
	&lt;description&gt;Demo project for Spring Boot&lt;/description&gt;
	&lt;packaging&gt;war&lt;/packaging&gt;
	
	&lt;properties&gt;
		&lt;java.version&gt;1.8&lt;/java.version&gt;
	&lt;/properties&gt;

	&lt;dependencies&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-starter-security&lt;/artifactId&gt;
		&lt;/dependency&gt;
		
		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
    		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    		&lt;artifactId&gt;spring-boot-starter-jdbc&lt;/artifactId&gt;
		&lt;/dependency&gt;

		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-devtools&lt;/artifactId&gt;
			&lt;scope&gt;runtime&lt;/scope&gt;
			&lt;optional&gt;true&lt;/optional&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;mysql&lt;/groupId&gt;
			&lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
			&lt;scope&gt;runtime&lt;/scope&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;com.mchange&lt;/groupId&gt;
			&lt;artifactId&gt;c3p0&lt;/artifactId&gt;
			&lt;version&gt;0.9.5.5&lt;/version&gt;
		&lt;/dependency&gt;
		
		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
			&lt;scope&gt;test&lt;/scope&gt;
			&lt;exclusions&gt;
				&lt;exclusion&gt;
					&lt;groupId&gt;org.junit.vintage&lt;/groupId&gt;
					&lt;artifactId&gt;junit-vintage-engine&lt;/artifactId&gt;
				&lt;/exclusion&gt;
			&lt;/exclusions&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
			&lt;artifactId&gt;spring-security-test&lt;/artifactId&gt;
			&lt;scope&gt;test&lt;/scope&gt;
		&lt;/dependency&gt;
		
	&lt;/dependencies&gt;

	&lt;build&gt;
		&lt;plugins&gt;
			&lt;plugin&gt;
				&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
				&lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
			&lt;/plugin&gt;
		&lt;/plugins&gt;
	&lt;/build&gt;

&lt;/project&gt;

> folder structure

https://i.stack.imgur.com/D0Wv0.png

答案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(&quot;/book&quot;)
public class BookController {

    @GetMapping(&quot;/login&quot;)
    public String getLoggedIn() {
        
        return &quot;fancy-login&quot;;
    }
}

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 :

&lt;packaging&gt;war&lt;/packaging&gt;

Also if you are using embedded tomcat add the following under dependency secton of your pom :

  &lt;dependency&gt;
     &lt;groupId&gt;org.apache.tomcat.embed&lt;/groupId&gt;
     &lt;artifactId&gt;tomcat-embed-jasper&lt;/artifactId&gt;
     &lt;scope&gt;provided&lt;/scope&gt;
  &lt;/dependency&gt;

And also add JSP Servlet and JSTL support if used in your JSP page then only

huangapple
  • 本文由 发表于 2020年7月23日 13:40:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/63047573.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定