2018년 3월 20일 화요일

[EtC] thymeleaf 자주 사용하는 예제


출처 : http://firstboos.tistory.com


Thymeleaf 에서 자주 사용하는 예제들을 정리해본다.

* Javascript 에서 비교 연산자
: Thymeleaf 에서 '<', '>' 태그를 엄격하게 검사하기 때문인지, 자바스크립트에서 사용시 [[CDATA]] 로 묶어줘야한다.
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">

...

<script type="text/javascript">

function checkLevel() {
var rate = $("#level").val();
/*<![CDATA[*/
if (rate > 100) {
alert("레벨은 100 이하의 숫자여야 합니다.");
return;
}
  /*]]>*/
}

</body>
</html>

* if / else 문
: <tr>, th:each 로 테이블 형태의 데이터를 표시할때, 값의 존재 유무에 따라서 컬럼값을 다르게 표현할때

- if / unless 사용
<td th:if="${data.buyTime} != 0" th:text="${ddf.format(data.buyTime)}" ></td>
<td th:unless="${data.buyTime} != 0">0</td>

- ? 이항 연산자 사용
<td th:text="${data.buyTime} ? ${customformat(data.buyTime)} : '0'" ></td>

- switch / case 사용
<td th:switch="${data.type}" >
<span th:case="1" class="center-block label label-info">접속</span>
<span th:case="2" class="center-block label label-primary">레벌 업</span>
<span th:case="3" class="center-block label label-warning">만료</span>
</td>

* select 사용
: 컨트롤러에서 List<itemData> 를 생성해서 model.addAttribute("itemDatas", itemDataList) 형태로 전달하는 경우.

<div class="row">
<label class="label col col-2">아이템 정보</label>
<div class="col col-10">
<label class="select"> 
 <select class="select" name="itemId" id="itemId">
<option value="0">ALL</option>
<option th:each="itemData : ${itemDatas}" th:value="${itemData.ItemId}" th:text="${itemData.itemId} + ' : ' + ${itemData.name}"></option>
</select><i></i>
</label>
         </div>
</div>

: 컨트롤러에서 Map<Integer, String> 를 생성해서 model.addAttribute("itemDataMap", itemDataMap) 형태로 전달하는 경우.
<div class="row">
<label class="label col col-2">아이템 정보</label>
<div class="col col-10">
<label class="select"> 
<select class="select" name="itemId" id="itemId">
<option th:each="itemData : ${itemDataMap}" th:value="${itemData.key}" th:text="${itemData.key} + ' : ' + ${itemData.value}"></option>
</select><i></i>
</label>
...

: 컨트롤러에서 Map<Integer, ItemData> 를 생성해서 model.addAttribute("itemDataMap", itemDataMap) 형태로 전달하는 경우.
@Data
public class ItemData {
    private int itemId;
    private String name;
}

<div class="row">
<label class="label col col-2">아이템 정보</label>
<div class="col col-10">
<label class="select"> 
<select class="select" name="itemId" id="itemId">
<option th:each="itemData : ${itemDataMap}" th:value="${itemData.key}" th:text="${itemData.key} + ' : ' + ${itemData.value.name}"></option>
</select><i></i>
</label>
...

* table 사용
: 컨트롤러에서 Map<Integer, String> 를 생성해서 model.addAttribute("itemDataMap", itemDataMap) 형태로 전달하는 경우.
<tr th:if="${not #lists.isEmpty(datas)}" th:each="data : ${datas}">
<td>
<a data-toggle="modal" href="#dataModal" class="btn btn-xs btn-default" th:onclick="|javascript:changePopup('${data.seq}')|"> 
<i class="fa fa-times"></i>
</a>
</td>
<td th:text="${data.seq}" ></td>
<td th:text="${data.type}" ></td>
<td th:text="${data.itemId} + ':' + ${itemDataMap.get(data.itemId)}" ></td>
<td th:text="${data.text}" ></td>
</tr>
: itemDataMap.get(id) 의 결과값은 itemData 의 이름이나 다른 값(value) 가 되겠다.

* a href 사용
: <a> 로 다른 페이지로 이동할 경우
<td th:if="${data.num != 0}">
<a href="/user/list?id=" th:attrappend="href=${data.id}" th:text="${id.num}"></a>
</td>

: 파라미터가 여러개(multil) 일 경우
<td th:if="${data.num != 0}">
<a th:href="@{/user/list(id=${data.id},nick=${data.nick})}" th:text="${data.num}"></a>
</td>


참고한 사이트

2018년 3월 10일 토요일

2018년 3월 5일 월요일

[LiNuX] Debian 8 에서 nginx compile



root@freecatz:~# apt-get build-dep nginx
root@freecatz:~# wget http://nginx.org/download/nginx-1.13.9.tar.gz
root@freecatz:~# tar zxvf nginx-1.13.9.tar.gz
root@freecatz:~# cd nginx-1.13.9
root@freecatz:~# ./configure --prefix=/usr/local/nginx-1.13.9 --with-http_ssl_module --with-http_v2_module --with-openssl=/root/archive/openssl-1.0.2l
root@freecatz:~# make
root@freecatz:~# make install

2018년 2월 20일 화요일

[LiNuX] debian8 iptables disable

