Java 使用post方法后,信息不会显示在其他页面中
我正试图更深入地学习thymeleaf,并且面临一个问题,即在使用post方法之后,我的另一页中没有任何内容出现。我看过教程和文档,但似乎我错过了一些东西 首先,我有MainController的主页:Java 使用post方法后,信息不会显示在其他页面中,java,spring-boot,spring-mvc,post,thymeleaf,Java,Spring Boot,Spring Mvc,Post,Thymeleaf,我正试图更深入地学习thymeleaf,并且面临一个问题,即在使用post方法之后,我的另一页中没有任何内容出现。我看过教程和文档,但似乎我错过了一些东西 首先,我有MainController的主页: package com.gallery.galleryproject.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.Req
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>
您的库控制器中没有与蚂蚁相关的POST方法。对于
/gallery
端点,您只有一个GET映射。请尝试将类似以下内容添加到gallery控制器中
@RequestMapping(value = "/gallery", method = RequestMethod.POST)
public String savePhoto(Photo photo) {
// Your service method to save photo here
return "xxx.html";
}
啊,我觉得每件事都有点不一样。我将尝试使用POST方法再添加一个RequestMapping。解决了一个问题
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>
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;
}
}
@RequestMapping(value = "/gallery", method = RequestMethod.POST)
public String savePhoto(Photo photo) {
// Your service method to save photo here
return "xxx.html";
}