日期:2014-05-19  浏览次数:20804 次

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.html</welcome-file>
	</welcome-file-list>
	<error-page>
		<error-code>404</error-code>
		<location>/error.html</location>
	</error-page>
	<session-config>
		<session-timeout>10</session-timeout>
	</session-config>
	<error-page>
		<exception-type>javax.faces.