Witam. Planowałem się podszkolić w zakresie korzystania z JavaBeans i stworzyłem projekt (Dynamic Web Project) w Eclipse bazujący na https://docs.oracle.com/javaee/5/tutorial/doc/bnbod.html?fbclid=IwAR1XlV-k6bF8H07EX1JG5Fr62CHznqac3wN4fRv7-hYOc8ji2nN0Mi8U26Q#bnboj. Mam w Eclipse skonfigurowany serwer WildFly pod Java EJB i nie wiem co powinienem dodać/zmienić abym zobaczył wynik mojego kodu. Może coś z formularzem i request.getParameter() by się przydało dodać, nie wiem. Liczę na waszą pomoc.
Oto struktura projektu(pliki w webapp są wygenerowane domyślnie)
package pakietInterfejsow;
import java.util.List;
import javax.ejb.Remote;
import Exceptions.BookException;
@Remote
public interface Cart {
public void initialize(String person) throws BookException;
public void initialize(String person, String id)
throws BookException;
public void addBook(String title) throws BookException;
public void removeBook(String title) throws BookException;
public List<String> getContents();
public void remove();
}
package Exceptions;
public class BookException extends Exception {
private static final long serialVersionUID = 1L;
public BookException() {
super();
}
public BookException(String message) {
super(message);
}
public BookException(String message, Throwable cause) {
super(message, cause);
}
public BookException(Throwable cause) {
super(cause);
}
}
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateful;
import Exceptions.BookException;
import jakarta.ejb.Remove;
@Stateful
public class CartBean implements pakietInterfejsow.Cart{
String customerName;
String customerId;
List<String> contents;
@Override
public void initialize(String person) throws BookException {
// TODO Auto-generated method stub
if(person == null) {
throw new BookException("Null person not allowed.");
}else {
this.customerName = person;
}
customerId = "0";
contents = new ArrayList<String>();
}
@Override
public void initialize(String person, String id) throws BookException {
// TODO Auto-generated method stub
if(person == null) {
throw new BookException("Null person not allowed.");
}else {
this.customerName = person;
}
IdVerifier idChecker = new IdVerifier();
if(idChecker.validate(id) == true) {
this.customerId = id;
} else {
throw new BookException("Invalid id:" + id);
}
contents = new ArrayList<String>();
}
@Override
public void addBook(String title) throws BookException{
// TODO Auto-generated method stub
if(title == null || title.isEmpty())
throw new BookException("Invalid title.");
else
contents.add(title);
}
@Override
public void removeBook(String title) throws BookException {
// TODO Auto-generated method stub
boolean result = contents.remove(title);
if(result == false) {
throw new BookException(title + " is not in a cart.");
}
}
@Override
public List<String> getContents() {
// TODO Auto-generated method stub
return contents;
}
@Remove
public void remove() {
// TODO Auto-generated method stub
contents = null;
}
}
public class IdVerifier {
boolean validate(String id) {
return id!=null && !id.isEmpty();
}
}