gpt4 book ai didi

java - 使用post方法后信息不会出现在其他页面

转载 作者:行者123 更新时间:2023-11-30 12:07:39 27 4
gpt4 key购买 nike

我正在尝试更深入地学习 thymeleaf 并面临一个问题,在 post 方法之后,我的其他页面中没有任何内容。我看过教程和文档,但似乎遗漏了什么。

所以首先我有带有 MainController 的主页:

package com.gallery.galleryproject.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class MainController {
@RequestMapping(value = "", method = RequestMethod.GET)
public String loadMainPage() {
return "main.html";
}
}

<!doctype html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Welcome</title>
</head>
<body style="background-color: #D3D3D3">
<nav>
<div>
<h2 style="text-align: center">Welcome to gallery</h2>
</div>
</nav>
<section style="padding-top: 20px">
<div style="text-align: center;">
<p>View gallery: <a href="/gallery" style="text-decoration: none">Visit</a></p>
<p>Add new photo to gallery: <a href="/photo" style="text-decoration: none">Visit</a></p>
</div>
</section>
</body>
</html>

然后我有一个画廊 Controller 和画廊页面(在这个页面中我想显示从照片页面发送的信息)。 GalleryController + 页面

package com.gallery.galleryproject.controller;

import com.gallery.galleryproject.model.Photo;
import com.gallery.galleryproject.service.GalleryService;
import com.gallery.galleryproject.service.PhotoService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class GalleryController {

private GalleryService galleryService;

public GalleryController(GalleryService galleryService) {
this.galleryService = galleryService;
}


@RequestMapping(value = "/gallery", method = RequestMethod.GET)
public String listOfObjects(Model model) {
Photo photo = galleryService.getAllPhotos();
model.addAttribute("photo", photo);
return "gallery.html";
}
}

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Gallery</title>
</head>
<body>
<nav>
<div>
<h2 style="text-align: center">Take a look..</h2>
<!-- TODO: implement here with thymeleaf for loop printing all photo objects received from backend -->
</div>
</nav>
<section>
<div class="container">
<div class="display-galery" style="padding-left: 30px">
<p>Id of the photo: <i th:text="${photo.id}"></i></p>
<p>Name of the photo: <i th:text="${photo.name}"></i></p>
<p>Tag of the photo: <i th:text="${photo.tag}"></i></p>
<p>Quality of the photo: <i th:text="${photo.quality}"></i></p></div>
</div>
</section>
</body>
</html>

这是我的照片 Controller + html 页面,我在其中填写表格。

package com.gallery.galleryproject.controller;

import com.gallery.galleryproject.model.Photo;
import com.gallery.galleryproject.service.PhotoService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class PhotoController {

private PhotoService photoService;

public PhotoController(PhotoService photoService){
this.photoService = photoService;
}

@RequestMapping(value = "/photo", method = RequestMethod.GET)
public String displayPhoto(Model model) {
Photo photo = new PhotoService().displayPhotos();
model.addAttribute("photoC", photo);
return "photo";
}
}

<!doctype html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Welcome</title>
</head>
<body >
<nav>
<div>
<h2 style="text-align: center">Fill all the fields</h2>
</div>
</nav>
<section>
<form action="#" th:action="@{/gallery}" th:object="${photoC}" method="POST">
<p>Enter ID: </p> <input type="number" th:field="*{id}"><br>
<p>Enter photo name: </p> <input type="text" th:field="*{name}"><br>
<p>Enter photo tag: </p> <input type="text" th:field="*{tag}"><br>
<p>Enter photo quallity:</p> <input type="number" th:field="*{quality}"><br>
<input type="file">
<input type="submit" value="Submit"> <input type="reset" value="Cancel">
</form>
</section>
</body>
</html>

我还有服务:GalleryService,现在是空的(现在我有点迷路了,所以它是空的)和 PhotoService,我使用 getter 获取信息

package com.gallery.galleryproject.service;

import com.gallery.galleryproject.controller.GalleryController;
import com.gallery.galleryproject.model.Photo;
import org.springframework.stereotype.Service;

@Service
public class GalleryService {

public Photo getAllPhotos() {
Photo photo = new Photo();

return photo;
}
}

package com.gallery.galleryproject.service;

import com.gallery.galleryproject.model.Photo;
import org.springframework.stereotype.Service;

@Service
public class PhotoService {

public Photo displayPhotos() {
Photo photos = new Photo();
photos.getId();
photos.getName();
photos.getTag();
photos.getQuality();
return photos;
}
}

最佳答案

您的 Gallery Controller 中没有 ant 相关的 POST 方法。您只有 /gallery 端点的 GET 映射 尝试向您的Gallery Controller 添加如下内容。

@RequestMapping(value = "/gallery", method = RequestMethod.POST)
public String savePhoto(Photo photo) {
// Your service method to save photo here
return "xxx.html";
}

关于java - 使用post方法后信息不会出现在其他页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54745997/

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