Guest User

AJAX and ViewParms w/ JSF 2.0

a guest
Jan 3rd, 2011
200
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //XHTML Page
  2. <?xml version='1.0' encoding='UTF-8' ?>
  3. <!DOCTYPE html>
  4. <html xmlns="http://www.w3.org/1999/xhtml"
  5. xmlns:h="http://java.sun.com/jsf/html"
  6. xmlns:f="http://java.sun.com/jsf/core"
  7. xmlns:ui="http://java.sun.com/jsf/facelets">
  8. <ui:composition template="/templates/masterLayout.xhtml">
  9. <ui:define name="content">
  10. <f:metadata>
  11. <f:viewParam name="lineid" value="#{lineViewBean.lineid}" required="true" requiredMessage="#{msgs.lblNoLineInfo}">
  12. <f:validateLongRange minimum="1"/>
  13. </f:viewParam>
  14. <f:event type="preRenderView" listener="#{lineViewBean.loadLine}"/>
  15. </f:metadata>
  16.  
  17. <h3><h:outputText id="headerText" value="#{lineViewBean.line.name} Details"/></h3>
  18.  
  19. <h:form id="lineDetails">
  20. <h:messages id="msgs"/>
  21. <h:inputText id="lineName" value="#{lineViewBean.line.name}">
  22. <f:ajax event="keyup" listener="updateListener" render="@this msgs" />
  23. </h:inputText>
  24. <h:message for="lineName" />
  25. <h:commandButton id="btnSave" action="#{lineViewBean.update}" value="#{msgs.btnSave}"/>
  26. <h:commandButton action="#{lineViewBean.delete}" value="#{msgs.btnDelete}" />
  27. </h:form>
  28. <h:link outcome="listLines" value="#{msgs.lnkBrowseCatalog}" />
  29. </ui:define>
  30. </ui:composition>
  31. </html>
  32.  
  33.  
  34. //backingbean
  35. package com.tf.web;
  36.  
  37. import com.tf.facade.LineMaintainFacade;
  38. import com.tf.model.Line;
  39. import java.io.Serializable;
  40. import javax.ejb.EJB;
  41. import javax.faces.bean.ViewScoped;
  42. import javax.faces.bean.ManagedBean;
  43.  
  44. @ManagedBean(name = "lineViewBean")
  45. @ViewScoped
  46. public class LineViewViewBackingBean implements Serializable {
  47.  
  48. @EJB
  49. private LineMaintainFacade lineService;
  50.  
  51. private Line line;
  52. private Long lineId;
  53.  
  54. public Long getLineid() {
  55. return lineId;
  56. }
  57.  
  58. public void setLineid(Long lineid) {
  59. this.lineId = lineid;
  60. }
  61.  
  62. public void loadLine() {
  63. if (line == null) {
  64. line = lineService.getLine(lineId);
  65. }
  66. }
  67.  
  68. public Line getLine() {
  69. return line;
  70. }
  71.  
  72. public void setLine(Line line) {
  73. this.line = line;
  74. }
  75.  
  76. public void updateListener (AjaxBehaviorEvent event){
  77. line = lineService.update(line);
  78. }
  79.  
  80. public String update() {
  81. line = lineService.update(line);
  82. return "update";
  83. }
  84. }
RAW Paste Data