Mencoba memvalidasi formulir menggunakan anotasi @NotNull @NotBlank tetapi tidak ada pesan yang ditampilkan di halaman web
_@ anotasi tidak berfungsi karena tidak ada validasi yang ditampilkan di halaman _
Periksa validasi formulir

Pesan kesalahan tidak ditampilkan

Formulir didefinisikan dalam JSP yang perlu divalidasi

JSP

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

<%@ taglib prefix="sf" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>BMI Calculator</title>
</head>
<body>
    <br>
    <br>
    <h1 id="heading">BMI Calculator</h1>
    <table>
    <caption style="font-weight: bolder;">Body Mass Index Calculator Form</caption>
    
    <sf:form action="getBMI" modelAttribute="user" method="GET" name="form">
            <tr>
                <td>Enter Name:</td> 
                <td><sf:input path="name" id="name"  required="true"
                        name="name" /> <sf:errors style="color:red" path="name" ></sf:errors></td>
            </tr>
                <tr>
                <td>Phone Number:</td>
                <td><sf:input path="phoneNumber" id="phoneNumber" required="true"
                        name="phoneNumber" /> <sf:errors  style="color:red" path="phoneNumber"></sf:errors></td>
            </tr>
                <tr>
                <td>Height in CM:</td>
                <td><sf:input path="height" id="height" required="true"
                        name="height" /> <sf:errors style="color:red" path="height" ></sf:errors></td>
            </tr>
                
                <tr>
                <td>Weight in KG:</td>
                <td><sf:input path="weight" id="weight" required="true"
                        name="weight" /> <sf:errors style="color:red" path="weight" ></sf:errors></td>
            </tr>
            
            <tr>
                <td>Gender:</td>
                <td><sf:select path="genderType" items="${genderList}"
                        id="genderType" name="genderType">
                        <sf:errors path="genderType"></sf:errors>
                    </sf:select><br></td>
            </tr>
            <tr>
                <td><input type="submit" id="submit" name="calculateBMI"
                    value="CalculateBMI"></td>
                <td><input type="reset" id="Clear" name="Clear" value="Clear"></td>
            </tr>
        </sf:form>
    </table>
    ${status}
</body>
</html>

Kelas pengontrol

KONTROLER

package com.cognizant.bmi.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.cognizant.bmi.bean.User;
import com.cognizant.bmi.service.BMIService;

@Controller
public class BMIController {
    @Autowired
private BMIService bmiService;
    @RequestMapping(value = "/bmiForm", method = RequestMethod.GET)
    public String showBMIForm(@ModelAttribute("user") User user) {
        return "bmiCalculatorForm";
    }
    @RequestMapping(value = "/getBMI", method = RequestMethod.GET)
    public String getBMIStatus(@ModelAttribute("user") @Valid User user, BindingResult result, ModelMap map) {
        if (result.hasErrors())
        {
            return "bmiCalculatorForm";
        }
        User user1=bmiService.addUserDetails(user);
        double bmi=bmiService.calculateBMI(user);
        String bmiStatus=bmiService.getBMIStatus(bmi);
        map.put("user.userId",user.getUserId());
        map.put("user.name", user.getName());
        map.put("user.height",user.getHeight());
        map.put("user.weight", user.getWeight());
        map.put("bmi", bmi);
        map.put("bmiStatus",bmiStatus);
    
        
        return "bmiStatus";

    }
    @ModelAttribute("genderList")
public List<String> populateGenderList() {
    return bmiService.genderType;
    }
}
2
Advaid 20 November 2020, 18:57

1 menjawab

Jawaban Terbaik

Masalahnya adalah saya menggunakan validasi pada JSP serta menggunakan @Annotation untuk memvalidasi

Menghapus "Diperlukan = 'benar'" dari masalah jsp diselesaikan dan sekarang berfungsi .
Terima kasih @Eniss atas tanggapan langsungnya
Snapshot Kerja