[JBoss Seam] - Re: HowTo: When going to a page to enter data, pull from db
by CptnKirk
It's late, but I'll give this a go. Christian or Pete feel free to jump in. It sounds like your mixing your retrieval and persistence mechanisms.
@Factory - Method called when a JSF page tries to reference a contextual variable that doesn't exist, but has a named factory method. So if #{activity} isn't found and there is a @Factory method, the method will be called and the contextual variable populated. The variable will have the scope defined by @Factory or if absent, the scope of the enclosing component. @Factory is only called if the contextual variable is null (needs to be created).
@Unwrap - Method called on a component that allows the component to manage the creation of a contextual variable. So Component ActivityManager might have an @Unwrap method that puts an Activity into scope referenceable by the EL #{activity}. This method will be called every time the #{activity} variable is referenced or injected into a bean. Because the @Unwrap annotation is on a method within a Seam Component, the component can take advantage of the entire Seam life cycle, including providing tear down logic.
EntityHome - These are classes the provide generic DAO capabilities to objects. By using an EntityHome you can Create, Update, Delete a single entity via its primary key. In order to Update or Delete with an EntityHome you must pass the ID of the object to the EntityHome prior to calling update or remove.
EJB3 Entity Beans - Seam components that are EJB3 Entity Beans are automatically registered as event scoped components. They have an implicit auto-create when used with JSF pages. So you could @Name("activity") your Activity EJB3 entity bean. And reference it in your JSF page as #{activity}. Seam will call the default constructor and you'll be able to use EL to set values and can inject @In("activity") into the controller that deals with the form action. Beans can have multiple names, and multiple scopes. Read more about the @Role/@Roles annotation for more information.
The Seam documentation covers these concepts in greater detail.
If all you want to do is supply simple CRUD operations, then you can use an EntityHome. By using an EntityHome it becomes your controller (action method), and thus you don't have to write one. EntityHome can be configured with FacesMessages to notify your users that CRUD operations have completed. Remember that in order to use EntityHome for update and remove you need to pass in an id. The developer guide shows how.
Based on the description you can decide whether @Factory or @Unwrap are best suited for your needs.
Also, by default JSF will populate an unfilled form with empty values, not null. So if you're trying to populate your #{activity} entity, have a blank form and hit submit. Your controller will be injected with a live object with the form values set to their empty state (empty Strings, 0 ints, etc). If you want these values to be null instead you'll need to write a custom converter. I've written a simple one below. Use the converter method or the <f:converter> tag. The version of MyFaces that ships with JBoss 4.0.5 doesn't seem to want to let you override the default String converter, so you'll need to specify the converter on every input component you'd like it on. This might change on the 1.2 RI, I've heard different things about the RI in the 4.2 CVS tree.
@Name("emptytonull")
| @org.jboss.seam.annotations.jsf.Converter(forClass=String.class)
| public class EmptyStringToNullConverter implements Converter, Serializable
| {
| public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) throws ConverterException
| {
| return arg2 == null || arg2.isEmpty() ? null : arg2;
| }
|
| public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) throws ConverterException
| {
| return arg2 == null ? "" : arg2.toString();
| }
| }
Hope this helps. I'm going to crash now.
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4039504#4039504
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4039504
19 years
[JBoss Seam] - Re: HowTo: When going to a page to enter data, pull from db
by saeediqbal1
Problem
Now one problem was fixed that it picked up stuff from database. But if the row does not exist, and then when i put stuff in the form and say save/submit ... it saves blank values. nothing null
This is what my xhtml page has at the bottom. Basically it was all generated by seam-gen (should i stop using seam-gen?)
<h:commandButton id="save"
| value="Save"
| action="#{activityHome.persist}"
| disabled="#{!activityHome.wired}"
| rendered="#{!activityHome.managed}"/>
|
| <h:commandButton id="update"
| value="Save"
| action="#{activityHome.update}"
| rendered="#{activityHome.managed}"/>
|
| <h:commandButton id="delete"
| value="Delete"
| action="#{activityHome.remove}"
| rendered="#{activityHome.managed}"/>
|
| <s:button id="done"
| value="Done"
| propagation="end"
| view="/Activityxhtml"
| rendered="#{activityHome.managed}"/>
|
| <s:button id="cancel"
| value="Cancel"
| propagation="end"
| view="/#{empty activityFrom ? 'ActivityList' : activityFrom}.xhtml"
| rendered="#{!activityHome.managed}"/>
As you can see from my previous post, my values are here #{activity.entrya} Not in #{activityHome.entrya} I dont understand whats the Home stuff.
I am pretty sure that save update delete stuff is messing my app up as it points to the activityHome
help.
While we are here. THis is what i have in my action bean
| @Out
| private Activity activity;
| @Factory("activity")
| public void actionMethod()
| {
| Activity activity= new Activity();
| try {
| activity= (Activity) em.createQuery("select o from Activity o where o.userid = :num")
| .setParameter("num", 1)
| .getSingleResult(); // the 1 number is temporary will change it
| }
| catch (NoResultException ex) {
| FacesMessages.instance().add("Enter new activity record.");
|
| }
|
| if(activity!= null){
| setActivity(activity);
| }
| }
|
|
Scope is event and it is stateless.
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4039500#4039500
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4039500
19 years
[JBoss Seam] - Re: What is the difference between EJB and POJO transactions
by christian.bauer@jboss.com
I haven't read the book, but the three basic transaction models are
- EJB/CMT: You have EJB components (session beans, MDBs) and you call these EJB components from whatever other code. By default, a system transaction will begin when an EJB method is called and it will be committed when that method returns. The transaction propagates into other calls, e.g. if your EJB calls another EJB. You can use annotations or XML metadata to change this default transaction assembly. Seam can emulate this behavior with the Seam-specific and proprietary @Transactional annotation on POJOs, then Seam acts as the container that manages transaction boundaries.
- JTA: You can call the JTA UserTransaction API in your application code somewhere and control the begin and end of a transaction programmatically.
- Seam-managed JTA: Seam will call the JTA API for you automatically during the JSF lifecycle, meaning it will wrap two transactions around a JSF request. One transaction starts when the request hits the server and is committed when INVOKE APPLICATION is complete. The second transaction spans only the RENDER RESPONSE phase. This is an optimized and recommended pattern in a Seam/JSF application. All EJBs and indeed any POJOs you call during that request are participating in that transaction. To enable it, configure JSF with the TransactionalSeamPhaseListener.
I recommend the last option, since it works with whatever components you execute and it is good from a scalability perspective. Also, you don't need to write any code or use any annotations with it.
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4039495#4039495
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4039495
19 years