wordpress 门户网站,趣天跨境电商官网,武进网站建设要多少钱,装修贷ejb jsf jpa视图–创建和JSF设置 本教程是第1部分的继续。 让我们创建一个新的Dynamic Web Project 。 如下图所示创建它#xff1a; 请注意#xff1a;在某些时候#xff0c;Eclipse会询问您是否要添加JSF功能#xff08;自动完成#xff09;#xff0c;然后启用它。 … ejb jsf jpa 视图–创建和JSF设置 本教程是第1部分的继续。 让我们创建一个新的Dynamic Web Project 。 如下图所示创建它 请注意在某些时候Eclipse会询问您是否要添加JSF功能自动完成然后启用它。 就像下面的屏幕一样 创建项目后让我们编辑“ web.xml”文件 它应具有与以下相同的代码 ?xml version1.0 encodingUTF-8?
web-app xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlnshttp://java.sun.com/xml/ns/javaee xmlns:webhttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsdxsi:schemaLocationhttp://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsdidWebApp_ID version3.0display-nameCrudJSF/display-namewelcome-file-listwelcome-filepages/protected/user/listAllDogs.xhtml/welcome-file/welcome-file-listservletservlet-nameFaces Servlet/servlet-nameservlet-classjavax.faces.webapp.FacesServlet/servlet-classload-on-startup1/load-on-startup/servletservlet-mappingservlet-nameFaces Servlet/servlet-nameurl-pattern/faces/*/url-patternurl-pattern*.jsf/url-patternurl-pattern*.xhtml/url-pattern/servlet-mapping!-- Protected area definition --security-constraintweb-resource-collectionweb-resource-nameRestricted Area - ADMIN Only/web-resource-nameurl-pattern/pages/protected/admin/*/url-pattern/web-resource-collectionauth-constraintrole-nameADMIN/role-name/auth-constraint/security-constraintsecurity-constraintweb-resource-collectionweb-resource-nameRestricted Area - USER and ADMIN/web-resource-nameurl-pattern/pages/protected/user/*/url-pattern/web-resource-collectionauth-constraintrole-nameUSER/role-namerole-nameADMIN/role-name/auth-constraint/security-constraint!-- Login page --login-configauth-methodFORM/auth-methodform-login-configform-login-page/pages/public/login.xhtml/form-login-pageform-error-page/pages/public/loginError.xhtml/form-error-page/form-login-config/login-config!-- System roles --security-rolerole-nameADMIN/role-name/security-rolesecurity-rolerole-nameUSER/role-name/security-role
/web-app 如果出现警告/错误您不必担心 我们稍后会解决。 注意我已经添加了我们需要的所有JAAS代码如果您想获得有关这些JAAS配置的详细信息可以在这里进行检查 使用JAAS和JSF的用户登录验证 。 根据JAAS的配置普通用户USER角色将只看到用户文件夹中的文件而这些文件只会是记录在我们数据库中的狗的列表。 ADMIN将能够执行所有CRUD操作因为所有页面都位于admins文件夹内。 我们的“ faces-config.xml”应具有以下代码 ?xml version1.0 encodingUTF-8?faces-configxmlnshttp://java.sun.com/xml/ns/javaeexmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsdversion2.0navigation-rulenavigation-casefrom-outcomelogout/from-outcometo-view-id/pages/protected/user/listAllDogs.xhtml/to-view-idredirect//navigation-case/navigation-rulenavigation-rulenavigation-casefrom-outcomelistAllDogs/from-outcometo-view-id/pages/protected/user/listAllDogs.xhtml/to-view-id/navigation-case/navigation-rulenavigation-rulenavigation-casefrom-outcomecreateDog/from-outcometo-view-id/pages/protected/admin/createDog.xhtml/to-view-idredirect//navigation-case/navigation-rulenavigation-rulenavigation-casefrom-outcomeupdateDog/from-outcometo-view-id/pages/protected/admin/updateDog.xhtml/to-view-id/navigation-case/navigation-rulenavigation-rulenavigation-casefrom-outcomedeleteDog/from-outcometo-view-id/pages/protected/admin/deleteDog.xhtml/to-view-id/navigation-case/navigation-ruleapplicationresource-bundlebase-namemessages/base-namevarmsgs/var/resource-bundle/application/faces-config 注意对于某些操作我使用了重定向操作。 通过此操作我们将更新浏览器URL栏中的请求链接URL更新后JAAS将拒绝对非法用户的访问。 我们还有一个文件其中包含我们系统的所有消息。 您会注意到我们页面中显示的所有文本都在此文件中在src文件夹中创建一个名为“ messages.properties”的文件 #Dog
dogDog
dogNameName
dogWeightWeight#Dog messages
dogCreateHeaderCreate a new Dog
dogUpdateHeaderUpdate the Dog
dogDeleteHeaderDelete this Dog
dogNameRequiredThe dog needs a name.
dogWeightRequiredThe dog needs a weight.#Actions
updateUpdate
createCreate
deleteDelete
cancelCancel#Login
loginHelloHello
loginErrorMessageCould not login. Check you UserName/Password
loginUserNameUsername
loginPasswordPassword
logoutLog 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 ListDog 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则可以使用如下注入EJBmappedName “ DogFacadeImp / local”。 注意将向我们的系统用户显示一条消息。 我们会对外观中执行的每个动作进行尝试/捕获如果发生某些错误我们将向用户发送错误消息。 正确的操作是验证ManagedBean和Façade中的数据。 这些验证的CPU成本较低。 如果您使用的是JBoss 4.2则需要执行JNDI查找如代码波纹管就像本文前面所说的那样使用LocalBinding批注。 像这样注释您的班级 Stateless
LocalBinding(jndiBindingMyBean)
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这就是为什么您会在我的所有页面中看到hinputHidden的原因。 我认为用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//ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
html xmlnshttp://www.w3.org/1999/xhtmlxmlns:fhttp://java.sun.com/jsf/corexmlns:hhttp://java.sun.com/jsf/htmlxmlns:uihttp://java.sun.com/jsf/facelets
h:headh:outputStylesheet librarycss namemain.css /
/h:head
h:bodypLogin to access secure pages:/pform methodpost actionj_security_checkh:messages layouttable errorStylebackground: #AFEEEE;infoStylebackground: #AFEEEE; globalOnlytrue /h:panelGrid columns2h:outputLabel valueUsername: /input typetext idj_username namej_username /h:outputLabel valuePassword: /input typepassword idj_password namej_password /h:outputText value /h:panelGrid columns1input typesubmit namesubmit valueLogin //h:panelGrid/h:panelGridbr //form
/h:body
/html 注意我们如何导入css就像它是一个库一样。 您在form标签中看到的动作指向我们一个未知的动作但是JAAS负责管理该动作。 /WebContent/pages/public/loginError.xhtml !DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
html xmlnshttp://www.w3.org/1999/xhtmlxmlns:fhttp://java.sun.com/jsf/corexmlns:hhttp://java.sun.com/jsf/htmlxmlns:uihttp://java.sun.com/jsf/facelets
h:headh:outputStylesheet librarycss namemain.css /
/h:head
h:body#{msgs.loginErrorMessage}
/h:body
/html /WebContent/pages/protected/user/listAllDogs.xhtml !DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
html xmlnshttp://www.w3.org/1999/xhtmlxmlns:fhttp://java.sun.com/jsf/corexmlns:hhttp://java.sun.com/jsf/htmlxmlns:uihttp://java.sun.com/jsf/facelets
h:headh:outputStylesheet librarycss namemain.css /
/h:head
h:bodyh:formh3#{msgs.loginHello}: #{userMB.user.name} || h:commandLink action#{userMB.logOut()} value#{msgs.logout} / /h3h:messages /h:dataTable value#{dogMB.allDogs} vardog styleClasstable headerClasstableColumnsHeader rowClassestableFirstLine,tableNextLine h:columnf:facet nameheader#{msgs.dogName}/f:facet#{dog.name}/h:columnh:columnf:facet nameheader#{msgs.dogWeight}/f:facet#{dog.weight}/h:columnh:columnh:panelGrid columns2!-- 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:commandButtonh: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 actioncreateDog value#{msgs.create} #{msgs.dog} //h:form
/h:body
/html 关于上面的代码 永远记得用hform标记包装代码。 如果没有hformhhead和hbody有些框架例如Primefaces将无法工作。 我们使用UserMB来显示用户名并注销我们的用户。 hmessages /标记将显示DogMB发送的消息。 请注意第33行的ID被隐藏。 如果您使用RequestScope而不是SessionScope则这是一个必需的值。 我宁愿使用RequestScope也不使用SessionScope因为您的服务器内存中的数据更少。 请注意按钮的呈现方式为“{userMB.userAdmin}”以指示仅ADMIN角色有权访问删除/更新。 我通过标签“ fsetPropertyActionListener ”将选定的狗传递给我的MB。 “创建”按钮没有渲染选项。 如果普通用户尝试访问页面它只是向您显示。 /WebContent/pages/protected/admin/createDog.xhtml !DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
html xmlnshttp://www.w3.org/1999/xhtmlxmlns:fhttp://java.sun.com/jsf/corexmlns:hhttp://java.sun.com/jsf/htmlxmlns:uihttp://java.sun.com/jsf/facelets
h:headh:outputStylesheet librarycss namemain.css /
/h:head
h:bodyh:formh:messages/h3${msgs.dogCreateHeader}/h3h:panelGrid columns2 styleClasspanelGrid rowClassespanelFirstLine,panelNextLine h:outputLabel fordogName value#{msgs.dogName} /h:inputText iddogName value#{dogMB.dog.name} requiredtrue requiredMessage#{msgs.dogNameRequired} /h:outputLabel fordogWeight value#{msgs.dogWeight} /h:inputText iddogWeight value#{dogMB.dog.weight} requiredtrue requiredMessage#{msgs.dogWeightRequired} f:convertNumber //h:inputText/h:panelGridh:panelGrid columns2h:commandButton action#{dogMB.createDogEnd()} value#{msgs.create} /h:commandButton action#{dogMB.listAllDogs()} value#{msgs.cancel} immediatetrue //h:panelGridbr//h:form
/h:body
/html 关于上面的代码 字段名称和权重是必填字段如果将其保留为空则会显示一条错误消息。 取消按钮需要选项即时“ true” 使用此选项JSF将不会验证任何字段。 /WebContent/pages/protected/admin/deleteDog.xhtml !DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
html xmlnshttp://www.w3.org/1999/xhtmlxmlns:fhttp://java.sun.com/jsf/corexmlns:hhttp://java.sun.com/jsf/htmlxmlns:uihttp://java.sun.com/jsf/facelets
h:headh:outputStylesheet librarycss namemain.css /
/h:head
h:bodyh:formh:messages/h3#{msgs.dogDeleteHeader}: #{dogMB.dog.name}?/h3h:inputHidden value#{dogMB.dog.id} /h:panelGrid columns2h:commandButton action#{dogMB.deleteDogEnd()} value#{msgs.delete} /h:commandButton action#{dogMB.listAllDogs()} value#{msgs.cancel} immediatetrue //h:panelGridbr//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//ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
html xmlnshttp://www.w3.org/1999/xhtmlxmlns:fhttp://java.sun.com/jsf/corexmlns:hhttp://java.sun.com/jsf/htmlxmlns:uihttp://java.sun.com/jsf/facelets
h:headh:outputStylesheet librarycss namemain.css /
/h:head
h:bodyh:formh:messages/h3#{msgs.dogUpdateHeader}: #{dogMB.dog.name}/h3h:inputHidden value#{dogMB.dog.id} /h:panelGrid columns2 styleClasspanelGrid rowClassespanelFirstLine,panelNextLine h:outputLabel fordogName value#{msgs.dogName} /h:inputText iddogName value#{dogMB.dog.name} requiredtrue requiredMessage#{msgs.dogNameRequired} /h:outputLabel fordogWeight value#{msgs.dogWeight} /h:inputText iddogWeight value#{dogMB.dog.weight} requiredtrue requiredMessage#{msgs.dogWeightRequired} f:convertNumber //h:inputText/h:panelGridh:panelGrid columns2h:commandButton action#{dogMB.updateDogEnd()} value#{msgs.update} /h:commandButton action#{dogMB.listAllDogs()} value#{msgs.cancel} immediatetrue //h:panelGridbr//h:form
/h:body
/html 关于上面的代码 请注意在第15行中该ID是隐藏的。 如果您使用RequestScope而不是SessionScope则这是一个必需的值。 我宁愿使用RequestScope也不使用SessionScope因为您的服务器内存中的数据更少。 字段名称和权重是必填字段如果将其保留为空则会显示一条错误消息。 取消按钮需要选项即时“ true” 使用此选项JSF将不会验证任何字段。 查看– JBoss 7 JAAS配置 现在我们只需要完成几个步骤即可完成我们的软件最后。 我们需要编辑JBoss配置并添加我们的JAAS配置。 再次打开文件“ YOUR_JBOSS / standalone / configuration / standalone.xml ”然后搜索密钥“ security-domains”。 添加下面的代码在这篇文章中我将演示如何为JBoss 6 – 使用JAAS和JSF进行用户登录验证 进行设置 subsystem xmlnsurn:jboss:domain:security:1.0security-domains!-- add me: begin --security-domain nameCrudJSFRealm cache-typedefaultauthenticationlogin-module codeorg.jboss.security.auth.spi.DatabaseServerLoginModule flagrequiredmodule-option namedsJndiName valueCrudDS/module-option nameprincipalsQuery valueselect password from users where email? /module-option namerolesQuery valueselect 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// localhost8080 / 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.htmlejb jsf jpa