我正在尝试制作一个网站来托管我的一些个人摄影作品。我已经使用 ubuntu 购买了 VPS 的访问权限。我的网站使用带有 MySQL 数据库的 Spring Boot 并使用 JSP。我正在尝试使用 tomcat 服务器让我的网站正常工作。
当我尝试通过 VPSip:8080/service 访问我的网站时,出现此错误:
HTTP 状态 404 – 未找到 类型状态报告 消息 请求的资源[/service/]不可用 描述 源服务器未找到目标资源的当前表示或不愿意透露该表示存在。
当我访问VPSip:8080/时,Tomcat 工作正常。
我尝试更改我的 MainController 类以具有 /service 的映射,以前它只是“/”,但这没有帮助。
主控制器代码:
package com.example.photographygallerysite.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class MainController {
@RequestMapping("/service")
public String start() {
return "start";
}
}
开始引用start.jsp:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Photography by Oliver Johnson</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
font-family: 'Arial', sans-serif;
background-color: rgb(255, 253, 247);
margin: 0;
padding: 0;
text-align: center;
}
h1 {
color: #333;
margin-top: 50px;
font-family: 'Corben', Georgia, Times, serif;
color: rgb(38, 24, 24);
}
a {
display: inline-block;
padding: 10px 20px;
margin-top: 20px;
background-color: rgb(38, 24, 24);
color: #fff;
text-decoration: none;
border-radius: 2px;
transition: background-color 0.3s;
font-family: 'Nobile', Helvetica, Arial, sans-serif;
}
a:hover {
background-color: rgba(66, 66, 66, 0.55);
}
.slideshow-container {
max-width: 750px;
margin: auto;
position: relative;
}
.mySlides {
display: none;
width: 100%;
animation: fade 7s linear; /* Adjust the duration as needed */
}
@keyframes fade {
0%, 5% {
opacity: 0;
}
5%, 95% {
opacity: 1;
}
99%, 100% {
opacity: 0;
}
}
.w3-badge {
height: 13px;
width: 13px;
margin: 0 2px;
background-color: #bbb;
border-radius: 50%;
display: inline-block;
transition: background-color 0.6s;
}
.w3-badge-active {
background-color: #4285f4;
}
/* Content separator styling */
.separator {
border-top: 2px solid #ccc;
margin: 20px auto;
width: 57%;
opacity: 50%;
}
</style>
</head>
<body>
<h1>Photography by Oliver Johnson</h1>
<div class="separator"></div>
<div class="slideshow-container w3-content w3-section">
<div class="mySlides w3-animate-fading">
<img src="http://ip:8080/images/firstImg.jpg" style="width:100%">
</div>
<div class="mySlides w3-animate-fading">
<img src="http://ip:8080/images/DSC_0393.JPG" style="width:100%">
</div>
<div class="mySlides w3-animate-fading">
<img src="http://ip:8080/images/DSC_0348.jpg" style="width:100%">
</div>
<div class="mySlides w3-animate-fading">
<img src="http://ip:8080/images/DSC_0383.jpg" style="width:100%">
</div>
</div>
<div class="separator"></div>
<script>
var myIndex = 0;
carousel();
function carousel() {
var slides = document.getElementsByClassName("mySlides");
for (var i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
myIndex++;
if (myIndex > slides.length) {
myIndex = 1;
}
slides[myIndex - 1].style.display = "block";
setTimeout(carousel, 7000); // Adjust the timeout to match the animation duration
}
</script>
<a href="http://ip:8080/displayAll">See all pictures</a>
</body>
</html>
摄影画廊网站应用程序代码
package com.example.photographygallerysite;
import com.example.photographygallerysite.Model.Image;
import com.example.photographygallerysite.repo.ImageRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
@SpringBootApplication
public class PhotographyGallerySiteApplication extends SpringBootServletInitializer implements CommandLineRunner {
@Autowired
private ImageRepository repo;
public static void main(String[] args) {
SpringApplication.run(PhotographyGallerySiteApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
Image firstImg = new Image();
firstImg.setFilename("firstImg.jpg");
firstImg.setLocation("Hartlepool");
firstImg.setCamera("Old Nikon");
repo.save(firstImg);
Image DSC_0393 = new Image();
DSC_0393.setFilename("DSC_0393.JPG");
DSC_0393.setLocation("Hartlepool");
DSC_0393.setCamera("Old Nikon");
repo.save(DSC_0393);
Image DSC_0323 = new Image();
DSC_0323.setFilename("DSC_0323.jpg");
DSC_0323.setLocation("Hartlepool");
DSC_0323.setCamera("Old Nikon");
repo.save(DSC_0323);
Image DSC_0332 = new Image();
DSC_0332.setFilename("DSC_0332.jpg");
DSC_0332.setLocation("Hartlepool");
DSC_0332.setCamera("Old Nikon");
repo.save(DSC_0332);
Image DSC_0348 = new Image();
DSC_0348.setFilename("DSC_0348.jpg");
DSC_0348.setLocation("Hartlepool");
DSC_0348.setCamera("Old Nikon");
repo.save(DSC_0348);
Image DSC_0368 = new Image();
DSC_0368.setFilename("DSC_0368.jpg");
DSC_0368.setLocation("Hartlepool");
DSC_0368.setCamera("Old Nikon");
repo.save(DSC_0368);
Image DSC_0383 = new Image();
DSC_0383.setFilename("DSC_0383.jpg");
DSC_0383.setLocation("Hartlepool");
DSC_0383.setCamera("Old Nikon");
repo.save(DSC_0383);
Image DSC_0389 = new Image();
DSC_0389.setFilename("DSC_0389.jpg");
DSC_0389.setLocation("Hartlepool");
DSC_0389.setCamera("Old Nikon");
repo.save(DSC_0389);
}
}
尝试对 Spring boot REST API 使用正确的指南, 现在我们很少将 JSP 与 Spring Boot 一起使用。
尝试使用一些配置 并尝试发布您的
application.properties
spring.mvc.view.prefix=/jsp/
spring.mvc.view.suffix=.jsp
在
tomcat-embed-jasper
中添加
pom.xml
<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-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>