Maven3 创建 JSF2应用程序

16 篇文章 0 订阅

本文主要介绍用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 developmentlifecycle. Valid values are:Development, UnitTest, SystemTest, or Production.The runtime will display helpful hints to correct common mistakeswhen 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-domainmvn org.glassfish.maven.plugin:maven-glassfish-plugin:start-domain

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

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

initForDebug.sh文件(该脚本用于debug模式启动glassfish和部署网站)mvn clean installmvn org.glassfish.maven.plugin:maven-glassfish-plugin:start-domainmvn org.glassfish.maven.plugin:maven-glassfish-plugin:redeploycd /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>

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是使用IDEA 2023.2.3创建Maven项目的步骤: 1. 打开IDEA,点击菜单栏的 "File",然后选择 "New",再选择 "Project"。 2. 在弹出的窗口中,选择 "Maven" 选项,并点击 "Next"。 3. 在 "New Project" 窗口中,选择 "Create from archetype",然后点击 "Add Archetype"。 4. 在 "Add Archetype" 窗口中,填写以下信息: - GroupId: 输入你的项目组织标识符,例如:com.example - ArtifactId: 输入你的项目名称,例如:my- Archetype GroupId: 输入 "org.apache.maven.archetypes" - Archetype ArtifactId: 输入 "maven-archetype-quickstart" - Archetype Version: 输入 "1.4" 然后点击 "OK"。 5. 在 "New Project" 窗口中,选择刚刚添加的archetype,并填写以下信息: - GroupId: 输入你的项目组织标识符,例如:com.example - ArtifactId: 输入你的项目名称,例如:my-project - Version: 输入你的项目版本号,例如:1.0-SNAPSHOT - Project name: 输入你的项目名称,例如:My Project - Project location: 输入你的项目存放路径,例如:D:\projects\my-project 然后点击 "Next"。 6. 在 "New Project" 窗口中,选择你需要的项目模块,然后点击 "Next"。 7. 在 "New Project" 窗口中,选择你需要的项目技术栈,然后点击 "Next"。 8. 在 "New Project" 窗口中,填写项目的其他配置信息,例如:项目的Java版本、项目的编码等,然后点击 "Finish"。 9. IDEA会自动创建并导入Maven项目。你可以在 "Project" 窗口中看到项目的目录结构和文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值