【问题标题】:Recover form in JAVA-Spring (with thymeleaf) from html page从 html 页面恢复 JAVA-Spring 中的表单(使用 thymeleaf)
【发布时间】:2018-09-14 07:35:33
【问题描述】:

我是开发工具的新手,我正在尝试将 spring 与 thymeleaf 一起使用来创建一个在网站上注册的表单。我目前无法将表单中的信息恢复到仅显示用户列表的另一个页面。 我在 Eclipse 中使用 model/service/dao/controller java 文件创建了一个经典的 Spring boot 安全性。

我的模型 java 文件 --> Register.java

@Entity
public class Register {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;
    private String pseudo;
    private String lastname;
    private String firstname;
    private String sex;
    private String country;
    private String phone;
    private String spokenlanguages;
    private String email;
    private String password;
    private String job;
    private boolean premium;

    public void updateRegister(String pseudo, String lastname, String firstname, String sex, String country, String phone,
            String spokenlanguages, String email, String password, String job, boolean premium) {
        this.pseudo = pseudo;
        this.lastname = lastname;
        this.firstname = firstname;
        this.sex = sex;
        this.country = country;
        this.phone = phone;
        this.spokenlanguages = spokenlanguages;
        this.email = email;
        this.password = password;
        this.job = job;
        this.premium = premium;
// with all the getters & setters
    }

My Service java file --> RegisterService.java

    public interface RegisterService {
    public List<Register> list();
    public Register getById(long id);
}

我的存储库 Java 文件 --> RegisterRepository.java

    @Repository
public interface RegisterRepository extends PagingAndSortingRepository<Register, Long> {}

我的控制器 Java 文件 --> RegisterController.java

    @Controller
public class RegisterController {

    @Autowired
    RegisterRepository registerRepository;

    @RequestMapping(value="/register", method=RequestMethod.GET)
    public String form (Model model) {
        return "register/form";
    }

    @PostMapping("/register/add")
    public String registerAdd(
            @Valid Register register,
            BindingResult result,
            Model model) {
        if (result.hasErrors()) {
            return "redirect:/error";
        }
        else {

            Register r = new Register();
            r.updateRegister(register.getPseudo(), 
            register.getLastname(),
            register.getFirstname(), 
            register.getSex(),
            register.getCountry(),
            register.getPhone(),
            register.getSpokenlanguages(),
            register.getEmail(),
            register.getPassword(),
            register.getJob(),
            register.isPremium());
            System.out.println(r);

            registerRepository.save(r);

            model.addAttribute("registerlist", registerRepository.findAll());
            return "register/valid";
        }
    }

    @RequestMapping(value="/register/valid", method=RequestMethod.GET)
    public String list (Model model) {
        return "redirect:/home";
    }

}

我的 HTML 表单 --> form.html

    <!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org/">
<head>
<title>Formulaire d'inscription</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet"
    href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
    integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"
    crossorigin="anonymous">
<script
    src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"
    integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
    crossorigin="anonymous"></script>
</head>
<body>
    <h1 class="register">Formulaire d'inscription</h1>
    <form action="#" method="post" th:object="${register}" th:action="@{/register/add}">
        <div class="form-group">
            <label for="pseudo">Pseudo</label> <input type="text"
                class="form-control" id="pseudo" placeholder="Pseudo"
                th:field="*{pseudo}" required="required">
        </div>

        <div class="form-group">
            <label for="lastname">Nom</label> <input type="text"
                class="form-control" id="lastname" placeholder="Nom"
                th:field="*{lastname}" required="required">
        </div>

        <div class="form-group">
            <label for="firstname">Pr&eacute;nom</label> <input type="text"
                class="form-control" id="firstname" placeholder="Prenom"
                th:field="*{firstname}" required="required">
        </div>

        <div class="form-group radio">
            <label for="sex">Sexe</label><br /> 
            <label for="male">Homme<input
                type="radio" class="form-control" id="sex" value="male"
                th:field="*{sex}" required="required">
            </label> <label for="female">Femme<input type="radio"
                class="form-control" th:field="*{sex}" id="sex" value="female">
            </label> <label for="other">Autre<input type="radio"
                class="form-control" th:field="*{sex}" id="sex" value="other"></label>
        </div>

        <div class="form-group">
            <label for="country">Pays</label> <br /> <select th:field="*{country}" required="required">
                <option value="France" selected="selected">France</option>
                ....other countries...

            </select>
        </div>

        <div class="form-group">
            <label for="phone">T&eacute;l&eacute;phone</label> <label
                class="optional">(Facultatif)</label> <input type="text"
                class="form-control" id="phone" placeholder="Téléphone" th:field="*{phone}">
        </div>

        <div class="form-group">
            <label for="spokenlanguages">Langues parl&eacute;es</label> <input
                type="text" class="form-control" id="spokenlanguages"
                placeholder="Langues" th:field="*{spokenlanguages}" required="required">
        </div>

        <div class="form-group">
            <label for="email">Adresse Email</label> <input type="email"
                class="form-control" id="email" aria-describedby="emailHelp"
                placeholder="Email" th:field="*{email}" required="required">
        </div>

