当前位置: 代码迷 >> J2EE >> J2EE6札记-(1)架构
  详细解决方案

J2EE6札记-(1)架构

热度:9576   发布时间:2013-02-25 21:38:44.0
J2EE6笔记-(1)架构

    感谢Sun公司的一系列解决方案,EJB3架构起来比SSH简单许多,而且EJB3天生有着强大的分布式部署特性,我个人认为EJB3要比SSH更有吸引力.

很多朋友认为EJB的架构很重量级,我个人认为那只是EJB2的时代,EJB3一样能构建一个很轻量便捷的系统.

    首先我们来构建一个最基本的EJB3系统:


系统环境:

1.glassfish3.1

2.eclipse 3.7

3.Derby

不要问我为什么用这些不主流的东西,原因是他们很简单,拿来就能用.特别是derby,很方便的说.


1.glassfish3.1配置,不调优的话基本不用配置.不过一些常用命令还是要记录一下

控制台:     http://localhost:4848/

创建域:     asadmin create-domain f:

开始域:     asadmin start-domain --domaindir f:

关闭域:     asadmin stop-domain --domaindir f:

部署war:  asadmin deploy f:j2ee.war

反部署:     asadmin undeploy f:j2ee.war

运行DB:   asadmin start-database --dbhome f:j2db

2.Derby

Derby有两种连接方式,(1)嵌入式,(2)客户端式,区别是嵌入式是独占连接

glassfish默认就有两个数据库连接,分别对应derby的两种连接方式,如果选择timepool,则当我们部署一个EJB的时候DB会同时启动,反之则要手动启动.

3.eclipse

(1)创建一个动态web项目

(2)在src/meta-inf文件夹中创建一个persistence.xml文件,内容如下


<?xml version="1.0" encoding="UTF-8"?><persistence version="2.0"	xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"	xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">	<persistence-unit name="appPU" transaction-type="JTA">		<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>		<jta-data-source>jdbc/__TimerPool</jta-data-source>		<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>		<validation-mode>NONE</validation-mode>		<properties>			<property name="eclipselink.target-database" value="Auto" />			<property name="eclipselink.ddl-generation" value="create-tables" />			<property name="eclipselink.show-sql" value="true" />		</properties>	</persistence-unit></persistence>

(3)修改web.xml文件如下

<?xml version='1.0' encoding='UTF-8'?><web-app xmlns="http://java.sun.com/xml/ns/javaee" version="3.0"	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-app_3_0.xsd">	<display-name>J2EE</display-name>	<context-param>		<param-name>javax.faces.STATE_SAVING_METHOD</param-name>		<param-value>client</param-value>	</context-param>	<context-param>		<param-name>javax.faces.PROJECT_STAGE</param-name>		<param-value>Production</param-value>	</context-param>	<context-param>		<param-name>com.sun.faces.resourceUpdateCheckPeriod</param-name>		<param-value>-1</param-value>	</context-param>	<context-param>		<param-name>com.sun.faces.validateXml</param-name>		<param-value>true</param-value>	</context-param>	<servlet>		<servlet-name>Faces Servlet</servlet-name>		<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>	</servlet>	<servlet-mapping>		<servlet-name>Faces Servlet</servlet-name>		<url-pattern>*.jsf</url-pattern>	</servlet-mapping>	<welcome-file-list>		<welcome-file>index</welcome-file>	</welcome-file-list>	<error-page>		<error-code>404</error-code>		<location>/error</location>	</error-page>	<session-config>		<session-timeout>10</session-timeout>	</session-config>	<error-page>		<exception-type>javax.faces.application.ViewExpiredException</exception-type>		<location>/index</location>	</error-page></web-app>

(4)小实例,服务器端,Count.java

/** *@Author          : Copper Lau *@Created Date    : Feb 19, 2013 *@Modified By     : Copper Lau *@Modified Date   : Feb 19, 2013 *@Version         : 1.0 */package com.copper.test;import javax.faces.bean.ManagedBean;import javax.faces.bean.SessionScoped;@ManagedBean(name = "count")@SessionScopedpublic class Count{	private Integer count = 0;	public Integer getCount() {		return count;	}	public void increCount(Integer i) {		count = count + i;	}	public void reset() {		this.count = 0;	}}

(5)Web端

<!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:h="http://java.sun.com/jsf/html"><h:head>	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />	<title>Ajax</title></h:head><h:body>	<h:form id="form1" prependId="false">		<h:outputScript name="jsf.js" library="javax.faces" target="head" />		<h:outputText id="out1" value="#{count.count}" />		<br />		<!-- Increment the counter on the server, and the client -->		<h:commandButton id="button1" value="Count" onclick="jsf.ajax.request(this, event, {execute: this.id, render: 'testId'}); return false;" actionListener="#{count.increCount(234)}" />		<br />		<!-- Resets the counter -->		<h:commandButton id="reset" value="reset" onclick="jsf.ajax.request(this, event, {execute:'reset', render: 'out1'}); return false;" actionListener="#{count.reset}" />		<h:panelGrid id="testId">		<div>			<h:outputText value="#{count.count}" />		</div>		</h:panelGrid>	</h:form></h:body></html>

配置完成,是不是很简单?

到目前为止,LIB文件夹的第三方包的个数为0.

部署完成后输入http://localhost:8080/J2EE/index.jsf就可以看到结果了.


  相关解决方案