Maven3 创建 JSF2应用程序

本文主要介绍用Maven3创建一个简单的JSF2web应用程序,该程序仅仅包含一个非常简单的Facelete页面index.xhtml。同时该工程还使用了MyBatis和logback等库。我自己将该工程作为一个模板工程。

创建Web程序的命令

使用maven-archetype-webapp命令创建程序,具体格式如下:
mvn archetype:create
  -DgroupId=[your project's group id]
  -DartifactId=[your project's artifact id]
  -DarchetypeArtifactId=maven-archetype-webapp
创建的项目目录结构如下:


  |-- src
  | `-- main
  | `-- java
  | |-- resources
  | |-- webapp
  | | `-- WEB-INF
  | | `-- web.xml
  | `-- index.jsp
  `-- pom.xml

创建项目

mvn archetype:generate -DgroupId=com.freebird -DartifactId=example -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
目录结构如下:
chenshu@csdesktop:~/work/svnclient/MyCodes/document/JSF2$ ls -R ./example/
./example/:
pom.xml src

./example/src:
main

./example/src/main:
resources webapp

./example/src/main/resources:

./example/src/main/webapp:
index.jsp WEB-INF

./example/src/main/webapp/WEB-INF:
web.xml

把该项目修改成JSF2项目

创建src/main/java目录,并创建子目录树如下:
chenshu@csdesktop:~/work/svnclient/MyCodes/document/JSF2/example/src/main/java$ ls -R ./
./:
com

./com:
freebird

./com/freebird:
example

./com/freebird/example:
component pagebean renderer

./com/freebird/example/component:

./com/freebird/example/pagebean:

./com/freebird/example/renderer:


src/main/webapp目录就是JavaEE Web模块的根目录,里面包含了所有的Faceletes的xhtml页面文件以及描述部署信息的配置文件

在该目录下创建index.xhtml文件,内容如下:
<!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:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html">
    <f:view contentType="text/html"/>
    <h:head>
        <title>Hello World!</title>
    </h:head>
    <h:body bgcolor="white">
        <h2>My name is Duke. What is yours?</h2>
        <h:form id="helloForm" >
<h:outputText value="This is an example page"/>
        </h:form>
    </h:body>
</html>

修改pom.xml文件如下

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.freebird</groupId>
  <artifactId>example</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>example Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>javax</groupId>
      <artifactId>javaee-web-api</artifactId>
      <version>6.0</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>0.9.26</version>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.0.4</version>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.14</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>example</finalName>
    <pluginManagement>
      <plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${compileSource}</source>
<target>${compileSource}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.0</version>
</plugin>
<plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-resources-plugin</artifactId>
          <version>2.3</version>
        </plugin>
<plugin>
<groupId>org.glassfish.maven.plugin</groupId>
<artifactId>maven-glassfish-plugin</artifactId>
<version>2.1</version>
<configuration>
<glassfishDirectory>/usr/local/glassfish-3.0.1</glassfishDirectory>
<user>admin</user>
<passwordFile>/home/chenshu/glassfish_password</passwordFile>
<debug>true</debug>
<droptables>true</droptables>
<!—

<terse>true</terse>—>

<echo>true</echo>
<domainDirectory>/usr/local/glassfish-3.0.1/glassfish/domains</domainDirectory>
<domain>
<name>domain1</name>
<adminPort>4848</adminPort>
<httpPort>8080</httpPort>
<httpsPort>8443</httpsPort>
<iiopPort>3700</iiopPort>
<jmsPort>7676</jmsPort>
<reuse>false</reuse>
</domain>
<components>
<component>
<name>example</name>
<artifact>target/${project.build.finalName}.war</artifact>
</component>
</components>
</configuration>
</plugin>
      </plugins>
    </pluginManagement>
  </build>

  <properties>
    <netbeans.hint.deploy.server>gfv3ee6</netbeans.hint.deploy.server>
    <compileSource>1.6</compileSource>
  </properties>
</project>

Modfy the web.xml file like so:

<?xml version='1.0' encoding='UTF-8'?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 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_2_5.xsd"> <display-name>example</display-name> <description>JSF2 example project</description> <context-param> <description> Tell the runtime where we are in the project development lifecycle. Valid values are: Development, UnitTest, SystemTest, or Production. The runtime will display helpful hints to correct common mistakes when the value is Development. </description> <param-name>javax.faces.PROJECT_STAGE</param-name> <param-value>Development</param-value> </context-param> <context-param> <param-name>facelets.FACELETS_SKIP_COMMENTS</param-name> <param-value>true</param-value> </context-param>

<context-param> <param-name>com.sun.faces.allowTextChildren</param-name> <param-value>true</param-value> </context-param>

<!— Faces Servlet —> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet>

<servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>/faces/*</url-pattern> </servlet-mapping>

<welcome-file-list> <welcome-file>faces/index.xhtml</welcome-file> </welcome-file-list>

</web-app>

部署网站相关命令

本文假设已经安装了glassfish v3.0.1,现在show一下maven glassfish plugin提供的各种操作命令,我在example目录下创建了几个脚本文件,省得每次敲很长的命令: start_doman.sh文件 mvn org.glassfish.maven.plugin:maven-glassfish-plugin:start-domain

restart-domain.sh文件 mvn org.glassfish.maven.plugin:maven-glassfish-plugin:stop-domain mvn org.glassfish.maven.plugin:maven-glassfish-plugin:start-domain

deploy.sh文件 mvn clean install mvn org.glassfish.maven.plugin:maven-glassfish-plugin:deploy

redeploy.sh文件 mvn clean install mvn org.glassfish.maven.plugin:maven-glassfish-plugin:redeploy

initForDebug.sh文件(该脚本用于debug模式启动glassfish和部署网站) mvn clean install mvn org.glassfish.maven.plugin:maven-glassfish-plugin:start-domain mvn org.glassfish.maven.plugin:maven-glassfish-plugin:redeploy cd /usr/local/glassfish-3.0.1/bin ./asadmin restart-domain domain1

logback配置文件

在src/main/resources目录下创建logback.xml文件,内容如下: <?xml version="1.0" encoding="UTF-8"?> <configuration>

<appender name="File" class="ch.qos.logback.core.FileAppender"> <file>/home/chenshu/example.log</file> <layout class="ch.qos.logback.classic.PatternLayout"> <Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern> </layout> </appender> <logger name="com.base22" level="TRACE"/> <root level="debug"> <appender-ref ref="File" /> </root> </configuration>

你可能感兴趣的:(maven3)