Home > Enterprise >  Problems displaying values ​with thymeleaf
Problems displaying values ​with thymeleaf

Time:10-30

I recently started using Spring with Thymeleaf and when making a project I had this error.I apologize if the question is stupid but I can't find the solution. I have two model classes, Team Home and TeamAway and a third class Match that has to give me statistics based on the fields of TeamHome and TeamAway.I managed to create TeamHome and TeamAway utility methods but I can't do it with the third class because it only has to give me back calculations. How can I proceed?

Model Team Home

@Entity
public class TeamAway {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int teamAwayId;

private String nameTeamAway;
private int goalsScoredAway;
private int goalsConcededAway;
private int gamesPlayedAway;/* Getter and Setter omitted */

Model Team Away

@Entity
public class TeamHome {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int teamHomeId;

private String nameTeamHome;
private int goalsScoredHome;
private int goalsConcededHome;
private int gamesPlayedHome;/* Getter and Setter omitted */

Model Match class

@Entity
    public class Match {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;
    
    private final double e = 2.718283;
    
    private final double calcoloA = (((this.teamHome.getGoalsScoredHome()/this.teamHome.getGamesPlayedHome()) 
            (this.teamAway.getGoalsConcededAway()/this.teamAway.getGamesPlayedAway()))/2.0) ;
    private final double calcoloB = (((this.teamAway.getGoalsScoredAway()/this.teamAway.getGamesPlayedAway()) 
            (this.teamHome.getGoalsConcededHome()/this.teamHome.getGamesPlayedHome()))/2.0);

    private final double teamHomeEnd0 = (((Math.pow(this.calcoloA, 0)) *
            Math.pow(e, this.calcoloA)) / 1.0);
    private final double teamHomeEnd1 = (((Math.pow(this.calcoloA, 1.0)) *
            Math.pow(e, this.calcoloA)) / 1.0);
    private final double teamHomeEnd2 = (((Math.pow(this.calcoloA, 2.0)) *
            Math.pow(e, this.calcoloA)) / 2.0);
    private final double teamHomeEnd3 = (((Math.pow(this.calcoloA, 3.0)) *
            Math.pow(e, this.calcoloA)) / 6.0);
    private final double teamHomeEnd4= (((Math.pow(this.calcoloA, 4.0)) *
            Math.pow(e, this.calcoloA)) / 24.0);
    private final double teamHomeEnd5 = (((Math.pow(this.calcoloA, 5.0)) *
            Math.pow(e, this.calcoloA)) / 120.0);
    private final double teamHomeEnd6 = (((Math.pow(this.calcoloA, 6.0)) *
            Math.pow(e, this.calcoloA)) / 720.0);

    private final double teamAwayEnd0 = (((Math.pow(this.calcoloB, 0)) *
            Math.pow(e, this.calcoloB)) / 1.0);
    private final double teamAwayEnd1 = (((Math.pow(this.calcoloB, 1.0)) *
            Math.pow(e, this.calcoloB)) / 1.0);
    private final double teamAwayEnd2 = (((Math.pow(this.calcoloB, 2.0)) *
            Math.pow(e, this.calcoloB)) / 2.0);
    private final double teamAwayEnd3 = (((Math.pow(this.calcoloB, 3.0)) *
            Math.pow(e, this.calcoloB)) / 6.0);
    private final double teamAwayEnd4 = (((Math.pow(this.calcoloB, 4.0)) *
            Math.pow(e, this.calcoloB)) / 24.0);
    private final double teamAwayEnd5 = (((Math.pow(this.calcoloB, 5.0)) *
            Math.pow(e, this.calcoloB)) / 120.0);
    private final double teamAwayEnd6 = (((Math.pow(this.calcoloB, 6.0)) *
            Math.pow(e, this.calcoloB)) / 720.0);

    

    //Values ​​I want to recall with thymeleaf with controller
    private double teamH0_teamA0 = this.teamHomeEnd0 *
            this.teamAwayEnd0 * 100;
    private double teamH1_teamA1 = this.teamHomeEnd1 *
            this.teamAwayEnd1 * 100;
    private double teamH2_teamA2 = this.teamHomeEnd2 *
            this.teamAwayEnd2 * 100;
    private double teamH3_teamA3 = this.teamHomeEnd3 *
            this.teamAwayEnd3 * 100;
    private double teamH4_teamA4 = this.teamHomeEnd4 *
            this.teamAwayEnd4 * 100;
    private double teamH5_teamA5 = this.teamHomeEnd5 *
            this.teamAwayEnd5 * 100;
    private double teamH6_teamA6 = this.teamHomeEnd6 *
            this.teamAwayEnd6 * 100;


