CDI bean, @Inject and params life cycle

Kind and bean worked until I tried to fix non-standard names, and now I broke the connection between them. Oddly enough, the back button has the correct link, but the content simply is not displayed and is not registered. Why is the Detail.getComments () function not executing?

I went through the Weld docs and tried to better understand @Inject. It seems to be a life cycle problem that I also don’t understand. If this is not a life cycle, then I can’t even guess why Detail.getComments () is never displayed in fiberglass logs:

INFO: MessageBean.getModel.. INFO: SingletonNNTP.returning messages.. INFO: MessageBean.getModel.. INFO: SingletonNNTP.returning messages.. INFO: MessageBean.getModel.. INFO: SingletonNNTP.returning messages.. INFO: Detail.. INFO: Detail.getId..null INFO: Detail.getId..SETTING DEFAULT ID INFO: Detail.onLoad..2000 INFO: Detail.getId..2000 INFO: Detail.getId..2000 INFO: Detail.setId..2000 INFO: Detail.getId..2019 INFO: ..Detail.setId 2019 INFO: Detail.back.. INFO: Detail.getId..2019 INFO: ..Detail.back 2,018 INFO: Detail.getId..2019 

The value 2000 is the default, which occurs only when id == null, which should never be. He must immediately remove this attribute. So, I'm not sure if the problem is with the scope (I just found out that CDI does not support @SessionScoped), life cycle or something else. Maybe I need to use @Inject for this variable?

View, detail.xhtml:

 <?xml version='1.0' encoding='UTF-8' ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html"> <body> <f:metadata> <f:viewParam name="id" id="id" value="#{detail.id}" /> </f:metadata> <ui:composition template="./complexTemplate.xhtml"> <ui:define name="top"> <h:link value="back" outcome="detail" includeViewParams="true"> <f:param name="id" value="#{detail.back}"/> </h:link> <ui:define name="content"> <h:outputText value="#{detail.content}" rendered="false"/> </ui:define> <ui:define name="bottom"> bottom </ui:define> </ui:composition> </body> </html> 

and bean support:

 package net.bounceme.dur.nntp; import java.io.Serializable; import java.util.logging.Level; import java.util.logging.Logger; import javax.annotation.PostConstruct; import javax.enterprise.context.ConversationScoped; import javax.inject.Named; import javax.mail.Message; @Named @ConversationScoped public class Detail implements Serializable { private static final long serialVersionUID = 1L; private static final Logger logger = Logger.getLogger(Detail.class.getName()); private static final Level level = Level.INFO; private String id = null; //should never get default value in getter private Message message = null; private SingletonNNTP nntp = SingletonNNTP.INSTANCE; private String forward = null; //id + 1 private String back = null; //id - 1 private String content = null; //message.content public Detail() { logger.log(level, "Detail.."); } @PostConstruct private void onLoad() { logger.log(level, "Detail.onLoad..{0}", getId()); } public Message getMessage() { logger.log(level, "Detail.getMessage.."); return message; } public void setMessage(Message message) { logger.log(level, "Detail.setMessage.."); this.message = message; } public String getId() { logger.log(level, "Detail.getId..{0}", id); if (id == null) { logger.log(level, "Detail.getId..SETTING DEFAULT ID"); id = String.valueOf(2000); } return id; } public void setId(String id) throws Exception { logger.log(level, "Detail.setId..{0}", getId()); this.id = id; logger.log(level, "..Detail.setId {0}", getId()); } public String getForward() { logger.log(level, "Detail.forward.."); int f = Integer.parseInt(getId()); f = f + 1; logger.log(level, "..Detail.forward {0}", f); forward = String.valueOf(f); return forward; } public void setForward(String forward) { this.forward = forward; } public String getBack() { logger.log(level, "Detail.back.."); int b = Integer.parseInt(getId()); b = b - 1; logger.log(level, "..Detail.back {0}", b); back = String.valueOf(b); return back; } public void setBack(String back) { this.back = back; } public String getContent() throws Exception { logger.log(level, "Detail.getContent..{0}", getId()); message = nntp.getMessage(Integer.parseInt(getId())); content = message.getContent().toString(); return content; } public void setContent(String content) { this.content = content; } } 

which, apparently, does not, according to the above logs, call Detail.getContent (), even though it is part of the view: <h:outputText value="#{detail.content}" rendered="false"/>

It is odd that Detail.content () was called before I changed this class to better follow the naming conventions. I am going through Weld and Oracle Java EE 6 docs, but completely ignore the exact manual. The documents I find describe this invariably use @ManagedBeans, but I do not. There seem to be a lot of gotchas as described in this @Kawu answer.

Adding @Inject to the id field causes a deployment error:

 init: deps-module-jar: deps-ear-jar: deps-jar: library-inclusion-in-archive: library-inclusion-in-manifest: compile: compile-jsps: In-place deployment at /home/thufir/NetBeansProjects/NNTPjsf/build/web Initializing... deploy?DEFAULT=/home/thufir/NetBeansProjects/NNTPjsf/build/web&name=NNTPjsf&contextroot=/NNTPjsf&force=true failed on GlassFish Server 3.1.2 Error occurred during deployment: Exception while loading the app : WELD-001408 Unsatisfied dependencies for type [String] with qualifiers [@Default] at injection point [[field] @Inject private net.bounceme.dur.nntp.Detail.id]. Please see server.log for more details. /home/thufir/NetBeansProjects/NNTPjsf/nbproject/build-impl.xml:749: The module has not been deployed. See the server log for details. BUILD FAILED (total time: 9 seconds) 

Of course, injecting a string is not a problem, perhaps it is a mistake .

0
source share
1 answer

I understand your disappointment, and I see that the problem is more your setup / understanding in general. Nevertheless, it is quite difficult to find any real questions for an answer, maybe you can try to share your problems next time.

Here are some answers:

Why is the Detail.getComments () function not executing?

Hm maybe because it's not in a bean? I assume you are returning to detail.getContent ?

which apparently never has, according to the above logs, Detail.getContent () is called, even though it is part of the view:

Try rendered = true

 @PostConstruct private void onLoad() { logger.log(level, "Detail.onLoad..{0}", getId()); } 

You put a lot of logic into the getter. Try debugging with a field, not a getter ...

The value 2000 is the default, which only happens when id == null, which should never be.

It looks like private String id = null; is the perfect explanation why id will be null.

Try to keep in mind that modern frameworks like JSF, CDI, and Java EE do a lot of things behind the scenes using reflection, proxies, and interceptors. Do not rely on the classic understanding of when (and how often) a constructor is called, for example.

Again, consider moving the initialization logic away from the receiver. @PostConstruct is the place that the fathers of the Java EE-spec chose.

To be honest: nothing seems extremely wrong, but your code is a bit dirty and it is very difficult to understand and follow.

Try to remove all indirect actions like this ...

 int b = Integer.parseInt(getId()); 

... and everything will look much better.

Oh, and is there a specific reason why you declare a fixed log level for the entire class? Try something like this

 private static final Logger LOG = Logger.getLogger(Some.class); ... LOG.info("..."); 

Hope you get started. Feel free to post additional questions, preferably a little shorter and with single, isolated aspects to answer.

+4
source

All Articles