2016年12月6日 星期二

test

測試程式碼


package com.test.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

public class SimpleController {


 String home() {
 return "Hello Spring Boot 1.4.2";
 }

 public static void main(String[] args) throws Exception {
 SpringApplication.run(SimpleController.class, args);
 }
}


2016年2月21日 星期日

在 Google Blogger 使用 jQuery 範例

Bootstrap + jQuery + Font Awesome Icon Hosted Demo




方法:
先在範本 -> Header 結束之前,加入 jQuery, Bootstrap, font awesome 的宣告

進到範本,點編輯HTML


找到 </head> 在之前貼上下列

<!-- bootstrap hosted -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" 
integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" 
integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"
integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
<!-- jQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<!-- font awesome icon -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" />




接著就可以在文章內容寫 script 了





2016年2月9日 星期二

使用 POI 讀取範例檔動態產生 word 報告

讀取 DB 產生一個個 word 報告時,通常不太可能用 POI 真的去慢慢產出 word 元素 一般會透過一個範本檔 .doc,再替換掉裡面的文字,如下範例

此範例使用到兩個 jar 檔,到 Apache POI 官網下載最新的 POI-3.13 ZIP 檔裡就會有這兩個檔案。

目錄結構如下,範本檔直接放在範例程式下

 


範本檔 student_template.doc 如下,內容可隨自己編排





範例程式 WordReportDemo01.java:


package demo.poi.word;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URISyntaxException;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.CharacterRun;
import org.apache.poi.hwpf.usermodel.Range;

/**
 * 透過 word 範本與 POI 產生報告
 * 
 * @author Rocky
 */
public class WordReportDemo01 {

 public static void main(String[] args) throws Exception {
  
  new WordReportDemo01().execute();
 }
 
 private void execute() throws Exception {
  
  Student student1 = new Student("子瑜", 79, "做的非常好");
  Student student2 = new Student("小明", 89, "記得多運動");

  createWordFile(student1);
  createWordFile(student2);
 }
 
 /**
  * 讀取範本並產生新報告
  * 
  * @param student
  * @throws IOException 
  * @throws URISyntaxException 
  */
 private void createWordFile(Student student) throws Exception {

  File tempFile = new File(WordReportDemo01.class.getResource("student_template.doc").toURI());
  File destFile = new File("D:/" + student.getName() + ".doc");
  
  InputStream is = new FileInputStream(tempFile);
  HWPFDocument doc = new HWPFDocument(is);
  Range range = doc.getRange(); // poi-scratchpad.jar

  // 依條件動態更換文字的顏色
  CharacterRun charRun = null;
  for (int i=0; i< range.numCharacterRuns(); i++) {
   charRun = range.getCharacterRun(i);
   if (! student.getName().equals("子瑜")) {
    if (charRun.text().equals("${score}") || charRun.text().equals("${comment}")) {
     charRun.setColor(6); // 設定為紅色
    } 
   }
  }

  // 替換範本檔中的文字
  range.replaceText("${name}", student.getName());
  range.replaceText("${score}", student.getScore() + "");
  range.replaceText("${comment}", student.getComment());

  // 寫出到新的 word 檔
  OutputStream os = new FileOutputStream(destFile);
  doc.write(os);
  
  os.close();
  is.close();
 }
 
 /**
  * Student Bean
  */
 class Student {
  
  private String name;
  /** 分數 */
  private int score;
  /** 評語 */
  private String comment;
  
  public Student(String name, int score, String comment) {
   this.name = name;
   this.score = score;
   this.comment = comment;
  }
  
  public String getName() {
   return name;
  }
  public void setName(String name) {
   this.name = name;
  }
  public int getScore() {
   return score;
  }
  public void setScore(int score) {
   this.score = score;
  }
  public String getComment() {
   return comment;
  }
  public void setComment(String comment) {
   this.comment = comment;
  }
 }
}
執行後即可在 D 槽下找到兩個新增的 word 檔

2016年2月2日 星期二

使用 span 請注意


龜毛個性,通常我會習慣多餘的程式就拿掉,所以類似下面的

<h:inputText value="xxx"></h:inputText>

一定會被我拿掉後面變成

<h:inputText value="xxx" />

但今天發現莫名的 Bug,原因竟然是用到 span 時,千萬別用這種簡潔的方式

<span class="xxx" />
<a href="yyy" />

我以為 a tag 應該是獨立出來的,但他被包進 span 裡了!!

所以一定要寫成底下這樣才行

<span class="xxx"></span>
<a href="yyy" />

2016年1月31日 星期日

jQuery 欄位自動轉大寫完美解法


寫程式一定會遇到該死的欄位預設要幫使用者轉大寫,底下是使用 jQuery 完美的解法


$("#name, #address").css("text-transform", "uppercase").change(function(){
$(this).val($(this).val().toUpperCase());
});

name 與 address 是你欄位的名稱,只要一行,就可以一次讓多個欄位預設轉大寫

切記,切萬不要再用什麼鬼 keyup 事件再搭配 toUpperCase,因為…
當使用者用滑鼠從別的地方複制文字,再過來用滑鼠貼上,你就 GG 回家吃自己了

而且,如果你用 keyup 事件再 toUpperCase,會發現,想在文字的中間打段文字,滑鼠會一直鬼撞牆的移到最後!


原理其實就是幫 input 加上 style,比如
<input id="address" value="" style="text-transform:uppercase"/>

然後在 change 事件後再轉大寫,因為 style 只有看起來是大寫,必須自己轉大寫。