使用post方法信息后不会出现在其他页面中 [英] After using post method information does not appear in other page
问题描述
我正在尝试更深入地学习百里香,面对一个问题,在一个帖子方法后,我的另一页中没有任何内容。我看过教程和文档,但似乎我错过了什么。
首先,我有MainController的主页:
I'm trying to learn thymeleaf deeper and facing a problem, that after a post method nothing appears in my other page. I've watched tutorials and docs, but seems I`m missing something.
So first of all I have main page with 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>
然后我有一个画廊控制器和画廊页面(在这个页面我想要显示信息哪个是从照片页面发送的)。 GalleryController + page:
Then I have a gallery controller and gallery page(in this page i want display information which was sent from photo page). GalleryController + page:
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>
</div>
</nav>
<section>
<div class="container">
<div class="display-galery" style="padding-left: 30px">
<p>Id of the photo: </p>
<p>Name of the photo: </p>
<p>Tag of the photo: </p>
<p>Quality of the photo: </p></div>
</div>
</section>
</body>
</html>
这是我的照片控制器+ html页面,我填写表格。
Here's my photo controller + html page where i fill up the form.
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来获取信息
Also I have services: GalleryService which is empty right now(now i'm kinda lost so that's the reason why it's empty) and PhotoService where I use getters to get info
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;
}
}
我的尝试:
堆栈,视频,春季教程,但可能我遗漏了一些东西。
What I have tried:
Stack, videos, spring tutorials, but probably I'm missing something.
推荐答案
{photoC}method = POST>
< p>输入ID:< / p>< input type =numberth:field =* {id}>< br>
< p>输入照片名称:< / p>< input type =textth:field =* {name}>< br>
< p>输入照片标记:< / p>< input type =textth:field =* {tag}>< br>
< p>输入照片质量:< / p>< input type =number th:field =* {quality}>< br>
< input type =file>
< input type =submitvalue =Submit> < input type =resetvalue =Cancel>
< / form>
< / section>
< / body>
< / html> ;
{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来获取信息
Also I have services: GalleryService which is empty right now(now i'm kinda lost so that's the reason why it's empty) and PhotoService where I use getters to get info
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;
}
}
我的尝试:
堆栈,视频,春季教程,但可能我遗漏了一些东西。
What I have tried:
Stack, videos, spring tutorials, but probably I'm missing something.
这篇关于使用post方法信息后不会出现在其他页面中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!