        <div class="form-group">
            <label for="emailconf">Confirmez votre adresse Email</label> <input
                type="email" class="form-control" id="emailconf"
                aria-describedby="emailHelp" placeholder="" required="required">
        </div>


        <div class="form-group">
            <label for="password">Mot de passe</label> <input type="password"
                class="form-control" id="password" placeholder=""
                th:field="*{password}" required="required">
        </div>

        <div class="form-group">
            <label for="passwordconf">Confirmez votre mot de passe</label> <input
                type="password" class="form-control" id="passwordconf"
                placeholder="" required="required">
        </div>

        <div class="form-group">
            <label for="job">Travail</label> <br /> <select id="job"
                th:field="*{job}" required="required">
                <option value="alexandre">Alexandre</option>
                <option value="filmlover">Cin&eacute;phile</option>
                <option value="scenarist">Sc&eacute;nariste</option>
                <option value="productor">Producteur</option>
                <option value="other">Autres</option>
            </select>
        </div>

        <div class="form-group radio">
            <label for="premium">Premium</label><br />
            <label for="yes">Oui<input
                type="radio" class="form-control" id="premium"
                value="yes" th:field="*{premium}" required="required">
            </label>
            <label for="no">Non<input type="radio" class="form-control"
                th:field="*{premium}" id="premium" value="no">
            </label>
        </div>

        <input type="submit" class="btn btn-primary" id="valider" name="valider" value="Valider" />
    </form>
    <a href="../home" > <input  class="btn btn-primary" type="reset" value="Annuler" /></a>
</body>
</html>

和我的第二个 HTML --> valid.html 现在注册成功后我首先想看到的地方......但是我在这个页面中收到了这条消息 --> org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor 和我在控制器中的试用 sysout 导致除了 id 和高级内容(布尔值)之外的所有内容都为空值

    <!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org/">
<head>
<title>Formulaire d'inscription</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet"
    href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
    integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"
    crossorigin="anonymous">
<script
    src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"
    integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
    crossorigin="anonymous"></script>
</head>

<body>
    <h1 class="register">Confirmation de votre inscription</h1>
<!--        <h2 class ="register" th:text="${'Bienvenue ' + register.pseudo}"></h2> -->

    <div class="container">
        <div class="page-header" id="banner">
            <div class="row">
                <div class="col-lg-8 col-md-7 col-sm-6">
                    <h1>Utilisateurs</h1>
                    <table class="table table-striped table-hover">
                        <thead>
                            <tr>
                                <th>Id</th>
                                <th>Pseudo</th>
                                <th>Nom</th>
                                <th>Pr&eacute;nom</th>
                                <th>Sexe</th>
                                <th>Pays</th>
                                <th>T&eacute;l&eacute;phone</th>
                                <th>Langues parl&eacute;es</th>
                                <th>E-mail</th>
                                <th>Mot de passe</th>
                                <th>Travail</th>
                                <th>Premium</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr th:each="register : ${registerlist}">
                                <td th:text="${register.id}"></td>
                                <td th:text="${register.pseudo}"></td>
                                <td th:text="${register.lastname}"></td>
                                <td th:text="${register.firstname}"></td>
                                <td th:text="${register.sex}"></td>
                                <td th:text="${register.country}"></td>
                                <td th:text="${register.phone}"></td>
                                <td th:text="${register.spokenlanguages}"></td>
                                <td th:text="${register.email}"></td>
                                <td th:text="${register.password}"></td>
                                <td th:text="${register.job}"></td>
                                <td th:text="${register.premium}"></td>
                                <td th:text="${register.id}"></td>

                            </tr>
                        </tbody>
                    </table>
            </div>
    </div>
<a href="../home" > <input  class="btn btn-primary" type="reset" value="Retour" /></a>

</body>
</html>

提前感谢您的帮助。 为了更好地理解,请随时问我...

【问题讨论】:

    标签: java spring forms spring-mvc thymeleaf


    【解决方案1】:

    尝试在你的registerAdd 方法中添加@ModelAttribute("register"),它应该看起来像

    @RequestMapping(value="/register/add", method=RequestMethod.POST)
    public String registerAdd(@ModelAttribute("register") @Valid Register register, BindingResult result, Model model) 
    

    如果是问题,请告诉我。

    编辑1

    将此行添加到您的 GET 映射中

    model.addAttribute("register", new Register());
    

    所以它应该是这样的:

    @RequestMapping(value = "/register", method = RequestMethod.GET)
    public String form(Model model) {
        model.addAttribute("register", new Register());
        return "register/form";
    }
    

    希望它能解决问题:)

    【讨论】:

    • 感谢您的回答。尽管如此,它并没有解决我的问题,我仍然收到我的错误('org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor'),这是由于我的 form.html 文件中的“th:field”。如果我更改“th: field" by "name" 我没有收到任何错误,但表单也没有保存(如我在 valid.html 文件中的表中或在我的控制台中使用我的 sysout 中所见)。
    • 您是否尝试过将原始 long 更改为 object Long?请记住在 getter 和 setter 中更改它。
    • 请检查我的最后一个答案,我已经进行了编辑:) 记住@ModelAttribute
    猜你喜欢
    • 2021-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-24
    • 1970-01-01
    • 1970-01-01
    • 2012-06-04
    • 1970-01-01
    相关资源
    最近更新 更多