저장 버튼을 클릭 한 후 데이터베이스에 저장되는 int 및 문자열 값을 사용자가 입력하는 jsf 형식을 사용합니다.데이터베이스에 수동으로 설정된 값 저장
<h:form id="TbtestCreateForm">
<p:inputText value="#{tbtestController.selected.name}" />
<p:commandButton actionListener="#{tbtestController.saveNew}" value="#{myBundle.Save}" />
</h:form>
나는 AbstractFacade 그 '바인딩'엔티티 클래스를 사용하여 인 AbstractController 클래스에 정의 된 CRUD 작업을 수행 Facade 패턴과 EJB를 사용하고 있습니다.
내가 필요한 건 하드 코드/수동으로 형식의 문자열 값을 설정하고 동일한 작업 (동일한 개체)에서 사용자가 알리는 int 값과 함께 데이터베이스에 저장합니다.
나는 다음과 같이하려고 노력했지만 성공하지 :
@Named
public class MyBean implements Serializable {
@Inject
TbtestController tbtestController;
private String myName;
//GETTERS AND SETTERS
@PostConstruct
public void init() {
myName = "some foo name";
Tbtest myTbtest = new Tbtest();
myTbtest.setName(myName);
tbtestController.setSelected(myTbtest);
tbtestController.saveNew(null); // to simulate what the savNew method do
}
난에 복제 할 사용할 수있는 작은 넷빈즈 프로젝트를 만들어했습니다 아래 https://github.com/f6750699/webAppTest.git
에 정의 된 두 가지 방법을 다음과 AbstractController에 클래스 :
saveNew 방법 :
public void saveNew(ActionEvent event) {
String msg = ResourceBundle.getBundle("/MyBundle").getString(itemClass.getSimpleName() + "Created");
persist(PersistAction.CREATE, msg);
if (!isValidationFailed()) {
items = null; // Invalidate list of items to trigger re-query.
}
}
이미 잠시 동안 거기에 붙어있어 때문에,
private void persist(PersistAction persistAction, String successMessage) {
if (selected != null) {
this.setEmbeddableKeys();
try {
if (persistAction != PersistAction.DELETE) {
this.ejbFacade.edit(selected);
} else {
this.ejbFacade.remove(selected);
}
JsfUtil.addSuccessMessage(successMessage);
} catch (EJBException ex) {
Throwable cause = JsfUtil.getRootCause(ex.getCause());
if (cause != null) {
if (cause instanceof ConstraintViolationException) {
ConstraintViolationException excp = (ConstraintViolationException) cause;
for (ConstraintViolation s : excp.getConstraintViolations()) {
JsfUtil.addErrorMessage(s.getMessage());
}
} else {
String msg = cause.getLocalizedMessage();
if (msg.length() > 0) {
JsfUtil.addErrorMessage(msg);
} else {
JsfUtil.addErrorMessage(ex, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
}
}
}
} catch (Exception ex) {
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
JsfUtil.addErrorMessage(ex, ResourceBundle.getBundle("/MyBundle").getString("PersistenceErrorOccured"));
}
}
}
정말 그것으로 어떤 도움을 주셔서 감사합니다 :
는 방법을 지속.
미리 감사드립니다.
폴 모리스 솔루션
바울이 제안 내가, 하나 개의 클래스에 집중했습니다
@Named
@ViewScoped
public class TbtestController extends AbstractController<Tbtest> {
@EJB
private TbtestFacade ejbFacade;
public TbtestController() {
super(Tbtest.class); //-> there is already an invocation here, see below
}
@PostConstruct
@Override
public void init() {
super.setFacade(ejbFacade);
//Paul Morris solution below
myName = "some foo name";
this.setMyName(myName);
this.saveNew(null); // to simulate what the savNew method do
}
private String myName;
public void setMyName(String name) {
myName = name;
}
@Override
public void saveNew(ActionEvent event) {
this.getSelected().setName(myName);
//super.(event); // -> invocation of a superclass constructor must be the first line in the subclass constructor, but there is already one invocation, see above. Then, I've tried to invoke the super method:
super.saveNew(event); // but this didn't resolved, as it launches a NullPointerException, see below.
}
}
예외 :
org.jboss.weld.exceptions.WeldException: WELD-000049: Unable to invoke public void beans.TbtestController.init() on [email protected]
[...]
Caused by: java.lang.NullPointerException
at beans.TbtestController.saveNew(TbtestController.java:40)
at beans.TbtestController.init(TbtestController.java:29)
... 87 more
"* 성공하지 못함 *". 이 EL'# {tbtestController.selected.name}'이 (가) 'null'로 평가 되었습니까? 약간의 오류/예외 또는 심지어 완전히 다른/이상한 일이 발생합니까? – Tiny
아니요,이 EL은 사용자가 문자열 값을 입력 할 때 제대로 작동하지만 정수 만 입력하면 문자열을 수동으로 설정해야합니다. 내가 시도한 것은'# {myBean.myName}'이며'value = "# {myBean.myName}": Target Unreachable, 식별자 'myBean'이 null로 해석되었습니다. 어떤 클래스에서 값을 설정하거나 하드 코딩해야하지만, 어디서 ... – jMarcel
CDI 빈의 기본 범위는'@ Dependent'입니다. 당신이 필요로하는 적절한 범위를 지정해 주거나 bean이'@ Dependent' 범위를 가질 수 있습니까? – Tiny