无法使用Spring Boot呈现JSP [英] Unable to render the JSP using Spring Boot

查看:92
本文介绍了无法使用Spring Boot呈现JSP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是春季靴子的新手,我想以一个简单的例子开始,但是似乎没有任何效果 这是我的控制器:

i 'm new in spring boot and i wanted to start with a simple example however nothing seemed to work this my controller:

Package controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class testController {
RequestMapping("view/aboutus")
public String greeting(Model model) {
 model.addAttribute("name", "azertyyyy");
  return "aboutus";  }}

这是jsp视图:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
   <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<!DOCTYPE>
<html> <body>  ${name}


 <p>hello world</p> </body></html>

我添加了所需的依赖项:

i added the needed dependencies:

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


 <dependency>
    <groupId>org.apache.tomcat.embed</groupId>
   <artifactId>tomcat-embed-jasper</artifactId>
   <scope>provided</scope>

我在application.properties中添加了前缀和后缀

and i added the prefix and suffix in application.properties

当我运行该应用程序时,视图上仅显示hello world,而没有name变量,在这里搜索后,我将视图目录从src/main/webapp/view更改为src/main/resource/templates,但是spring boot无法进行找不到视图了

when i run the application only hello world is shown on the view without the name variable, after searching here i changed the view directory from src/main/webapp/view to src/main/resource/templates but spring boot couldn't find the view anymore

推荐答案

我不确定您是否完全了解我们要以war程序包运行应用程序时必须进行的更改.

I am not sure if you are fully aware of the changes we have to make when we want to run the application as war package.

  1. 将包装从jar更改为war,并需要依赖项.
  2. 更改主应用程序类以扩展SpringBootServletInitializer并实现configure()
  3. 在应用程序属性中定义jsp页面路径
  4. 将JSP放置在正确的路径中(webapp/WEB-INF/jsp检查屏幕截图)

项目的目录结构

以下是我运行JSP

Following is the code changes I have to done to run the JSP

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.test</groupId>
    <artifactId>Test_Exception_Framework</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>Test_Exception_Framework</name>
    <description>Project to test ExceptionFramework</description>


    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <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>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>


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

        <!-- Need this to compile JSP -->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.eclipse.jdt.core.compiler</groupId>
            <artifactId>ecj</artifactId>
            <version>4.6.1</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>

            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.7.5.201505241946</version>
                <executions>
                    <execution>
                        <id>default-prepare-agent</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>default-report</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

主类

package com.exception;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;

@SpringBootApplication
public class SpringBootWebApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {

        SpringApplication.run(SpringBootWebApplication.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(SpringBootWebApplication.class);
    }


}

Rest Controller-重定向到JSP

Rest Controller - which redirects to JSP

package com.exception.controller;

import java.util.Map;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(method = RequestMethod.GET)
public class TestExceptionController {

    @RequestMapping("/")
    public String welcome(Map<String, Object> model) {

        System.out.println("in controller");

        model.put("message", "hello spring boot");

        return "welcome";
    }

}

application.properties-定义JSP的路径

application.properties - define the path for JSPs

server.port=8085
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp

这篇关于无法使用Spring Boot呈现JSP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