📄 index.xtp
字号:
<document> <header> <title>JSF with WebBeans</title> <description> <p>WebBeans (JSR-299) gives JSF a solid foundation forits component model, based on WebBeans' typesafe IoC capabilitiesand annotation-based discovery. </p> </description> <type>tutorial</type> <tutorial-startpage>test.jsf</tutorial-startpage> </header><body><localtoc/><s1 title="Files in this tutorial"><deftable><tr> <th>File</th> <th>Description</th></tr><tr> <td><viewfile-link file="test.jsp"/></td> <td>JSP to create the JSF component tree.</td></tr><tr> <td><viewfile-link file="WEB-INF/classes/example/Calculator.java"/></td> <td>Calculator model WebBean, taking the input and calculating the result.</td></tr><tr> <td><viewfile-link file="WEB-INF/resin-web.xml"/></td> <td>Configures FacesServlet.</td></tr><tr> <td><viewfile-link file="WEB-INF/classes/META-INF/web-beans.xml"/></td> <td>WebBeans configuration file.</td></tr></deftable></s1><s1 title="Overview"><p>WebBeans works together with JSF to provide a solid component-basisfor the data model of a JSF application.</p><p>With WebBeans, data components can be created by marking them witha @Component annotation, reducing the amount of configuration XML toa minimum. In this example, we only need XML to define the FacesServlet,and a marker web-beans.xml to direct WebBeans to search forcomponent classes.</p><p>The data components automatically populate the JSF EL (expression language),so they are automatically available to the JSF application.</p><p>This example creates a simple calculator which adds two numbers together.The <code>Calculator</code> model receives the user data andproduces the results. A trivial JSP page creates the JSF UIcomponent tree.</p></s1><s1 title="Model Component"><p>The data model is the heart of the JSF application. In this case,a trivial calculator.</p><p>The <code>@Component</code> marks <code>example.Calculator</code> asa WebBeans component. When WebBeans scans the classes, it will discover<code>Calculator</code>, introspect it, and automatically register thecalculator in the WebBeans directory. Once it's registered, any otherWebBeans component, or JSP/JSF EL, or PHP file or servlet or EJB canuse the component.</p><p>The <code>Calculator</code> component has no XML configuration at all,since there's nothing to configure. For other applications, some of thecomponent beans will want configuration to set properties, which willoccur in something like the resin-web.xml file.</p><example title="Calculator.java">package example;import javax.webbeans.*;@Component@RequestScoped@Named("calc") public class Calculator { private int _a; private int _b; public int getA() { return _a; } public void setA(int a) { _a = a; } public int getB() { return _b; } public void setB(int b) { _b = b; } public int getSum() { return _a + _b; }}</example><p>The <code>@RequestScoped</code> annotation tells WebBeans to storethe bean in the servlet request scope. Each request will use its owninstance of the calculator. If the scope was <code>@SessionScoped</code>,the same <code>Calculator</code> would be used for the entire session.If it was <code>@ConversationScoped</code> it would be used for theJSF page.</p><p>The optional <code>@Named</code> annotation gives an alternate namefor the calculator. If there is no <code>@Named</code>, WebBeans willuse the class name, e.g. "calculator" in this case.</p><p>WebBeans components can also be injected with other WebBeans, orDataSources, JPA EntityManager or EntityManagerFactoryor JMS Queues, and they can also use the <code>@PostConstruct</code>and <code>@PreDestroy</code> lifecycle annotations. Method interceptionand event listening are also possible.</p></s1><s1 title="JSF/JSP: Building the Component Tree"><p>JSF is designed around a UI component tree model. The JSP codebuilds the JSF component tree, hands it back to JSF, and then JSF willdisplay the component tree based on its current rendering configuration.</p><ul><li><f:view> is a wrapper tag around all the JSF component tree.</li><li><h:messages> displays any error messages, like typing a string tothe number fields.</li><li><h:form> creates a HTML <form></li><li><h:inputText> creates a HTML <input> tag, using the<code>Calculator</code> methods <code>getA()</code> and <code>setA()</code>to receive the form values.</li><li><h:outputText> creates a HTML <span> tag, with the textvalue generated by the <code>Calculator</code> <code>getSum()</code> method.</li><li><h:commandButton> creates a HTML <input type="submit"> tag.</li></ul><example title="test.jsp"><%@ taglib prefix="f" uri="http://java.sun.com/jsf/core" %><%@ taglib prefix="h" uri="http://java.sun.com/jsf/html" %><f:view> <h:messages/> <h:form> <h:inputText value="#{calc.a}" size="4"/> + <h:inputText value="#{calc.b}" size="4"/> = <h:outputText value="#{calc.sum}" style="color:red"/> <br> <h:commandButton value="Add"/> </h:form></f:view></example><p>The JSF expression language expressions <code>#{calc.a}</code> and<code>#{calc.b}</code> are used in two phases of JSF. When displaying,JSF will lookup the <code>Calculator</code> with "calc", and callits <code>getA()</code> method. When processing the form, JSF willlookup the <code>Calculator</code> and call the <code>setA()</code>method to assign the new value.</p></s1><s1 title="Housekeeping: the resin-web.xml and web-beans.xml"><p>The housekeeping overhead is a minimum when using WebBeans. In thisexample we just need two pieces of XML configuration:</p><ol><li>Configuring the JSF servlet in the web.xml</li><li>Marking a classpath root with a web-beans.xml</li></ol><p>WebBeans will scan classes directories and jars if they containa <code>META-INF/web-beans.xml</code> file, so many applications willjust use <code>web-beans.xml</code> as a marker file with no content.Others applications will want to configure the WebBeans components usingthe <code>web-beans.xml</code> or may put that configuration in the<code>resin-web.xml</code>.</p><example title="WEB-INF/resin-web.xml"><web-app xmlns="http://caucho.com/ns/resin"> <servlet-mapping url-pattern="*.jsf" servlet-class="javax.faces.webapp.FacesServlet"/></web-app></example><example title="META-INF/web-beans.xml"><web-beans xmlns="http://caucho.com/ns/resin"> <!-- - The web-beans.xml marks a class root for WebBeans to search for - @Component beans. Since the example doesn't need to override any - defaults, there's no additional configuration necessary. --></web-beans></example></s1><s1 title="Completing the Application"><p>A more complete application would likely the IoC injection capabilitiesof WebBeans. For example:</p><ul><li>Use Java Persistence by injecting a <code>@In EntityManager</code>to a model field.</li><li>Injecting a WebBeans singleton service with <code>@In</code>, definedby a <bean> configuration in the resin.conf (assuming it needs configuration.</li><li>Using JDBC directly with <code>@In DataSource</code> or <code>@Named("jdbc/test") DataSource.</code>.</li><li>Using EJB stateless or stateful session beans as services.</li></ul></s1></body></document>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -