pobieranie wartosci id z DB, wyswietlanie jej w JSP

0

czesc, staram sie pobrac z bazy danych konkretny obiekt, a nastepnie wyswietlic jego zawartosc na stronie JSP.

klasa Snake dziedziczy po klasie Animal

klasa Animal

package kaczynski.impl;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;

import kaczynski.DBaccess.AnimalDAO;
import kaczynski.api.InputController;
import kaczynski.domain.ApplicationConsoleInterface;

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Animal {

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

	private String name;

	private int age;
	@Column(name="date_of_birth")
	private String dateOfBirth;

	private double weight;

	public Animal(String x) {
		String date = null;
		Scanner input = new Scanner(System.in);

		System.out.println("Podaj imie");
		String name = input.nextLine();
		this.setName(name);

		System.out.println("Podaj wiek");
		int age = InputController.intInput(input);
		this.setAge(age);

		System.out.println("Podaj wage");
		double weight = InputController.doubleInput(input);
		this.setWeight(weight);

		do {
			System.out.println("Podaj date narodzin w formacie DD-MM-YYYY");
			date = input.nextLine();
		} while (!InputController.dateValidation(date));

		this.setDateOfBirth(date);

	}

	public Animal(String name, int age, String dateOfBirth, double weight) {
		this.name = name;
		this.age = age;
		this.dateOfBirth = dateOfBirth;
		this.weight = weight;
	}

	public Animal() {
	}

	

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getDateOfBirth() {
		return dateOfBirth;
	}

	public void setDateOfBirth(String dateOfBirth) {
		this.dateOfBirth = dateOfBirth;
	}

	public double getWeight() {
		return weight;
	}

	public void setWeight(double weight) {
		this.weight = weight;
	}

	@Override
	public String toString() {
		return "id= "+id+" name=" + name + ", age=" + age + ", dateOfBirth=" + dateOfBirth + ", weight=" + weight;
	}

	public String tease() {
		return "";
	}

	public String giveVoice() {
		return "";
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	
	

}// end of class

klasa snake:

package kaczynski.impl;

import java.time.LocalDate;
import java.util.Scanner;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import kaczynski.api.InputController;
import kaczynski.api.VitalFunctions;

@Entity
public class Snake extends Animal implements VitalFunctions {
	
	
	@Column(name="is_venomous")
	private boolean isVenomous;

	private double length;

	public Snake(String x) {
		super(x);
		Scanner input = new Scanner(System.in);
		System.out.println("Is Venomous?");
		String answer = input.nextLine();
		this.isVenomous=InputController.answerValidation(answer);
		System.out.println("How long is the snake");
		Double length = InputController.doubleInput(input);
		this.length = length;
	}

	public Snake(boolean isVenomous, double length, String name, int age, String dateOfBirth, double weight) {
		super(name, age, dateOfBirth, weight);
		this.isVenomous = isVenomous;
		this.length = length;
	}

	public Snake() {
	}

	@Override
	public String toString() {
		return "Snake [isVenomous=" + isVenomous + ", length=" + length + super.toString() + "]";
	}

	@Override
	public String giveVoice() {
		return "Ssssssss";
	}

	@Override
	public String tease() {
		return "That was not a good idea, snake bites you and you die";
	}

	

	public double getLength() {
		return length;
	}

	

	public boolean isVenomous() {
		return isVenomous;
	}

	public void setVenomous(boolean isVenomous) {
		this.isVenomous = isVenomous;
	}

	public void setLength(double length) {
		this.length = length;
	}
	
}

teraz kod JSP, ktory wyswietla obiekt Snake

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>
<head>Szczegoly</head>

<body>


Dane dotyczace zwierzaka:
${animalDetails}
</br>




</body>

taki kod wyswietla obiekt snake w wersji toString, wszystkie jego pola sa widoczne - ok!
tak wyglada output :

Szczegoly Dane dotyczace zwierzaka: Snake [isVenomous=true, length=5.6id= 6 name=Sekacz, age=2, dateOfBirth=17-11-2032, weight=6.5] 

moge sobie rowniez wołać poszczegolne pola np ${animalDetails.name} i wtedy dziala wszystko tez jak powinno.

jesli jednak chce wywolac ${animalDetails.isVenomous} dostaje error

Stacktrace:] with root cause
javax.el.PropertyNotFoundException: Property 'isVenomous' not found on type kaczynski.impl.Snake

pytanie - why?

1

Może dlatego, że jak masz metodę isVenomous(), to pole powinno się nazywać venomous.

1 użytkowników online, w tym zalogowanych: 0, gości: 1