Javatwo2012 java frameworkcomparison

1,745 views

Published on

The javatwo ( Taiwan Java Develpers' Day ) to compare the hot java frameworks .

Published in: Technology, Education
  • Be the first to comment

Javatwo2012 java frameworkcomparison

  1. 1. SoftLeader Tech. Corp.李日貴 jini王文農 steven
  2. 2. Java 熱門 Framework 運用與比較
  3. 3. About us..• Demo codeshttp://code.google.com/p/javatwo2012-java-framework-comparison/• My facebook pagehttps://www.facebook.com/EnterpriseJava• Javaworld@Tw ( javaworld.com.tw )jiniatpqq
  4. 4. JavaEE Multi-Tiers architectureHttpServletRequest Web Business DAO DB Tier Tier TierHttpServletResponse
  5. 5. Lots of frameworks
  6. 6. Java Persistence ( DAO )
  7. 7. JPA 2
  8. 8. • 簡化 JDBC• Native SQL• 設定容易• XML or Annotation
  9. 9. Customer.javapublic class Customer { private Long id; private String name; private Integer age; // getters and setters}
  10. 10. Customer-mapper.xml<mappernamespace=“javatwo2012.dao.mybatis.Customer”> <select id=“getCustomerById”parameterType=“Long” resultType=“customer”> <!-- select SQL with #id --> </select></mapper>
  11. 11. mybatis-config.xml<configuration> <settings> … </settings> <mappers> <mapperresource=“javatwo2012/dao/mybatis/Customer-mapper.xml”/> </mappers></configuration>
  12. 12. CustomerDao.javaSqlSession session =sqlSessionFactory.openSession();try { Customer customer =session.selectOne(“javatwo2012.dao.mybatis.Customer.getCustomerById”, new Long(101));} finally { session.close();}
  13. 13. • 完整 ORM• 易於更換資料庫• 多種查詢機制• 支援多種 Cache• 多元的資料 Initialize
  14. 14. Customer.java@Entity@Table(name=“CUSTOMER”)public class Customer { @Id @GeneratedValue(strategy=GenerationType.Identity) @Column(name=“ID”) private Long id; // getters and setters}
  15. 15. hibernate-cfg.xml<hibernate-configuration> <session-factory> <property name=“dialect”>DB_Dialect</property> <mapping class=“javatwo2012.dao.hibernate.Customer”/> </seesion-factory></hibernate-configuration>
  16. 16. CustomerDao.javapublic Customer findById(Long id) { SessionFactory sessionFactory = newConfiguration().configure().buildSessionFactory(); Session session = sessionFactory.openSession(); Criteria crit = session.createCriteria(Customer.class); crit.add(Restrictions.idEq(id)); return (Customer) crit.uniqueResult();}
  17. 17. • JavaEE 標準 • 吸收了各家的優點JPA 2 • 多種查詢機制 • 支援多種 Cache • 多元的資料 Initialize • Transaction Control
  18. 18. Customer.java @Entity @Table(name=“CUSTOMER”) public class Customer {JPA 2 @Id @GeneratedValue(strategy=GenerationType.Identity) @Column(name=“ID”) private Long id; // getters and setters }
  19. 19. persistence.xml <persistence> <persistence-unit name=“javatwo2012demo” transaction- type=“RESOURCE_LOCAL” >JPA 2 <provider>org.hibernate.ejb.HibernatePersistence</provider> <properties>..</properties> </persistence-unit> </persistence>
  20. 20. CustomerDao.java public Customer findById(Long id) { EntityManagerFactory emFactory = Persistence.createEntityManagerFactory(“javatwo2012demo”);JPA 2 EntityManager manager = emFactory.createEntityManager(); return (Customer) manager.find(Customer.class, id); }
  21. 21. 項目 JPA 2 學習容易度 容易 複雜 複雜 查詢 API 少 多 多 SQL優化容易度 較簡單 複雜 複雜資料庫方言依賴程度 高 低 低 效能 佳 佳 依實作而定 可移植性 普通 佳 依實作而定 社群支援 多 多 多
  22. 22. Java Web ( MVC )
  23. 23. • Annotation Driven• AJAX inside• UnitTest• REST Integration• Multi-Languages• HTML5 + CSS3
  24. 24. • Action-Based • Component-Based JSF 2
  25. 25. web.xml<filter-class>org.apache.struts2.dispatcher.ng.StrutsPrepareAndExecuteFilter</filter-class>
  26. 26. struts.xml<struts> <package name=“javatwo2012” extends=“struts-default”> <action name=“hello” class=“…HelloAction”> <result>/hello.jsp</result> <result name=“input”>/hello.jsp</result> </action> </package></struts>
  27. 27. HelloAction.javapublic class HelloAction extends ActionSupport { private String message; public String getMessage() { return this.message; } public String execute() { return SUCCESS; }}
  28. 28. hello.jsp<%@ taglib prefix=“s” uri=“/sturts-tags”%><s:property value=“message”/>
  29. 29. AuthAction.javapublic class AuthAction extends ActionSupport { public String validate() { if( … ) { addFieldError(“account”, “Account can’t be empty”); } } public String execute() { …. }}
  30. 30. • 優點– 很多人用, 工作機會多– 豐富的 Taglibs– 很多擴充的 plugins• 缺點– struts.xml– 需要用 result 對應– Source-Codes 混亂
  31. 31. web.xml<filter-class>net.sourceforge.stripes.controller.StripesFilter</filter-class><servlet-class>net.sourceforge.stripes.controller.DispatcherServlet</servlet-class>
  32. 32. HelloActionBean.javapublic class HelloActionBean implements ActionBean { private ActionBeanContext context; pirvate String message; @DefaultHandler public Resolution init() { message = “Hello Stripes !” return new ForwardResolution(“/hello.jsp”); }}
  33. 33. hello.jsp${actionBean.message}
  34. 34. AuthActionBean.java@Validate(required=true)private String account;@Validate(required=true, minlength=4, maxlength=6)private String password;
  35. 35. login.jsp<%@taglib prefix=“stripes”uri=“http://stripes.sourceforge.net/stripes.tld” %><stripes:errors/><stripes:form action=“/Auth.action” focus=“account”> <stripes:text id=“account” name=“account”/> <stripes:password id=“password” name=“password”/> <stripes:submit name=“login” value=“LOGIN”/></stripes:form>
  36. 36. • 優點 – 利用 ActionContext 處理 HttpServletRequest – 使用 Resolution 作為 forward / redirect 物件 – 在 JSPs 中直接使用 ${actionBean.obj}• 缺點 – 很少人使用 – 開發週期長 – V1.5.6(2011-03-14), v1.5.7(2012-05-18)
  37. 37. web.xml<servlet> <servlet-name>spring</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class></sevlet>/WEB-INF/spring-servlet.xml
  38. 38. spring-servlet.xml<beans …> <context:component-scan base-package=“javatwo2012.mvc”/> <beanclass=“org.springframework.web.servlet.view.InternalResourceViewResolver”> <property name=“prefix” value=“/WEB-INF/jsp/”/> <property name=“suffix” value=“.jsp”/> </bean></beans>
  39. 39. HelloController.java@Controllerpublic class HelloController { @RequestMapping(“/hello”) public String hello(ModelMap model) { model.addAttribute(“message”, “Hello SpringMVC !”); return “hello”; }}
  40. 40. LoginForm.javapublic class LoginForm { @NotEmpty private String account; @NotEmpty @Size(min=4, max=6) private String password;}
  41. 41. AuthController.java@Controllerpublic class AuthController { @RequestMapping(“/auth/doLogin”) public String hello(@Valid LoginForm loginForm,BindingResult result) { if( result.hasErrors() ) return “login”; //.. }}
  42. 42. login.jsp<%@ taglib prefix=“form”uri=“http://www.springframework.org/tags/form”%><form:form action=“/auth/doLogin” commandName=“loginForm”><form:input path=“account”/><form:errors path=“account”/><form:password path=“password”/><form:errorspath=“password”/><input type=“submit” value=“LOGIN”/></form:form>
  43. 43. • 優點 – 彈性且清晰的框架 – View 端可整合多種解決方案 – 強大的 RESTful 支援 – JSR 303 Bean Validation• 缺點 – 過於彈性, 規範不易 – 沒有內建 AJAX
  44. 44. web.xml<context-param><param-name>tapestry.app-package</param-name><param-value>javatwo2012.mvc.tapestry.web</param-value></context-param><filter-name>app</fitler-name><filter-class>org.apache.tapestry5.TapestryFilter</filter-class>
  45. 45. javatwo2012.mvc.tapestry.web• components – Layout.java• pages – Hello.java – Hello.tml• services – AppModule.java
  46. 46. Login.javapublic class Login { @Property private String account; @Property private String password; public Class<?> onSuccess() { return Welcome.class; }}
  47. 47. Login.tml<html xmlns:t=“…”> <t:form t:id=“loginForm”> <t:textField t:id=“account” value=“account” validate=“required”/> <t:passwordField t:id=“password” value=“password”validate=“required, minLength=4, maxLength=6”/> <t:submit value=“LOGIN”/></html>
  48. 48. • 優點 – 物件開發容易 – 執行效能卓越 – 異常報告完整• 缺點 – 學習曲線高 – 使用者較少, 工作數量也不多
  49. 49. web.xml<filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class><init-param> <param-name>applicationClassName</param-name> <param-value>javatwo2012.mvc.wicket.HelloApp</param-value></init-param>
  50. 50. HelloApp.javapublic class HelloApp extends WebApplication { public Class<? extends Page> getHomePage() { return HelloPage.class; }}
  51. 51. HelloPage.javapublic class HelloPage extends WebPage { public HelloPage() { add( new Label(“message”, “Hello Wicket !!”); }}HelloPage.html<span wicket:id=“message”> Message on here </span>
  52. 52. • 優點 – Swing-based UI 動態開發方式 – 元件可重複使用性高 – 熱血的社群• 缺點 – 學習曲線高 – Server-side 所需硬體較高 ( cpu/ram) – 效能調校需要長足經驗 – 使用者較少, 工作數量也不多
  53. 53. web.xml <servlet-class> Javax.faces.webapp.FacesServlet </servlet-class>JSF 2
  54. 54. HelloBean @ManagedBean @SessionScoped public class HelloBean {JSF 2 private String message = “Hello JSF 2.0 !”; private String getMessage() { return message; } }
  55. 55. hello.xhtml <html xmlns:f=“http://java.sun.com/jsf/core” xmlns:h=“http://java.sun.com/jsf/html”> <h:head><title>JSF 2.0 Hello</title></h:head>JSF 2 <h:body> #{helloBean.message} </h:body> </html>
  56. 56. AuthBean public class AuthBean { public String checkLogin() { if(true) {JSF 2 forward=“login”; return “Login Success”; } else { return “Login Failed”; } } public String doLogin() { return forward; } }
  57. 57. login.xhtml <form id=“loginForm”> <h:inputText id=“account” value=“#{authBean.account}” required=“true”/> <h:message for=“account” style=“color:red”/>JSF 2 <h:inputSecret id=“password” value=“#{authBean.password}” required=“true”> <f:validateLength minimum=“4” maximum=“6”/> </h:inputSecret> <h:commandButton id=“loginBtn” value=“LOGIN” actionListener=“#{authBean.checkLogin}” action=“#{authBean.doLogin}”/> </form>
  58. 58. JSF 2
  59. 59. • 優點 – JavaEE 標準, 官方支援 – 具有非常多實作專案 – 進入門檻低 – 有視覺化設計的 IDE 介面JSF 2 • 缺點 – 大多元件需要使用 SessionScope 浪 費 Server-side 資源 – 大量與繁瑣的 JSPs (xhtml) 開發
  60. 60. hello.gwt.xml<module> <inherits name=“com.google.gwt.user.User” /> <source path=“client”/> <entry-point class=“javatwo2012.mvc.gwt.client.Hello” /></module>
  61. 61. client/HelloService.java@RemoteServiceRelativePath(“hello”)public interface HelloService extends RemoteService { public String getMessage();}client/HelloServiceAsync.javapublic interface HelloServiceAnsyc { void getMessage(AsyncCallback<String> callback);}
  62. 62. client/Hello.javapublic class Hello implements EntryPoint { private final HelloServiceAsync helloService =GWT.create(HelloService.class); private final Label message = new Label(); public void onModuleLoad() { RootPanel.get().add(message); helloService.getMessage(new AsyncCallback<String>() { public void onSuccess(String result) { .. } public void onFailure(Throwable caught) { … } });}
  63. 63. server/HelloServiceImplpublic class HelloServiceImpl extends RemoteServiceServletimplements HelloService { public String getMessage() { return “Hello GWT !”; }}
  64. 64. web.xml<servlet-class>Javatwo2012.mvc.gwt.server.HelloServiceImpl</servlet-class>
  65. 65. hello.jsp<script type=“text/javascript” language=“javascript”src=“…/nocache.js”></script>
  66. 66. hello.gwt.xml<module> <inherits name=“com.google.gwt.user.User” /> <source path=“client”/> <entry-point class=“javatwo2012.mvc.gwt.client.Hello” /></module>
  67. 67. • 優點 – Rich-client – 有 GWT Designer 支援 – UI 編寫容易, 無須理解 Javascript ? – 有許多延伸實作的專案• 缺點 – 學習曲線高 – 開發效率慢 – 都是在 Java 上開發
  68. 68. Conclusion

×