Javatwo2012 java frameworkcomparison
Upcoming SlideShare
Loading in...5
×
 

Javatwo2012 java frameworkcomparison

on

  • 1,667 views

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

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

Statistics

Views

Total Views
1,667
Views on SlideShare
1,665
Embed Views
2

Actions

Likes
1
Downloads
61
Comments
0

1 Embed 2

https://si0.twimg.com 2

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Javatwo2012 java frameworkcomparison Javatwo2012 java frameworkcomparison Presentation Transcript

  • SoftLeader Tech. Corp.李日貴 jini王文農 steven
  • Java 熱門 Framework 運用與比較
  • 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
  • JavaEE Multi-Tiers architectureHttpServletRequest Web Business DAO DB Tier Tier TierHttpServletResponse
  • Lots of frameworks
  • Java Persistence ( DAO )
  • JPA 2
  • • 簡化 JDBC• Native SQL• 設定容易• XML or Annotation
  • Customer.javapublic class Customer { private Long id; private String name; private Integer age; // getters and setters}
  • Customer-mapper.xml<mappernamespace=“javatwo2012.dao.mybatis.Customer”> <select id=“getCustomerById”parameterType=“Long” resultType=“customer”> <!-- select SQL with #id --> </select></mapper>
  • mybatis-config.xml<configuration> <settings> … </settings> <mappers> <mapperresource=“javatwo2012/dao/mybatis/Customer-mapper.xml”/> </mappers></configuration>
  • CustomerDao.javaSqlSession session =sqlSessionFactory.openSession();try { Customer customer =session.selectOne(“javatwo2012.dao.mybatis.Customer.getCustomerById”, new Long(101));} finally { session.close();}
  • • 完整 ORM• 易於更換資料庫• 多種查詢機制• 支援多種 Cache• 多元的資料 Initialize
  • Customer.java@Entity@Table(name=“CUSTOMER”)public class Customer { @Id @GeneratedValue(strategy=GenerationType.Identity) @Column(name=“ID”) private Long id; // getters and setters}
  • hibernate-cfg.xml<hibernate-configuration> <session-factory> <property name=“dialect”>DB_Dialect</property> <mapping class=“javatwo2012.dao.hibernate.Customer”/> </seesion-factory></hibernate-configuration>
  • 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();}
  • • JavaEE 標準 • 吸收了各家的優點JPA 2 • 多種查詢機制 • 支援多種 Cache • 多元的資料 Initialize • Transaction Control
  • 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 }
  • persistence.xml <persistence> <persistence-unit name=“javatwo2012demo” transaction- type=“RESOURCE_LOCAL” >JPA 2 <provider>org.hibernate.ejb.HibernatePersistence</provider> <properties>..</properties> </persistence-unit> </persistence>
  • 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); }
  • 項目 JPA 2 學習容易度 容易 複雜 複雜 查詢 API 少 多 多 SQL優化容易度 較簡單 複雜 複雜資料庫方言依賴程度 高 低 低 效能 佳 佳 依實作而定 可移植性 普通 佳 依實作而定 社群支援 多 多 多
  • Java Web ( MVC )
  • • Annotation Driven• AJAX inside• UnitTest• REST Integration• Multi-Languages• HTML5 + CSS3
  • • Action-Based • Component-Based JSF 2
  • web.xml<filter-class>org.apache.struts2.dispatcher.ng.StrutsPrepareAndExecuteFilter</filter-class>
  • 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>
  • HelloAction.javapublic class HelloAction extends ActionSupport { private String message; public String getMessage() { return this.message; } public String execute() { return SUCCESS; }}
  • hello.jsp<%@ taglib prefix=“s” uri=“/sturts-tags”%><s:property value=“message”/>
  • AuthAction.javapublic class AuthAction extends ActionSupport { public String validate() { if( … ) { addFieldError(“account”, “Account can’t be empty”); } } public String execute() { …. }}
  • • 優點– 很多人用, 工作機會多– 豐富的 Taglibs– 很多擴充的 plugins• 缺點– struts.xml– 需要用 result 對應– Source-Codes 混亂
  • web.xml<filter-class>net.sourceforge.stripes.controller.StripesFilter</filter-class><servlet-class>net.sourceforge.stripes.controller.DispatcherServlet</servlet-class>
  • HelloActionBean.javapublic class HelloActionBean implements ActionBean { private ActionBeanContext context; pirvate String message; @DefaultHandler public Resolution init() { message = “Hello Stripes !” return new ForwardResolution(“/hello.jsp”); }}
  • hello.jsp${actionBean.message}
  • AuthActionBean.java@Validate(required=true)private String account;@Validate(required=true, minlength=4, maxlength=6)private String password;
  • 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>
  • • 優點 – 利用 ActionContext 處理 HttpServletRequest – 使用 Resolution 作為 forward / redirect 物件 – 在 JSPs 中直接使用 ${actionBean.obj}• 缺點 – 很少人使用 – 開發週期長 – V1.5.6(2011-03-14), v1.5.7(2012-05-18)
  • web.xml<servlet> <servlet-name>spring</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class></sevlet>/WEB-INF/spring-servlet.xml
  • 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>
  • HelloController.java@Controllerpublic class HelloController { @RequestMapping(“/hello”) public String hello(ModelMap model) { model.addAttribute(“message”, “Hello SpringMVC !”); return “hello”; }}
  • LoginForm.javapublic class LoginForm { @NotEmpty private String account; @NotEmpty @Size(min=4, max=6) private String password;}
  • AuthController.java@Controllerpublic class AuthController { @RequestMapping(“/auth/doLogin”) public String hello(@Valid LoginForm loginForm,BindingResult result) { if( result.hasErrors() ) return “login”; //.. }}
  • 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>
  • • 優點 – 彈性且清晰的框架 – View 端可整合多種解決方案 – 強大的 RESTful 支援 – JSR 303 Bean Validation• 缺點 – 過於彈性, 規範不易 – 沒有內建 AJAX
  • 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>
  • javatwo2012.mvc.tapestry.web• components – Layout.java• pages – Hello.java – Hello.tml• services – AppModule.java
  • Login.javapublic class Login { @Property private String account; @Property private String password; public Class<?> onSuccess() { return Welcome.class; }}
  • 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>
  • • 優點 – 物件開發容易 – 執行效能卓越 – 異常報告完整• 缺點 – 學習曲線高 – 使用者較少, 工作數量也不多
  • 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>
  • HelloApp.javapublic class HelloApp extends WebApplication { public Class<? extends Page> getHomePage() { return HelloPage.class; }}
  • HelloPage.javapublic class HelloPage extends WebPage { public HelloPage() { add( new Label(“message”, “Hello Wicket !!”); }}HelloPage.html<span wicket:id=“message”> Message on here </span>
  • • 優點 – Swing-based UI 動態開發方式 – 元件可重複使用性高 – 熱血的社群• 缺點 – 學習曲線高 – Server-side 所需硬體較高 ( cpu/ram) – 效能調校需要長足經驗 – 使用者較少, 工作數量也不多
  • web.xml <servlet-class> Javax.faces.webapp.FacesServlet </servlet-class>JSF 2
  • HelloBean @ManagedBean @SessionScoped public class HelloBean {JSF 2 private String message = “Hello JSF 2.0 !”; private String getMessage() { return message; } }
  • 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>
  • 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; } }
  • 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>
  • JSF 2
  • • 優點 – JavaEE 標準, 官方支援 – 具有非常多實作專案 – 進入門檻低 – 有視覺化設計的 IDE 介面JSF 2 • 缺點 – 大多元件需要使用 SessionScope 浪 費 Server-side 資源 – 大量與繁瑣的 JSPs (xhtml) 開發
  • hello.gwt.xml<module> <inherits name=“com.google.gwt.user.User” /> <source path=“client”/> <entry-point class=“javatwo2012.mvc.gwt.client.Hello” /></module>
  • client/HelloService.java@RemoteServiceRelativePath(“hello”)public interface HelloService extends RemoteService { public String getMessage();}client/HelloServiceAsync.javapublic interface HelloServiceAnsyc { void getMessage(AsyncCallback<String> callback);}
  • 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) { … } });}
  • server/HelloServiceImplpublic class HelloServiceImpl extends RemoteServiceServletimplements HelloService { public String getMessage() { return “Hello GWT !”; }}
  • web.xml<servlet-class>Javatwo2012.mvc.gwt.server.HelloServiceImpl</servlet-class>
  • hello.jsp<script type=“text/javascript” language=“javascript”src=“…/nocache.js”></script>
  • hello.gwt.xml<module> <inherits name=“com.google.gwt.user.User” /> <source path=“client”/> <entry-point class=“javatwo2012.mvc.gwt.client.Hello” /></module>
  • • 優點 – Rich-client – 有 GWT Designer 支援 – UI 編寫容易, 無須理解 Javascript ? – 有許多延伸實作的專案• 缺點 – 學習曲線高 – 開發效率慢 – 都是在 Java 上開發
  • Conclusion