首页 > 解决方案 > Java Spring Boot 访问服务类中的对象

问题描述

这里的新编码器实际上是学生。

所以我在班上遇到了一个没人能解决的问题,因为我们在家工作,所以我不能问任何人。

我们的任务是创建一个简单的 API,我们可以在其中通过 HTML 输入我们的内容,Spring Boot 应用程序在 ServiceLayer 类中返回我们的 gpa。

所以我现在的问题是,我无法访问 Object.size,所以我可以通过 for 循环进行迭代,并基本上总结了我所有的元素。

我已经被困了将近一周,找不到 Spring Boot 的解决方案,因为我们是框架的新手。

public class Student {


     // private final double anzahl = 4;

    private double mathGrade;
    private double germanGrade;
    private double englishGrade;
    private double codeGrade;
//  private double avgGrade;



    public double getMathGrade() {
        return mathGrade;
    }

    public void setMathGrade(double mathGrade) {
        this.mathGrade = mathGrade;
    }

    public double getGermanGrade() {
        return germanGrade;
    }

    public void setGermanGrade(double germanGrade) {
        this.germanGrade = germanGrade;
    }

    public double getEnglishGrade() {
        return englishGrade;
    }

    public void setEnglishGrade(double englishGrade) {
        this.englishGrade = englishGrade;
    }

    public double getCodeGrade() {
        return codeGrade;
    }

    public void setCodeGrade(double codeGrade) {
        this.codeGrade = codeGrade;
    }


    @Override
    public String toString() {
        return "Student{" +
        "mathGrade=" + mathGrade +
        ", germanGrade=" + germanGrade +
        ", englishGrade=" + englishGrade +
        ", codeGrade=" + codeGrade +
        '}';
    }
}
package com.Beispiel.math.student;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;      
import org.springframework.web.bind.annotation.PostMapping;

@Controller
public class StudentController {

    private static final Logger log = LoggerFactory.getLogger(StudentController.class);

    @Autowired
    private StudentService studentService;


    @GetMapping("/register")
    public String GetGrades(Model model) {
        Student student = new Student();
        model.addAttribute("student",student);
        return "register_form";
    }

    @PostMapping("/register")
    public String submitGrades(@ModelAttribute("student") Student student){
        log.trace(String.valueOf(student));
        return "register_success";
    }
}
package com.Beispiel.math.student;

import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class StudentService {

    public String submitGrades(){
        Student student = new Student();
        for (int i = 0; i < student.getsize; i++) {
            student.getAvgGrade += student.get(i);
        }
        return String.valueOf(student.getAvgGrade());
    }
}
<!DOCTYPE html>
<html xmlns:th="http://thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>User Registration</title>
    <style type="text/css">
        label {
            display: inline-block;
            width: 200px;
            margin: 5px;
            text-align: left;
        }
    </style>
</head>
<body>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<div align="center">
    <h1>User Registration</h1>
    <form action="#" th:action="@{/register}" th:object="${student}" method="post">
        <label>Mathe Grade : </label>
        <input type="number" th:field="*{mathGrade}"/>

        <label>German Grade : </label>
        <input type="number" th:field="*{germanGrade}"/>

        <label>English Grade : </label>
        <input type="number" th:field="*{englishGrade}"/>

        <label>Code Grade : </label>
        <input type="number" th:field="*{codeGrade}"/><br><br><br><br>

        <button type="submit">Register</button>
    </form>
</div>

</body>
    <!DOCTYPE html>
    <html xmlns:th="http://thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>Registration Success</title>
        <style type="text/css">
        span {
            display: inline-block;
            width: 200px;
            text-align: left;
        }
    </style>
</head>
<body>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    
    <div align="center">
        <h1>User Registration succeeded</h1>
        <span>Math Grade :</span><span th:text="${student.mathGrade}"></span>
        <span>German Grade :</span><span th:text="${student.germanGrade}"></span>
        <span>English Grade :</span><span th:text="${student.englishGrade}"></span>
        <span>Code Grade :</span><span th:text="${student.codeGrade}"></span>
    </div>
    
</body>
</html>

标签: javahtmlspring-bootoopmodel-view-controller

解决方案


推荐阅读