    //@OneToOne(cascade = CascadeType.ALL)
    private TeamHome teamHome;
    //@OneToOne(cascade = CascadeType.ALL)
    private TeamAway teamAway;

i have to recall these values ​​and show them with thymeleaf but i don't know how to proceed. sorry if it's poorly written it's my first post. thanks for any replies.

CodePudding user response:

The fact that a team is an away team or a home team should be modeled in the Match class, you don't need TeamAway and TeamHome as separate classes.

I would advice to create 1 class Team like this:

@Enity
public class Team {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private int id;

  private String name;
  private int goalsScored;
  private int goalsConceded;
  private int gamesPlayed;

  /* Getter and Setter omitted */

}

Next, you can create the Match entity like this:

@Entity
public class Match {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Integer id;

  @ManyToOne
  private Team teamHome;
  
  @ManyToOne
  private Team teamAway;

  /* Getter and Setter omitted */
}

I changed your @OneToOne to @ManyToOne as I assume a single team can play multiple matches.

The calculations can be left in the Match class, you just need to foresee getters so the controller can access the results.

@Controller
@RequestMapping("/")
public class MatchController {

  private final MatchService service;

  public MatchController(MatchService service) {
    this.service = service;
  }

  @GetMapping
  public String index(Model model) {
    Match match = service.getMatch(...); //somehow get a `Match` instance from the Service.
    
    model.addAttribute("teamH0_teamA0", match.get....); // use the correct getter on the `match` instance

    // call `addAttribute` for each of the values you want to display on the Thymeleaf page

    return "match" // return the name of your Thymeleaf template. In this case, the template should be `src/main/resources/templates/match.html`
  }

}

In the Thymeleaf template, use the attributes:

<div th:text="${teamH0_teamA0}"></div>

CodePudding user response:

You want to create Team and map team_id/team_name columns with with your both Entity

Now we have Composite primary key so, we use @IdClass() for them

Composite key -> Primary key that is made by the combination of more than one attribute

Here down is my code:

Here we can make TeamIdName for composite primary key

TeamIdName

public class TeamIdName implements Serializable{
    private int team_id;
    private String team_name;
    //getter setter
}

Team

@Entity
@IdClass(TeamIdName.class)
public class Team {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int team_id;

    @Id
    private String team_name;

    @OneToMany(mappedBy = "team")
    private Set<MatchHome> matchHome;

    @OneToMany(mappedBy = "team")
    private Set<MatchAway> matchAway;

    //getter setter
}

MatchHome

@Entity
public class MatchHome {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int team_home_id;

    private int goals_scored_home;
    private int goals_conceded_home;
    private int games_played_home;

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumns({ @JoinColumn(name = "home_id", referencedColumnName = "team_id"),
            @JoinColumn(name = "home_name", referencedColumnName = "team_name") })
    private Team team;

    //getter setter
}

MatchAway

@Entity
public class MatchAway {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int team_away_id;

    private int goals_scored_away;
    private int goals_conceded_away;
    private int games_played_away;

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumns({ @JoinColumn(name = "away_id", referencedColumnName = "team_id"),
            @JoinColumn(name = "away_name", referencedColumnName = "team_name") })
    private Team team;

    // getter setter
}

For merge two table data you want to create DTO

TeamJoinDto

public interface TeamJoinDto {
    public Integer getTeam_home_id();
    public String getName_team_home();
    public Integer getGoals_scored_home();
    public Integer getGoals_conceded_home();
    public Integer getGames_played_home();
    
    public Integer getTeam_away_id();
    public String getName_team_away();
    public Integer getGoals_scored_away();
    public Integer getGoals_conceded_away();
    public Integer getGames_played_away();
}

Repository

public interface TeamHomeRepo extends JpaRepository<MatchHome, Integer>{
    @Query(nativeQuery =  true, value = "SELECT  * FROM match_home as mh INNER JOIN match_away AS ma ON mh.home_id = ma.away_id")
    public List<TeamJoinDto> MergeTeam();
}

Service

public interface TeamService {
    public List<TeamJoinDto> MergeTeam();
}

ServiceImpl

public interface TeamServiceImpl {
    @Autowired
    private TeamHomeRepo teamHomeRepo;

    @Override
    public List<TeamJoinDto> MergeTeam() {
        return this.teamHomeRepo.MergeTeam();
    }
}

Controller

public class TeamController {
    @Autowired
    private TeamService teamService;
    
    @RequestMapping(value = "/team", method = RequestMethod.GET)
    public String getTeam(Model mdl) {
         List<TeamJoinDto> teamData = this.teamService.MergeTeam();
         mdl.addAttribute("teamData", teamData);
         return "teamPg";
    }
}

teamPg(Thymeleaf)

<th:block th:each="iterateTeamData: ${teamData}">
            <th:block th:each="iterateSubData: ${iterateTeamData}">
                <span th:text=" 'calcocoA : '   (${iterateSubData.goals_scored_home / iterateSubData.games_played_home}   ${iterateSubData.goals_conceded_away / iterateSubData.games_played_away}) / 2"></span>
            </th:block>
</th:block>
  • Related