-
1. Re: handling managed bean
nbelaevski Apr 27, 2010 2:17 PM (in response to pankajjain_1510)- Where is "request" variable used?
- How does page code look like?
-
2. Re: handling managed bean
pankajjain_1510 Apr 28, 2010 1:18 AM (in response to nbelaevski)request variable is not used any where.
let me explain you in more detail:
Item bean:
public class Item
{
private String itemno;
public String getItemno() {
return itemno;
}
public void setItemno(String itemno) {
this.itemno = itemno;
}}
controller.java:
public String navigation()
{
String outcome="success";
FacesContext facecontext=FacesContext.getCurrentInstance();
HttpServletRequest request = (HttpServletRequest)facecontext.getExternalContext().getRequest();
String link=request.getParameter("link");
System.out.println("Link value is--->"+link);
if(link.equals("view_inventory_items"))
{
outcome="view_inventory_items";
}
String itemno=null;
getItemDetail(itemno);
return outcome;
}private void getItemDetail(String itemno,int rowid)
{
Connection conn=connection.DBConnection.getConnection();
PreparedStatement pstm=null;
ResultSet rs=null;
String query=null;
query="select citemno from inventory limit 1";
System.out.println(query);
try
{
pstm=conn.prepareStatement(query);
rs=pstm.executeQuery();
while(rs.next())
{
setItemno(rs.getString("citemno"));
}
}
catch(SQLException sql)
{
sql.printStackTrace();
}
finally
{
try
{
if(rs!=null)
rs.close();
if(pstm!=null)
pstm.close();
if(conn!=null)
conn.close();
}
catch(SQLException sql)
{
sql.printStackTrace();
}
}
}item_link.jsp
<rich:panel header=" Select one please" style="height:160px">
<h:panelGrid columns="2">
<h:form>
<h:commandLink action="#{Controller.navigation}">
<f:param name="link" value="view_inventory_items" />View inventory items</h:commandLink>
<br />
<br />
<h:commandLink action="#">
<f:param name="link" value="view_purchase" />Add new inventory items</h:commandLink>
<br />
<br />
</h:form>
<h:graphicImage value="/images/S4697P-C.JPG" style="background-color:#F1F1F1;margin-left:230px"/>
</h:panelGrid>
</rich:panel>view_item.jsp
<h:panelGrid columns="2">
<h:outputText value="Item no:" />
<h:outputText value="#{Item.itemno}" />
<h:outputText value="Size:" />
<h:outputLabel value="" id="size" />
</h:panelGrid>i am not getting the value of #{Item.itemno} thoigh it is being set.
-
3. Re: handling managed bean
liuliu Apr 28, 2010 3:32 AM (in response to pankajjain_1510)hi,
your item is a request scope backingbean, it is initialised every request.
liuliu
-
4. Re: handling managed bean
pankajjain_1510 Apr 28, 2010 3:35 AM (in response to liuliu)no its not like that. backing bean is in session scope.
-
5. Re: handling managed bean
liuliu Apr 28, 2010 6:11 AM (in response to pankajjain_1510)In your post, you said i stored item bean in request scope in facesconfig.xml file . So i think it is request scope.
if it is session scope, i dont see you retrieve your item bean from the context in your code. Or do your use injection somewhere?
-
6. Re: handling managed bean
pankajjain_1510 Apr 28, 2010 7:12 AM (in response to liuliu)no i am not doing any thing else.
Any way , please look at the code i writtend for the second time in same post.please tell me if want any more information
Thanks
-
7. Re: handling managed bean
liuliu Apr 28, 2010 9:55 AM (in response to pankajjain_1510)there are serveral ways to do what you want.
like this one:
in faces-config.xhtml
<managed-bean>
<managed-bean-name>item</managed-bean-name>
<managed-bean-class>your class</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<managed-bean>
<managed-bean-name>controller</managed-bean-name>
<managed-bean-class>your class</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>
<property-name>item</property-name>
<value>#{Item}</value>
</managed-property>
</managed-bean>in controller class
declare a field Item item, add getter and setter for item, then you can use it directly.
hope
-
8. Re: handling managed bean
pankajjain_1510 May 18, 2010 8:08 AM (in response to liuliu)to what this value refers
<value>#{Item}</value>
???
-
9. Re: handling managed bean
liuliu May 18, 2010 8:18 AM (in response to pankajjain_1510)it is the backing bean you declared before this one
-
10. Re: handling managed bean
pankajjain_1510 May 22, 2010 1:16 AM (in response to liuliu)hi liumin , thanks for your feed back and it is working fine..
but i have one more problem
i am just creating a structure for a web application to be made in richface and hibernate.
just consider a basic transaction on item i.e find item, add item,delete item,edit item.
what i did is i created a ItemAction.java as a controller and ItemBean.java as a backingbean , ItemDao which handles all the buisness logic for add ,edit ,delete,find.
ItemAction and itemBean both are in session scope and ItemAction has ItemBean as a managed property as you solved it earlier.
ItemActio contains following code
{
private ItemBean item;
//getter and setter of item goes here
findItem() //this method is called when user clicks on search button , but before that he looks for item with the help of rich suggestion box
{
// now wat i did is
item=ItemDao.getInstance().findItem(item.getItemName());
//here wat i thinking is , after clicking on search button , name of item is stored in itemname property of ItemBean, so i can use it directly and in findItem of ItemDao i am creating new instance of bean and sending back , which is assigned to itme , but i am not getting the values returnd from dao thoug they are retrieved from DB correctly.
//But if i am doing like below
ItemDao.getInstance().findItem(item); //direcltly sending the bean to findItem of dao and there iteself setting the new value , i am getting the correct output.
would you please elaboreate me what is going wrong in first method of finditem()
}
addItem()
{}
}
-
11. Re: handling managed bean
liuliu May 27, 2010 3:45 AM (in response to pankajjain_1510)hi,
i think new instance you creat, it is not the one in the session, so it is not the one created by jsf.
liu
-
12. Re: handling managed bean
pankajjain_1510 May 27, 2010 9:08 AM (in response to liuliu)thank you so much for your feedback..
but..
if you look at very first method
item=ItemDao.getInstance().findItem(item.getItemName());
item is a property of Item.java and it is defined as managed property in faces-config.xml as follow:
<managed-bean>
<managed-bean-name>Item</managed-bean-name>
<managed-bean-class>action.Item</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>
<property-name>item</property-name>
<value>#{Itembean}</value>
</managed-property>
</managed-bean>
<managed-bean>
<managed-bean-name>Itembean</managed-bean-name>
<managed-bean-class>model.bean.Itembean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>please tell me what is gng wrong over there ???
-
13. Re: handling managed bean
liuliu May 27, 2010 10:36 AM (in response to pankajjain_1510)step 1 . item is injected by jsf, your item variable points on it
step 2. you changed the variable item in your first method, it points on the new instance you created, but jsf still points on the old instance.
in your 2nd method, the pointer is not changed. so it works
-
14. Re: handling managed bean
pankajjain_1510 May 28, 2010 2:59 AM (in response to liuliu)please look at the following code snippet
Item.java ---->
public class Item {
Connection con = ConnectToServer.Connect();
private Itembean itembean;
public Itembean getItembean() {
return itembean;
}public void setItembean(Itembean itembean) {
this.itembean = itembean;
}public Item()
{
System.out.println("Item Constructor");}
public ArrayList<Itembean> getItemList(Object suggest)// this code belongs to rich:suggestion box
{
//itembean.clear();
ArrayList<Itembean> list=new ArrayList<Itembean>();
Itemdao itemDao=new Itemdao();
list=itemDao.getItemList(suggest);//setItemList(itemList);
return list;
}/**
*
* @return
*/
public String findItem() //this method is called after search button is clicked from UI
{
System.out.println("name of item-- in bean -------------->"+itembean.getItemname());
Itemdao dao=new Itemdao();
itembean=dao.findItem(itembean.getItemname());// here if you see dao.findItem returns itembean look at that method below and please let me know why itembean here is not being set ??
return "display";
}dao.findItem
public Itembean findItem(String itemname) {
Itembean item=new Itembean();
Connection con = ConnectToServer.Connect();
PreparedStatement pstm = null;
ResultSet rs = null;
String queryFindItem = "select * from gnd_item where itemname='"
+ itemname() + "'";
System.out.println(queryFindItem);
try {
pstm = con.prepareStatement(queryFindItem);
rs = pstm.executeQuery();
if (rs.next() != false) {
item.setItemno(rs.getInt("Itemno"));
item.setItemname(rs.getString("itemname"));
item.setDescription(rs.getString("description"));
}} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null)
rs.close();
if (pstm != null)
pstm.close();
} catch (SQLException sql) {
sql.printStackTrace();
}}
return item;
}my faces-config.xml
<managed-bean>
<managed-bean-name>Item</managed-bean-name>
<managed-bean-class>com.gnd.kq.action.Item</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>
<property-name>itembean</property-name> // pointing to itembean declared in Item.java
<value>#{Itembean}</value> // pointing to following itembean
</managed-property>
</managed-bean>
<managed-bean>
<managed-bean-name>Itembean</managed-bean-name>
<managed-bean-class>com.gnd.kq.model.bean.Itembean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>