gpt4 book ai didi

java - 无法使用 Spring Boot 呈现 JSP

转载 作者:行者123 更新时间:2023-11-28 22:06:07 25 4
gpt4 key购买 nike

我是 spring boot 的新手,我想从一个简单的例子开始,但似乎没有任何效果这是我的 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 View :

<%@ 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>

我添加了所需的依赖项:

 <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中添加了前缀和后缀

当我运行应用程序时,只有 hello world 显示在没有名称变量的 View 上,在这里搜索后我将 View 目录从 src/main/webapp/view 更改为 src/main/resource/templates 但 spring boot 不能'找不到风景了

最佳答案

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

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

项目的目录结构

Directory Structure以下是我为运行 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

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 的路径

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

关于java - 无法使用 Spring Boot 呈现 JSP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45533908/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com