gpt4 book ai didi

jsp - Spring Boot和JSP无法渲染

转载 作者:行者123 更新时间:2023-12-03 14:29:31 26 4
gpt4 key购买 nike

我已经为此工作了好几天,试图使其工作。尝试将JSP与Spring Boot结合使用。我一直在github上关注该示例。 Web上的许多示例都有一个web.xml文件,但是在该论坛上被多次引用的github上的示例甚至都没有web.xml。另外,在创建Spring Boot项目时,STS不会创建带有子目录“WEB-INF”的“webapp”目录。我必须在Windows资源管理器中手动创建它们,然后在STS中刷新项目资源管理器以使其出现。

无论如何,当我在地址字段中键入http://localhost:8080时,我尽力地按照示例进行操作,但仍然无法在浏览器中呈现index.jsp文件。

任何指导将不胜感激。

到目前为止,这就是我所拥有的。目录结构:

enter image description here

application.properties:

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/jeddatae_editor
spring.datasource.username=webapp
spring.datasource.password=secret

index.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%
String name = request.getParameter("name");
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JED Login</title>
</head>
<body>
<form action="/index" method="post">
<p>Insert ID here: <input type="text" id="id" name="id"></p>
<input type=submit>
</form>
<%
if(!name.equals(""))
{
%>
<div class="welcome">
<p>Welcome <%=name %> </p><p>If you are not <%=name %>, then try a different id.</p>
</div>
<%
}
%>

hello.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h2>Hello</h2>
</body>
</html>

GreetingController.java
package com.anypackage.jedcontext;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.view.RedirectView;
import com.tacticalenterprisesltd.Database;

@Controller
public class GreetingController {

@RequestMapping("/")
public RedirectView index(@RequestParam(value="id", required=false) String id, RedirectAttributes attribs) {
if (id != null)
{
Database db = new Database("jeddatae_editor");
String[][] result = db.executeSelect("SELECT CONCAT(first_name, ' ', last_name) FROM employees WHERE id=" + id + ";");
if(result.length != 0){
attribs.addAttribute("name", result[0][0]);
}
}

return new RedirectView("index");
}

@RequestMapping("/hello")
public String hello() {

return "hello";
}
}

pom.xml:
<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.anypackage</groupId>
<artifactId>jedtest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<name>jedtest</name>
<description>An application context test</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>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Tomcat embedded container-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- Need this to compile JSP -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>

<!-- JSTL -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>


</project>

最佳答案

这取决于您如何将工件打包为war/jar文件。
War File Package遵循以下规则。

  • 您的主类应扩展 SpringBootServletInitializer
  • 您必须创建一个自定义error.jsp页面不会覆盖默认 View 以进行错误处理,而应改用自定义错误页面。
  • 您可以运行这些普通的Maven目标(例如 mvn全新安装软件包-e )并将其部署在tomcat Web服务器中。

  • JAR文件包
  • 因为您的嵌入式tomcat在tomcat中使用了硬编码文件模式,所以它不起作用。
  • 严格遵循此sample并相应地重构代码。
    请引用此JSP Limitations with Spring Boot
  • 关于jsp - Spring Boot和JSP无法渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45068734/

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