javascript
完整的WebApplication JSF EJB JPA JAAS –第2部分
本教程是第1部分的繼續。
讓我們創建一個新的Dynamic Web Project 。 如下圖所示創建它:
注意:在某些時候,Eclipse會詢問您是否要添加JSF功能(自動完成),然后啟用它。 就像下面的屏幕一樣:
創建項目后,讓我們編輯“ web.xml”文件; 它應該具有與以下相同的代碼:
<?xml version='1.0' encoding='UTF-8'?> <web-app xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'xmlns='http://java.sun.com/xml/ns/javaee' xmlns:web='http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd'xsi:schemaLocation='http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd'id='WebApp_ID' version='3.0'><display-name>CrudJSF</display-name><welcome-file-list><welcome-file>pages/protected/user/listAllDogs.xhtml</welcome-file></welcome-file-list><servlet><servlet-name>Faces Servlet</servlet-name><servlet-class>javax.faces.webapp.FacesServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>Faces Servlet</servlet-name><url-pattern>/faces/*</url-pattern><url-pattern>*.jsf</url-pattern><url-pattern>*.xhtml</url-pattern></servlet-mapping><!-- Protected area definition --><security-constraint><web-resource-collection><web-resource-name>Restricted Area - ADMIN Only</web-resource-name><url-pattern>/pages/protected/admin/*</url-pattern></web-resource-collection><auth-constraint><role-name>ADMIN</role-name></auth-constraint></security-constraint><security-constraint><web-resource-collection><web-resource-name>Restricted Area - USER and ADMIN</web-resource-name><url-pattern>/pages/protected/user/*</url-pattern></web-resource-collection><auth-constraint><role-name>USER</role-name><role-name>ADMIN</role-name></auth-constraint></security-constraint><!-- Login page --><login-config><auth-method>FORM</auth-method><form-login-config><form-login-page>/pages/public/login.xhtml</form-login-page><form-error-page>/pages/public/loginError.xhtml</form-error-page></form-login-config></login-config><!-- System roles --><security-role><role-name>ADMIN</role-name></security-role><security-role><role-name>USER</role-name></security-role> </web-app>您不必擔心是否會出現一些警告/錯誤。 我們稍后會解決。 請注意,我已經添加了我們需要的所有JAAS代碼(如果您想獲得有關這些JAAS配置的詳細信息,可以在此處進行檢查: 使用JAAS和JSF的用戶登錄驗證 )。
根據JAAS的配置,普通用戶(USER角色)將只看到用戶文件夾中的文件,而這些文件僅僅是我們數據庫中記錄的狗的列表; ADMIN將能夠執行所有CRUD操作,因為所有頁面都位于admins文件夾內。
我們的“ faces-config.xml”應具有以下代碼:
<?xml version='1.0' encoding='UTF-8'?><faces-configxmlns='http://java.sun.com/xml/ns/javaee'xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'xsi:schemaLocation='http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd'version='2.0'><navigation-rule><navigation-case><from-outcome>logout</from-outcome><to-view-id>/pages/protected/user/listAllDogs.xhtml</to-view-id><redirect/></navigation-case></navigation-rule><navigation-rule><navigation-case><from-outcome>listAllDogs</from-outcome><to-view-id>/pages/protected/user/listAllDogs.xhtml</to-view-id></navigation-case></navigation-rule><navigation-rule><navigation-case><from-outcome>createDog</from-outcome><to-view-id>/pages/protected/admin/createDog.xhtml</to-view-id><redirect/></navigation-case></navigation-rule><navigation-rule><navigation-case><from-outcome>updateDog</from-outcome><to-view-id>/pages/protected/admin/updateDog.xhtml</to-view-id></navigation-case></navigation-rule><navigation-rule><navigation-case><from-outcome>deleteDog</from-outcome><to-view-id>/pages/protected/admin/deleteDog.xhtml</to-view-id></navigation-case></navigation-rule><application><resource-bundle><base-name>messages</base-name><var>msgs</var></resource-bundle></application></faces-config>注意,對于某些操作,我使用了重定向操作。 通過此操作,我們將更新瀏覽器URL欄中的請求鏈接,URL更新后,JAAS將拒絕對非法用戶的訪問。
我們還有一個文件,其中包含我們系統的所有消息。 您會注意到,頁面中顯示的所有文本都在此文件中(在src文件夾中創建一個名為“ messages.properties”的文件):
#Dog dog=Dog dogName=Name dogWeight=Weight#Dog messages dogCreateHeader=Create a new Dog dogUpdateHeader=Update the Dog dogDeleteHeader=Delete this Dog dogNameRequired=The dog needs a name. dogWeightRequired=The dog needs a weight.#Actions update=Update create=Create delete=Delete cancel=Cancel#Login loginHello=Hello loginErrorMessage=Could not login. Check you UserName/Password loginUserName=Username loginPassword=Password logout=Log Out視圖–創建和JSF設置
現在讓我們創建ManagedBeans。
首先,我們需要將EJB添加到Web項目中。 用鼠標右鍵單擊JSF項目>屬性:
Java構建路徑>項目>添加>檢查CrudEJB>確定
首先,讓我們創建DogMB:
package com.mb;import java.util.List;import javax.ejb.EJB; import javax.ejb.EJBException; import javax.faces.application.FacesMessage; import javax.faces.bean.ManagedBean; import javax.faces.bean.RequestScoped; import javax.faces.context.FacesContext;import com.facade.DogFacade; import com.model.Dog;@ManagedBean @RequestScoped public class DogMB {@EJBprivate DogFacade dogFacade;private static final String CREATE_DOG = 'createDog';private static final String DELETE_DOG = 'deleteDog';private static final String UPDATE_DOG = 'updateDog';private static final String LIST_ALL_DOGS = 'listAllDogs';private static final String STAY_IN_THE_SAME_PAGE = null;private Dog dog;public Dog getDog() {if(dog == null){dog = new Dog();}return dog;}public void setDog(Dog dog) {this.dog = dog;}public List<Dog> getAllDogs() {return dogFacade.findAll();}public String updateDogStart(){return UPDATE_DOG;}public String updateDogEnd(){try {dogFacade.update(dog);} catch (EJBException e) {sendErrorMessageToUser('Error. Check if the weight is above 0 or call the adm');return STAY_IN_THE_SAME_PAGE;}sendInfoMessageToUser('Operation Complete: Update');return LIST_ALL_DOGS;}public String deleteDogStart(){return DELETE_DOG;}public String deleteDogEnd(){try {dogFacade.delete(dog);} catch (EJBException e) {sendErrorMessageToUser('Error. Call the ADM');return STAY_IN_THE_SAME_PAGE;} sendInfoMessageToUser('Operation Complete: Delete');return LIST_ALL_DOGS;}public String createDogStart(){return CREATE_DOG;}public String createDogEnd(){try {dogFacade.save(dog);} catch (EJBException e) {sendErrorMessageToUser('Error. Check if the weight is above 0 or call the adm');return STAY_IN_THE_SAME_PAGE;} sendInfoMessageToUser('Operation Complete: Create');return LIST_ALL_DOGS;}public String listAllDogs(){return LIST_ALL_DOGS;}private void sendInfoMessageToUser(String message){FacesContext context = getContext();context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, message, message));}private void sendErrorMessageToUser(String message){FacesContext context = getContext();context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, message, message));}private FacesContext getContext() {FacesContext context = FacesContext.getCurrentInstance();return context;} }關于上面的代碼:
- 您將在faces-config.xml中找到所有導航。 您應該在頁面導航中使用常量或資源包; 與僅在方法中保留字符串相比,這種方法是一種更好的方法。
- 注意,我們僅使用@EJB將EJB注入MB內。 發生這種情況是因為我們正在使用同一個EAR中的所有東西。 JBoss 7使這種本地化變得容易。
- 如果注入不適用于JBoss 6(或者在EAR之外使用EJB jar),則可以使用如下注入:@EJB(mappedName =“ DogFacadeImp / local”)。
- 注意,將向我們的系統用戶顯示一條消息。 我們會對在Fa?ade中執行的每個動作進行嘗試/捕獲,如果發生某些錯誤,我們將向用戶發送錯誤消息。
- 正確的操作是驗證ManagedBean和Fa?ade中的數據。 這些驗證的CPU成本較低。
- 如果您正在使用JBoss 4.2,則需要像下面的代碼那樣進行JNDI查找(就像本文前面所說的那樣,使用LocalBinding批注)。 像這樣注釋您的班級: @Stateless @LocalBinding(jndiBinding='MyBean') public class MyBeanImp implements MyBean{@Overridepublic String hello() {return 'Value From EJB';} } // In your Servlet class you would lookup like the code bellow: public class Inject extends HttpServlet {private MyBean local;protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {try {InitialContext iniCtx = new InitialContext();local = (MyBean) iniCtx.lookup('MyBean');} catch (NamingException e) {e.printStackTrace();}System.out.println(local.hello());request.getRequestDispatcher('/finish.jsp').forward(request, response);}/*** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)*/protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {} }
現在,讓我們看看UserMB:
package com.mb;import javax.ejb.EJB; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; import javax.faces.context.ExternalContext; import javax.faces.context.FacesContext; import javax.servlet.http.HttpServletRequest;import com.facade.UserFacade; import com.model.User;@SessionScoped @ManagedBean public class UserMB {private User user;@EJBprivate UserFacade userFacade;public User getUser(){if(user == null){ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();String userEmail = context.getUserPrincipal().getName();user = userFacade.findUserByEmail(userEmail);}return user;}public boolean isUserAdmin(){return getRequest().isUserInRole('ADMIN');}public String logOut(){getRequest().getSession().invalidate();return 'logout';}private HttpServletRequest getRequest() {return (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();} }關于上面的代碼:
- 該MB僅用于存儲我們應用程序的會話用戶。 您將使用此MB顯示用戶名或有關應用程序用戶的任何其他操作。
- 注意,這是一個會話MB; 我們只檢查一次用戶是否為null,如果返回true,我們將轉到數據庫。 在這種情況下,我們將只獲得一次數據庫保存性能。
- 如果@EJB注入引發異常,請檢查上面DogMB中給出的技巧。
查看–頁面
下面是頁面,css及其各自的路徑:
不要介意上面圖片中顯示的詢問圖標或任何其他圖標類型。 這些是指向我的代碼的版本控制圖標。 始終保存您的代碼。
我在ManagedBean中使用RequestScope,這就是為什么您會在所有頁面中看到h:inputHidden的原因。 我認為用RequestScope MB重復此字段是一種更好的方法,因為您將擁有更多的可用內存,而不是使用SessionScope MB。
/WebContent/resources/css/main.css
.table {border-collapse: collapse; }.tableColumnsHeader {text-align: center;background: none repeat scroll 0 0 #E5E5E5;border-bottom: 1px solid #BBBBBB;padding: 16px; }.tableFirstLine {text-align: center;background: none repeat scroll 0 0 #F9F9F9;border-top: 1px solid #BBBBBB; }.tableNextLine {text-align: center;background: none repeat scroll 0 0 #FFFFFFF;border-top: 1px solid #BBBBBB; }.panelGrid {border: 1px solid; }.panelFirstLine {text-align: center;border-top: 1px solid #BBBBBB; }.panelNextLine {text-align: center;border-top: 1px solid #BBBBBB; }/WebContent/pages/public/login.xhtml
<!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:f='http://java.sun.com/jsf/core'xmlns:h='http://java.sun.com/jsf/html'xmlns:ui='http://java.sun.com/jsf/facelets'> <h:head><h:outputStylesheet library='css' name='main.css' /> </h:head> <h:body><p>Login to access secure pages:</p><form method='post' action='j_security_check'><h:messages layout='table' errorStyle='background: #AFEEEE;'infoStyle='background: #AFEEEE;' globalOnly='true' /><h:panelGrid columns='2'><h:outputLabel value='Username: ' /><input type='text' id='j_username' name='j_username' /><h:outputLabel value='Password: ' /><input type='password' id='j_password' name='j_password' /><h:outputText value='' /><h:panelGrid columns='1'><input type='submit' name='submit' value='Login' /></h:panelGrid></h:panelGrid><br /></form> </h:body> </html> 注意我們如何像導入庫一樣導入css。 您在form標簽中看到的動作指向我們一個未知的動作,但是JAAS負責管理該動作。
/WebContent/pages/public/loginError.xhtml
/WebContent/pages/protected/user/listAllDogs.xhtml
<!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:f='http://java.sun.com/jsf/core'xmlns:h='http://java.sun.com/jsf/html'xmlns:ui='http://java.sun.com/jsf/facelets'> <h:head><h:outputStylesheet library='css' name='main.css' /> </h:head> <h:body><h:form><h3>#{msgs.loginHello}: #{userMB.user.name} || <h:commandLink action='#{userMB.logOut()}' value='#{msgs.logout}' /> </h3><h:messages /><h:dataTable value='#{dogMB.allDogs}' var='dog' styleClass='table' headerClass='tableColumnsHeader' rowClasses='tableFirstLine,tableNextLine' ><h:column><f:facet name='header'>#{msgs.dogName}</f:facet>#{dog.name}</h:column><h:column><f:facet name='header'>#{msgs.dogWeight}</f:facet>#{dog.weight}</h:column><h:column><h:panelGrid columns='2'><!-- Always save the id as hidden when you use a request scope MB --><h:inputHidden value='#{dog.id}' /><h:commandButton action='#{dogMB.updateDogStart()}' value='#{msgs.update}' rendered='#{userMB.userAdmin}' ><f:setPropertyActionListener target='#{dogMB.dog}' value='#{dog}' /></h:commandButton><h:commandButton action='#{dogMB.deleteDogStart()}' value='#{msgs.delete}' rendered='#{userMB.userAdmin}' ><f:setPropertyActionListener target='#{dogMB.dog}' value='#{dog}' /></h:commandButton></h:panelGrid></h:column></h:dataTable><!-- This button is displayed to the user, just to you see the error msg --><h:commandButton action='createDog' value='#{msgs.create} #{msgs.dog}' /></h:form> </h:body> </html>關于上面的代碼:
- 永遠記得用h:form標記包裝代碼。 如果沒有h:form,h:head和h:body,有些框架(例如Primefaces)將無法工作。
- 我們使用UserMB來顯示用戶名并注銷我們的用戶。
- <h:messages />標記將顯示DogMB發送的消息。
- 請注意,第33行的ID被隱藏。 如果您使用RequestScope而不是SessionScope,則這是一個必需的值。 我寧愿使用RequestScope也不使用SessionScope,因為服務器內存中的數據更少。
- 請注意,按鈕的呈現方式為=“#{userMB.userAdmin}”,以指示僅ADMIN角色有權訪問刪除/更新。
- 我通過標簽“ f:setPropertyActionListener ”將選定的狗傳遞給我的MB。
- “創建”按鈕沒有渲染選項。 如果普通用戶嘗試訪問頁面,它只是向您顯示。
/WebContent/pages/protected/admin/createDog.xhtml
<!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:f='http://java.sun.com/jsf/core'xmlns:h='http://java.sun.com/jsf/html'xmlns:ui='http://java.sun.com/jsf/facelets'> <h:head><h:outputStylesheet library='css' name='main.css' /> </h:head> <h:body><h:form><h:messages/><h3>${msgs.dogCreateHeader}</h3><h:panelGrid columns='2' styleClass='panelGrid' rowClasses='panelFirstLine,panelNextLine' ><h:outputLabel for='dogName' value='#{msgs.dogName}' /><h:inputText id='dogName' value='#{dogMB.dog.name}' required='true' requiredMessage='#{msgs.dogNameRequired}' /><h:outputLabel for='dogWeight' value='#{msgs.dogWeight}' /><h:inputText id='dogWeight' value='#{dogMB.dog.weight}' required='true' requiredMessage='#{msgs.dogWeightRequired}' ><f:convertNumber /></h:inputText></h:panelGrid><h:panelGrid columns='2'><h:commandButton action='#{dogMB.createDogEnd()}' value='#{msgs.create}' /><h:commandButton action='#{dogMB.listAllDogs()}' value='#{msgs.cancel}' immediate='true' /></h:panelGrid><br/></h:form> </h:body> </html>關于上面的代碼:
- 字段名稱和權重是必填字段,如果將其保留為空,則會顯示一條錯誤消息。
- 取消按鈕需要選項Instant =“ true”; 使用此選項,JSF將不會驗證任何字段。
/WebContent/pages/protected/admin/deleteDog.xhtml
<!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:f='http://java.sun.com/jsf/core'xmlns:h='http://java.sun.com/jsf/html'xmlns:ui='http://java.sun.com/jsf/facelets'> <h:head><h:outputStylesheet library='css' name='main.css' /> </h:head> <h:body><h:form><h:messages/><h3>#{msgs.dogDeleteHeader}: #{dogMB.dog.name}?</h3><h:inputHidden value='#{dogMB.dog.id}' /><h:panelGrid columns='2'><h:commandButton action='#{dogMB.deleteDogEnd()}' value='#{msgs.delete}' /><h:commandButton action='#{dogMB.listAllDogs()}' value='#{msgs.cancel}' immediate='true' /></h:panelGrid><br/></h:form> </h:body> </html>請注意,在第15行中,該ID是隱藏的。 如果您使用RequestScope而不是SessionScope,則這是一個必需的值。 我寧愿使用RequestScope也不使用SessionScope,因為服務器內存中的數據更少。
/WebContent/pages/protected/admin/updateDog.xhtm l
<!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:f='http://java.sun.com/jsf/core'xmlns:h='http://java.sun.com/jsf/html'xmlns:ui='http://java.sun.com/jsf/facelets'> <h:head><h:outputStylesheet library='css' name='main.css' /> </h:head> <h:body><h:form><h:messages/><h3>#{msgs.dogUpdateHeader}: #{dogMB.dog.name}</h3><h:inputHidden value='#{dogMB.dog.id}' /><h:panelGrid columns='2' styleClass='panelGrid' rowClasses='panelFirstLine,panelNextLine' ><h:outputLabel for='dogName' value='#{msgs.dogName}' /><h:inputText id='dogName' value='#{dogMB.dog.name}' required='true' requiredMessage='#{msgs.dogNameRequired}' /><h:outputLabel for='dogWeight' value='#{msgs.dogWeight}' /><h:inputText id='dogWeight' value='#{dogMB.dog.weight}' required='true' requiredMessage='#{msgs.dogWeightRequired}' ><f:convertNumber /></h:inputText></h:panelGrid><h:panelGrid columns='2'><h:commandButton action='#{dogMB.updateDogEnd()}' value='#{msgs.update}' /><h:commandButton action='#{dogMB.listAllDogs()}' value='#{msgs.cancel}' immediate='true' /></h:panelGrid><br/></h:form> </h:body> </html>關于上面的代碼:
- 請注意,在第15行中,該ID是隱藏的。 如果您使用RequestScope而不是SessionScope,則這是一個必需的值。 我寧愿使用RequestScope也不使用SessionScope,因為服務器內存中的數據更少。
- 字段名稱和權重是必填字段,如果將其保留為空,則會顯示一條錯誤消息。
- 取消按鈕需要選項Instant =“ true”; 使用此選項,JSF將不會驗證任何字段。
查看– JBoss 7 JAAS配置
現在,我們只需要完成一些步驟即可完成我們的軟件(最后!)。
我們需要編輯JBoss配置并添加我們的JAAS配置。
再次打開文件“ YOUR_JBOSS / standalone / configuration / standalone.xml ”,然后搜索密鑰:“ <security-domains>”。 添加下面的代碼(在這篇文章中,我演示如何為JBoss 6 – 使用JAAS和JSF進行用戶登錄驗證 )進行設置:
<subsystem xmlns='urn:jboss:domain:security:1.0'><security-domains><!-- add me: begin --><security-domain name='CrudJSFRealm' cache-type='default'><authentication><login-module code='org.jboss.security.auth.spi.DatabaseServerLoginModule' flag='required'><module-option name='dsJndiName' value='CrudDS'/><module-option name='principalsQuery' value='select password from users where email=?' /><module-option name='rolesQuery' value='select role, 'Roles' from users u where u.email=?' /></login-module></authentication></security-domain><!-- add me: end --><!-- Other data... --></security-domains> </subsystem>運行我們的應用程序
讓我們創建一個EAR來統一我們的項目。
文件>新建>其他> EnterpriseApplication項目
我們只需要在我們的JBoss中添加EAR。
讓我們運行我們的應用程序。 啟動JBoss并通過以下URL訪問我們的應用程序: http:// localhost:8080 / CrudJSF / 。
我使用簡單CSS編寫了頁面,以使理解更加容易。
以USER身份登錄,您將不會看到更新/刪除按鈕; 您只會看到我們留在此處的“創建”按鈕,只是為了查看非法訪問的例外情況。
看一下我們的頁面:
記錄為ADMIN:
以USER身份登錄:
今天就這些
要下載此帖子的源代碼, 請單擊此處 。
希望這篇文章對您有所幫助。
如果您有任何疑問或評論,請在下面將其發布。
再見。 \ o_
對我有幫助的鏈接:
http://7thursdays.wordpress.com/2008/03/18/dependency-injection-in-jboss-42-hold-your-excitement/
http://jan.zawodny.pl/blog/2011/07/jboss-7-postgresql-9
http://blog.xebia.com/2011/07/19/developing-a-jpa-application-on-jboss-as-7/
http://community.jboss.org/wiki/DataSourceConfigurationInAS7
http://stackoverflow.com/questions/286686/how-to-create-conditions-based-on-user-role-using-jsf-myfaces/
http://www.mkyong.com/jsf2/jsf-2-datatable-example/
參考: uaiHebert博客上來自我們的JCG合作伙伴 Hebert Coelho的完整WebApplication JSF EJB JPA JAAS 。
翻譯自: https://www.javacodegeeks.com/2012/06/full-webapplication-jsf-ejb-jpa-jaas_19.html
總結
以上是生活随笔為你收集整理的完整的WebApplication JSF EJB JPA JAAS –第2部分的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 抚州房地产信息备案网官网(抚州房地产信息
- 下一篇: (linux的系统时间)