目錄
1、去官網下載glassfish4.1.1(最后發現glassfish5也行)
2、配置glassfish環境變數
3、修改jdk環境變數
4、測驗glassfish是否可以正常啟動
5、在IDEA中創建一個JSF專案
6.問題:部分標簽元素無法顯示
1、下載glassfish
去官網下載glassfish4.1.1(最后發現glassfish5也行)
官網地址:GlassFish
https://javaee.github.io/glassfish/
點擊download
選擇圖中版本 
下載完解壓到自己指定的目錄即可
2、配置glassfish環境變數
將glassfish的bin目錄添加到系統變數path中
3、修改jdk環境變數
將之前系統環境變數path里的jdk1.8的環境變數上移到第一個
注意:jdk一定要是1.8,并且必須移到第一個
4、測驗glassfish是否可以正常啟動
在cmd里直接輸入 asadmin start-domain ,出現下圖所示即表明配置成功

再輸入 asadmin stop-domain 可以停止glassfish
5、在IDEA中創建一個JSF專案
1.選擇 java Enterprise, 點擊next
2.勾選 Web Profile,IDEA會自動幫你勾選其他所需要的框架(包括JSF),點擊next
3.輸入模塊名,點擊finish
4.IDEA生成默認的專案結構

5.默認pom檔案內容
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>jsf-03</artifactId>
<version>1.0-SNAPSHOT</version>
<name>jsf-03</name>
<packaging>war</packaging>
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<junit.version>5.6.2</junit.version>
</properties>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>8.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.0</version>
</plugin>
</plugins>
</build>
</project>
6.修改默認生成檔案,不修改會報錯
<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
version="2.2">
</faces-config>
7.在webapp目錄下新建index.xhtml檔案
<?xml version="1.0" encoding="UTF-8"?>
<!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://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<f:view>
<h:outputLabel value="Hello, world"/>
</f:view>
</html>
8.配置運行環境
點擊運行->編輯配置
9.點擊左上角?,選擇glassfish,local

10.點擊配置,找到自己解壓的glassfish的根目錄

11.點擊部署,點擊?,點擊工件,選擇war_explore結尾的

12.點擊應用,點擊服務器,修改默認生成的url

13.點擊確定, 啟動服務器,在瀏覽器里輸入相應鏈接進行訪問

6.問題:部分標簽元素無法顯示
1.頁面代碼如圖所示
<?xml version="1.0" encoding="UTF-8"?>
<!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" xml:lang="en" lang="en"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<head>
MessagesTags
</head>
<h:head>
</h:head>
<h:body>
<h:form>
<h:panelGrid columns="1" width="60%">
<f:facet name="header">Login</f:facet>
<f:facet name="footer">
<h:outputText value="快到中秋節了"/>
<h:commandButton action="Layoutpage" value="Submit"/>
</f:facet>
<h:panelGroup>
<h:outputText value="Enter ID:"/><h:inputText id="it1" required="true" requiredMessage="ynb"/>
<h:message for="it1"/>
</h:panelGroup>
<h:panelGroup>
<h:inputSecret required="true">
<h:outputLabel value="Enter Password"/>
</h:inputSecret>
</h:panelGroup>
</h:panelGrid>
<h:messages/>
</h:form>
</h:body>
</html>
2.運行效果圖

3.報錯代碼:
Error in event handler: Error: Failed to execute 'appendChild' on 'Node': This node type does not support this method.
at Updater.check (chrome-extension://fhopckooaleifhophcpcakhfkajmnffo/js/gmWrapper.js:4:3204)
at init (chrome-extension://fhopckooaleifhophcpcakhfkajmnffo/js/jquery-1.7.2.js:1:3311)
at onReadyGM (chrome-extension://fhopckooaleifhophcpcakhfkajmnffo/js/jquery-1.7.2.js:1:76)
at Object.onInitializedGM (chrome-extension://fhopckooaleifhophcpcakhfkajmnffo/js/gmWrapper.js:4:636)
at Object.callbackResponse (chrome-extension://fhopckooaleifhophcpcakhfkajmnffo/js/gmWrapper.js:4:331)
at chrome-extension://fhopckooaleifhophcpcakhfkajmnffo/js/gmWrapper.js:4:3378
4.原因,web.xml 檔案配置錯誤
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0">
<display-name>JSF_SE_CH2_html01_0915</display-name>
<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>
<context-param>
<description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>resources.application</param-value>
</context-param>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>
</web-app>
5.解決辦法,將web.xml檔案內容洗掉,只保留 <welcome-file-list> 即可
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0">
<display-name>JSF_SE_CH2_html01_0915</display-name>
<welcome-file-list>
<welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>
</web-app>
6.運行截圖,至此,大功告成!

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/300458.html
標籤:java
