日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

轻量级Web应用程序:PrimeFaces(JSF)+ Guice + MyBatis(第1部分)

發布時間:2023/12/3 javascript 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 轻量级Web应用程序:PrimeFaces(JSF)+ Guice + MyBatis(第1部分) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最近,我的朋友問我如何構建輕量級的Java Web應用程序。 許多Java Web開發人員會選擇Spring和Hibernate來構建傳統的Web應用程序。 但是,它可能不夠輕巧。 我建議他嘗試使用Guice和MyBatis構建應用程序框架。 盡管Spring比Guice功能更豐富,但我承認Guice更輕巧且易于使用。 MyBatis還是一個輕量級的SQL映射框架。 它可以很好地與Guice框架集成。

在這里,我將嘗試使用PrimeFaces,Guice和MyBatis建立一個簡單的Web應用程序。 我希望我的朋友可以學習如何做。

集成MyFaces和PrimeFaces很簡單。 只需從MyFaces網站和PrimeFaces網站獲取JARS文件。

對于MyFaces,只需將以下示例配置添加到您的web.xml中。

<display-name>TestGuice</display-name> <context-param><param-name>facelets.DEVELOPMENT</param-name><param-value>false</param-value></context-param><context-param><param-name>facelets.REFRESH_PERIOD</param-name><param-value>2</param-value></context-param><context-param><param-name>javax.faces.CONFIG_FILES</param-name><param-value>/WEB-INF/faces-config.xml</param-value></context-param><context-param><param-name>javax.faces.STATE_SAVING_METHOD</param-name><param-value>client</param-value></context-param><context-param><param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name><param-value>resources.application</param-value></context-param><context-param><param-name>org.apache.myfaces.ALLOW_JAVASCRIPT</param-name><param-value>true</param-value></context-param><context-param><param-name>org.apache.myfaces.AUTO_SCROLL</param-name><param-value>false</param-value></context-param><context-param><param-name>org.apache.myfaces.DETECT_JAVASCRIPT</param-name><param-value>false</param-value></context-param><context-param><param-name>org.apache.myfaces.ERROR_HANDLING</param-name><param-value>false</param-value></context-param><context-param><param-name>org.apache.myfaces.EXPRESSION_FACTORY</param-name><param-value>org.jboss.el.ExpressionFactoryImpl</param-value></context-param><context-param><param-name>org.apache.myfaces.PRETTY_HTML</param-name><param-value>false</param-value></context-param><welcome-file-list><welcome-file>index.do</welcome-file></welcome-file-list><servlet><servlet-name>Faces Servlet</servlet-name><servlet-class>org.apache.myfaces.webapp.MyFacesServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>Faces Servlet</servlet-name><url-pattern>*.do</url-pattern></servlet-mapping>

對于PrimeFaces,應該沒有配置。 如果要使用PrimeFaces主題,可以在web.xml中添加以下上下文參數。

<context-param><param-name>primefaces.THEME</param-name><param-value>glass-x</param-value></context-param>

Google Guice將用作IOC容器。 服務類和SQL映射器類的依賴注入通過Guice完成。 為了與JSF集成,我建議簡單地添加一個ServletContextListener來實現。

在您的web.xml中添加以下配置:

<listener><listener-class>org.borislam.GuiceContextListener</listener-class></listener>

在ServletContextListener內部,只需創建一個Guice Injector并將其放入ServletContext中:

public class GuiceContextListener implements ServletContextListener {public void contextDestroyed(ServletContextEvent servletContextEvent) {ServletContext servletContext = servletContextEvent.getServletContext();servletContext.removeAttribute(Injector.class.getName());}public void contextInitialized(ServletContextEvent servletContextEvent) {Injector injector = Guice.createInjector(new MyBatisModule() {@Overrideprotected void initialize() { //add singleton service classbind(SimpleService.class).to(SimpleServiceImpl.class).in(Singleton.class); });ServletContext servletContext = servletContextEvent.getServletContext();servletContext.setAttribute(Injector.class.getName(), injector);} }

在基礎支持bean中,通過PostConstruct方法獲取Guice注入器。 然后,Web應用程序中的每個后備bean都需要擴展此基本后備bean。

package org.borislam.view;import java.io.Serializable;import javax.annotation.PostConstruct; import javax.faces.context.FacesContext; import javax.servlet.ServletContext;import com.google.inject.Injector;public abstract class BasePageBean implements Serializable{private Injector injector;public BasePageBean() {}public Injector getInjector() {if(injector == null) {ServletContext servletContext = (ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext();injector = (Injector)servletContext.getAttribute(Injector.class.getName()); }return injector;}public void setInjector(Injector injector) {this.injector = injector;}@PostConstructpublic void init() {getInjector().injectMembers(this);} }

在第二部分中 ,我將繼續演示如何集成MyBatis和Guice。

參考: 輕量級Web應用程序框架:來自我們的JCG合作伙伴 Boris Lam的PrimeFaces(JSF)+ Guice + MyBatis(PART1) ,位于“ Programming Peaceally”博客中。

翻譯自: https://www.javacodegeeks.com/2013/01/lightweight-web-application-primefaces-jsf-guice-mybatis-part-1.html

總結

以上是生活随笔為你收集整理的轻量级Web应用程序:PrimeFaces(JSF)+ Guice + MyBatis(第1部分)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。