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

标签 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.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/

相关文章:

java - 防止消息处理的竞争条件

java - 如何使用 Maven 配置文件?

java - 无法自动接线。找不到 'RestTemplateBuilder' 类型的 bean

html - 保存自由格式描述以及空格和换行符

java - 使 Swagger 在生成的模型中使用原始 int 和 boolean

java - 以给定概率生成区间内的随机数

java - 使用 JSch 0.1.48 时出现 AuthFail 异常,但在 0.1.49 及更高版本中使用密码身份验证时不会出现异常

spring - NoSuchBeanDefinitionException : PlatformTransactionManager running Junit Tests in Spring Boot 2. 0.0.M6

angularjs - 无法通过 Jboss 4.2.2 部署使用 Spring Boot 构建的 Web 应用程序

java - Spring MVC 基于 Java 的配置 - 未检测到 servlet 调度程序