Lunski's Clutter

This is a place to put my clutters, no matter you like it or not, welcome here.

0%

JSF Tutorial - Basic

新企劃。

最近看到YT有個不錯的教學,練了一陣子覺得還不錯, 跟大家分享一下。

目前看完基礎環境設定與語法部份, 除了將基礎練習放到Github, 也會繼續完成後續專案部份。

Server: GlassFish vs. Payara

Payara Server is an open source version of Glassfish since 2014 and more powerful than Glassfish

1,2 Add Framework

1
2
3
4
project properties > Frameworks > Add> JavaServer Faces 

* You can change URL pattern
Frameworks > Configuration > {Change JSF Servlet URL Pattern `faces/*`}

3 Add Controller, action tag to call a function

1
2
3
4
5
6
7
Source Packages/*Controller.java
@ManagedBean(name = "UserCtrl") // 未指定name預設首字小寫
...
public void clicked()

Web Pages> WEB-INF/*.xhtml
<h:commandButton action="#{UserCtrl.clicked()}"

4 Controller, getter, setter, variable

1
2
3
4
5
*Controller.java
private String name; // insert code getter, setter

*.xhtml
<h:inputText value="#{UserCtrl.name}">

5 show outputText, use br/

1
2
3
4
5
6
7
8
9
*Controller.java
private String outputText;

public void sayHello(){
outputText="Hello "+ name;
}

*.xhtml
<h:outputText value="#{UserCtrl.outputText}"/>

6, 7 forward page, method

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<h:commandButton action="second.xhtml" value="go to next page"/>
---
*Controller.java

public String forwardPage(){
if("2".equals(pageNumber)){
return "second.xhtml";
}else{
return "index.xhtml";
}
}

*.xhtml

<h:commandButton value="forward page" action="#{UserCtrl.forwardPage()}" />
```

## 8 [redirect page](https://mkyong.com/jsf2/jsf-page-forward-vs-page-redirect/)
faces-redirect=true 保持頁面跳轉時狀態連續, 不渲染新頁面, 參數不丟失

"second.xhtml?faces-redirect=true"

## 9-17 元件

<h:inputSecret value=”#{UserCtrl.pageNumber}”/>
<h:inputTextarea
<h:selectBooleanCheckbox

*Controller.java
@PostConstruct
public void init(){}

        <h:selectManyCheckbox>
            <f:selectItem itemLabel="Java Server Faces" itemValue="JSF"/>

<f:selectItems value=”#{}”>
h:selectOneRadio
h:selectOneListbox

1
2
3

## 18 [File](https://stackoverflow.com/questions/18302287/illegal-syntax-for-set-operation)

value=”#{UserCtrl.file} “<– 不可以有空格

```

19-21 DataTable

25 使用開發者工具, script與自製時鐘

26 Ajax


如果你覺得這篇文章很棒,請你不吝點讚 (゚∀゚)

Welcome to my other publishing channels