# iptables -F
# iptables -X
# iptables -t nat -F
# iptables -t nat -X
# iptables -t mangle -F
# iptables -t mangle -X
# iptables -P INPUT ACCEPT
# iptables -P OUTPUT ACCEPT
# iptables -P FORWARD ACCEPT

2018년 2월 9일 금요일

[EtC] 맥 사용자 계정의 비밀번호 잃어 버렸을때


맥의 전원을 켜거나 재부팅 후, Command + S 키를 눌러

싱글모드로 부팅을 합니다.

그리고 명령어 입력 상태가 되면 아래의 명령어를 입력 합니다.

# mount -uw /
# passwd <계정명>

새로운 비밀번호를 입력 후,

# reboot -n

부팅이되면 키체인 새로 생성 선택.


2018년 1월 18일 목요일

[GoLaNg] xml parse2

1. XML File 내용
<?xml version="1.0" encoding="UTF-8" ?>
<properties>
    <propertie key="HelloMsg"><![CDATA[Hello World!!]]></propertie>
    <propertie key="TestMsg"><![CDATA[Test!!]]></propertie>
</properties>


2. 프로그램
import (
    "encoding/xml"
    "fmt"
    "io/ioutil"
    "os"
)

/* XML  파싱 하기 위한 구조체 */
type Properties struct {
    Propertie []Propertie `xml:"propertie"`
}

/* XML  파싱 하기 위한 구조체 */
type Propertie struct {
    Key   string `xml:"key,attr"`
    Value string `xml:",cdata"`
}

/* XML 파일로 부터 프로퍼티를 읽어 반환 */
func GetPropFromXML(key string) string {

    fp, err := os.Open("conf/properties2.xml")
    if err != nil {
        panic(err)
    }
    defer fp.Close()

    // xml 파일 읽기
    data, err := ioutil.ReadAll(fp)

    prop := &Properties{}
    xmlErr := xml.Unmarshal(data, prop)
    if xmlErr != nil {
        fmt.Println(err)
    }
    fmt.Println(prop)
    fmt.Println(len(prop.Propertie))
    var result string
    for i := 0; i < len(prop.Propertie); i++ {
        if prop.Propertie[i].Key == key {
            result = prop.Propertie[i].Value
        }
        //        println("---> " + prop.Propertie[i].Key + "\t -> " + prop.Propertie[i].Value)
    }
    return result

}

2018년 1월 16일 화요일

[GoLaNg] sleep

import (
    "fmt"
    "time"
)

func init() {
    fmt.Println("2 초후 프로그램이 시작 합니다.")
}


func main() {
    time.Sleep(time.Second * 2)
    fmt.Println("Hello World!!")
}

[GoLaNg] 시스템 변수 출력 및 기록

import (
"os"
"fmt"
)

/* 시스템 변수를 출력 한다. */
func GetEnvironments() {
for index, env := range os.Environ() {
fmt.Println(index, env)
}
}

/* 시스템 변수를 반환 한다. */
func ReadSysEnv(env string) string {
return os.Getenv(env)
}

/* 시스템 변수를 기록 한다. */
func WriteSysEnv(env string, val string) {
os.Setenv(env, val)
}

[GoLaNg] 특정 디렉토리 목록 출력

import (
"fmt"
"io/ioutil"
"os"
"strconv"
)

/* 특정 디렉토리의 목록을 출력 한다. */
func DirectoryList(dir string) {
files, err := ioutil.ReadDir(dir)
if err != nil {
fmt.Println(err)
}

for _, f := range files {
if f.IsDir() {
fmt.Println(f.Name() + "\t" + "디렉토리")
} else {
fmt.Println(f.Name() + "\t" + "파일" + "\t" + strconv.FormatInt(f.Size(), 10) + "Byte")
}
}
}

[GoLaNg] xml parse

1. XML File 내용
<?xml version="1.0" encoding="UTF-8" ?>
<properties>
    <propertie key="HelloMsg">
        <value><![CDATA[Hello World!!]]></value>
    </propertie>
    <propertie key="TestMsg">
        <value><![CDATA[Test!!]]></value>
    </propertie>
</properties>


2. 프로그램
import (
    "encoding/xml"
    "fmt"
    "io/ioutil"
    "os"

)

/* XML 을 파싱 하기 위한 구조체 */
type Properties struct {
Propertie []Propertie `xml:"propertie"`
}

/* XML 을 파싱 하기 위한 구조체 */
type Propertie struct {
Key   string `xml:"key,attr"`
Value string `xml:"value"`
}

/* XML 파일로 부터 프로퍼티를 읽어 반환 */
func GetPropFromXML(key string) string {
    fp, err := os.Open("freecatz.pe.kr/Conf/properties.xml")
    if err != nil {
        panic(err)
    }
    defer fp.Close()

    // xml 파일 읽기
    data, err := ioutil.ReadAll(fp)

    // xml 디코딩
    var properties Properties
    xmlerr := xml.Unmarshal(data, &properties)
    if xmlerr != nil {
        panic(xmlerr)
    }

    var result string
    for i := 0; i < len(properties.Propertie); i++ {
        if properties.Propertie[i].Key == key {
            result = properties.Propertie[i].Value
        }
        //        fmt.Println("key: " + properties.Propertie[i].Key + "\t -> " + properties.Propertie[i].Value)
    }
    return result
}