廣告聯播

2019年12月3日 星期二

WordPress 安裝與網站基本設定

From: Polin Wei

WordPress WordPress是一個以PHP和MySQL為平臺的自由開源的 Blog 部落格軟體和內容管理系統。WordPress具有外掛程式架構和範本系統,更可以透過 HOOK 的規範加入自己的客製程式。截至2018年4月,排名前1000萬的網站中超過30.6%使用WordPress。WordPress是最受歡迎的網站內容管理系統。很多虛擬主機或雲端網站都有提供免費的 WordPress 佈建軟體供自行建置屬於自己的部落格,本站則是先個人電腦安裝並設置後才上載到雲端網站,整個WordPressordPress建站的過程如下:
  1. 設定 DNS 與安裝 WordPress
  2. WordPress 網站基本的設定
  3. WordPress 佈景主題安裝與設定
  4. WordPress 實用外掛推薦
  5. WordPress 網站狀態檢查
  • 設定 DNS 與安裝 WordPress
若想要建置一個專屬自己的部落格,雖然可以在 Google 的 Blogspot 如: 我的工作日記 或是 痞客邦 如: MISTECH 技術手抄本 中申請免費的空間,但免費的資源總是會有限制,因此, 若想要一個完全可以屬於自己可以操控的網站,那麼就必需購買一個網域及一個主機。

2019年12月1日 星期日

WordPress Useful Plugin – 實用外掛推薦

From: Polin Wei

WordPress Useful Plugin – 實用外掛推薦

WordPress 分成 wordpress.org 與 wordpress.com  兩種 ,本站所介紹的主要的是 wordpress.org, WordPress 的優點就是他的自由度高。即使本體其實是Blog,但也能夠利用各主題或是外掛 Plugins 來讓它搖身一變成為企業網站或是網路商店等。以下整理了本站使用的及各功能實用的WordPress常用外掛,推薦 WordPress 的站長們使用。
實用的 WordPress 常用外掛以下列幾種來作區分介紹:
  1. 網站防護
  2. 網站優化
  3. 文章編輯
  4. 後台管理
  • 網站防護
Jetpack: WordPress.com官方所出的多功能型外掛,它是你網站的安全護衛,可確保你不會遭受暴力破解攻擊及未經授權的登入。基本的防護一律免費,etpack擁有各式各樣的免費功能,例如:網站流量統計分析、發文後自動分享至社群網站、文章分享按鈕、相關文章列表、暴力破解防護、主機停機監控、圖片用CDN、隨選圖庫、各式側邊欄小工具、使用社群網站帳號登入留言等……進階級方案則會加入擴充的備份以及自動化修正。
Akismet Anti-Spam : 它是保護網站遠離垃圾留言的絕佳方式,並由 Akismet 提供不間斷的保護。Akismet 使用全球垃圾留言資料庫,為網站上的留言及聯絡表單內容進行檢查,保護網站免受惡意內容侵害。網站管理員可以在網站的 [留言] 管理頁面中,檢視已遭 Akismet 攔截的垃圾留言。


繼續閱讀: WordPress Useful Plugin – 實用外掛推薦


2018年12月14日 星期五

Powerful Spring Boot Admin

From: Polin Wei
利用 Spring Boot 可以快速開發客製化應用系統, 而這些由 Spring Boot 框架建立的應用系統則可以利用 Spring Boot Admin 來作統一的管理. 由這系統可以讓您知道 Application Server 的版本如: Java , Tomcat , session .... 等, 所以在管理這些服務系統架構中...監控管理是非常重要的一環

Server 端

1. 建立一個新的 Spring Starter Project : sbAdmin , 且只要選擇 Web 就好. 其它的相依套件如下:
buildscript {
 ext {
  springBootVersion = '2.1.1.RELEASE'
 }
 repositories {
  mavenCentral()
 }
 dependencies {
  classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
 }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'com.admin'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
 mavenCentral()
}


dependencies {
 implementation('org.springframework.boot:spring-boot-starter-web')
 compile('de.codecentric:spring-boot-admin-starter-server:2.1.1') // SpringBoot Admin
 testImplementation('org.springframework.boot:spring-boot-starter-test')
}
Server 端的 application.properties 設定啟動的 port: 8099

server.port=8099

Client 端

引入的相依套件
dependencies {
 compile('de.codecentric:spring-boot-admin-starter-client:2.1.1') // SpringBoot Admin Client
}

設定Spring Boot Admin 可以存取 Client 端的設定

(繼續閱讀...)

Develop custom taglib using Freemarker in Spring Boot

From: Polin Wei
在 Spring Boot 使用 Freemarker 模板來快速建立客製化的標籤 (taglib) 是非常簡單的事. 作下列幾個步驟即可.


public class FtlTemplateTag extends TagSupport {

 private static final long serialVersionUID = 1L;
 
 private String fileName = null;
    private String paramsStr = null;
    private String columnsStr = null;
    private ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
    
 public void setFileName(String fileName) {
  this.fileName = fileName;
 }    
 public void setParamsStr(String paramsStr) {
  this.paramsStr = paramsStr;
 }
 public void setColumnsStr(String columnsStr) {
  this.columnsStr = columnsStr;
 }
 /**
  * JSON 字串轉換成 MAP
  * @return
  */
 private Map setParams() {
  Gson gson = new Gson();
  return gson.fromJson(paramsStr, new TypeToken>() {
  }.getType());
 }
 
 private Map setColumns(){
  Locale locale = LocaleContextHolder.getLocale();
  
  messageSource.setBasenames("i18n/messages");
  
  Gson gson = new Gson();
  Map columns = gson.fromJson(columnsStr, new TypeToken>() {}.getType() );
  
  //轉換多語系
  Map dataModel = new LinkedHashMap();
  columns.forEach( (k,v)-> dataModel.put(k, messageSource.getMessage(v, null, v, locale)) );
   
  return dataModel;
 }
 
 
 @Override
 public int doStartTag() throws JspException {
  JspWriter out = pageContext.getOut();
  
  Configuration conf = new Configuration();
  conf.setClassForTemplateLoading(this.getClass(), "/templates/");
  conf.setDefaultEncoding("UTF-8");
  
  try {
   Template  tl = conf.getTemplate(fileName);
   Map dataModel = new HashMap();
   dataModel.putAll(this.setParams());
   dataModel.put("columns", this.setColumns());
   

   tl.process(dataModel, out);
   
  } catch (Exception e) {
   e.printStackTrace();
  }  
  return Tag.SKIP_BODY;
 }
}

<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
        version="2.0">
    <description>Polin WEI Tag Library</description>

    <tlib-version>1.0</tlib-version>
    <short-name>pw</short-name>
    <uri>/polinwei/tags</uri>

    <tag>
        <description>Get the file name of freemarker template</description>
        <name>FtlSampleTemplate</name>
        <tag-class>com.spring.jwt.tablibs.FtlTemplateTag</tag-class>
        <body-content>empty</body-content>
        <attribute>
            <description> Freemarker Template 的檔案</description>
            <name>fileName</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>parmsStr</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>columnsStr</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
</taglib>


(繼續閱讀...)

2018年12月7日 星期五

[Solved] DocumentBuilder parse XML returns null <解決 org.w3c.dom.Document 解析 XML 檔回傳值均為 NULL>

From: Polin Wei Document 解析 XML 檔案時, 解析的值總是為 NULL , 解決方法如下: 先把檔案用 StringBuilder 變成字串後, 再作解析就可以

import org.springframework.util.ResourceUtils;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;

public class TestFunc {

        public static void main(String[] args) throws Exception  {
                // TODO Auto-generated method stub
                try {
                        File xmlFile = ResourceUtils.getFile("classpath:ckfinder-config.xml");
                        readMXLFile(xmlFile);
                        
                } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
        }
        
        public static void readMXLFile(File xmlFile) throws Exception {
                InputStream is = new FileInputStream(xmlFile);
                StringBuilder contentBuilder = new StringBuilder();
                try (Stream stream = Files.lines(Paths.get(xmlFile.getAbsolutePath()), StandardCharsets.UTF_8)) {
                        stream.forEach(s -> contentBuilder.append(s).append("\n"));
                } catch (IOException e) {
                        e.printStackTrace();
                }
                System.out.println(contentBuilder.toString());

                ByteArrayInputStream bis = new ByteArrayInputStream(contentBuilder.toString().getBytes());

繼續閱讀

2018年9月28日 星期五

Spring Boot Validating Form Input with i18n and FreeMarker

From: Polin Wei

build.gradle
buildscript {
 ext {
  springBootVersion = '2.0.4.RELEASE'
  jjwtVersion = '0.9.0'
  findbugsVersion='3.0.1'
  bootstrapVersion = '3.3.7'
  jqueryVersion = '3.3.1'
  vueVersion ='2.5.13'
  fontawesomeVersion = '5.2.0'
  jspapiVersion = '2.3.3'
 }
 repositories {
  mavenCentral()
 }
 dependencies {
  classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
 }
}

/*
* 在這個段落中你可以聲明使用哪些外掛程式
* apply plugin: 'java' 代表這是一個Java專案,需要使用java外掛程式
* 如果想生成一個 `Intellij IDEA` 的工程,類似的如果要生成
* eclipse工程,就寫 apply plugin: 'eclipse'
* 同樣的我們要學的是Spring Boot,所以應用Spring Boot外掛程式
*/
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

// 在這個段落中你可以聲明編譯後的Jar檔資訊
bootJar {
 baseName = 'myspring'
 group = 'com.polinwei'
 version = '0.0.1-SNAPSHOT'
}

// 在這個段落中你可以聲明原始檔案和目標編譯後的Java版本相容性
sourceCompatibility = 1.8
targetCompatibility = 1.8

// 在這個段落中你可以聲明在哪裡可以找到你的項目依賴
repositories {
 mavenCentral()
 maven { url "https://repo.spring.io/snapshot" }
 maven { url "https://repo.spring.io/milestone" }
 maven { url "https://code.lds.org/nexus/content/groups/main-repo"}
 maven { url "http://maven.aliyun.com/nexus/content/repositories/central"}
}


dependencies { 
 compile("org.springframework.boot:spring-boot-starter-data-jpa")
 compile("org.springframework.boot:spring-boot-starter-thymeleaf")
 compile("org.springframework.boot:spring-boot-starter-freemarker")
 compile("org.springframework.boot:spring-boot-starter-web")
 compile("org.springframework.boot:spring-boot-starter-security")
 compile("org.springframework.security:spring-security-taglibs")
 compile("org.springframework.boot:spring-boot-devtools") // Class 程式有更改時, 自動重啟
 compile("org.hibernate.validator:hibernate-validator") //驗證
 compile("javax.servlet.jsp:javax.servlet.jsp-api:${jspapiVersion}")
 compile("org.springframework.session:spring-session-data-redis")
 compile("org.springframework.boot:spring-boot-starter-data-redis")
 runtime("mysql:mysql-connector-java")
 compileOnly("org.projectlombok:lombok")
 compile("com.maxmind.geoip2:geoip2:2.12.0")
 compile("io.jsonwebtoken:jjwt:${jjwtVersion}")
 compile("com.google.code.findbugs:findbugs:${findbugsVersion}")
 compile("org.webjars:bootstrap:${bootstrapVersion}")
 compile("org.webjars:jquery:${jqueryVersion}")
 compile("org.webjars:vue:${vueVersion}")
 compile("org.webjars:font-awesome:${fontawesomeVersion}")
 compile("org.webjars.bowergithub.lipis:flag-icon-css:3.1.0")
 compileOnly("org.springframework.boot:spring-boot-configuration-processor")
 testCompile("org.springframework.boot:spring-boot-starter-test")
 testCompile("org.springframework.security:spring-security-test")
}
model: Authority.java
/**
 * Authority generated by hbm2java
 */
@Entity
@Table(name = "authority", uniqueConstraints = @UniqueConstraint(columnNames = "name"))
public class Authority implements java.io.Serializable {

 private Long id;
 private String name;
 @Size(min=2, max=30 , message = "{Size}")
 private String description;
 private Set users = new HashSet(0);

 public Authority() {
 }

 public Authority(String name, String description) {
  this.name = name;
  this.description = description;
 }

 public Authority(String name, String description, Set users) {
  this.name = name;
  this.description = description;
  this.users = users;
 }

 @Id
 @GeneratedValue(strategy = IDENTITY)

 @Column(name = "id", unique = true, nullable = false)
 public Long getId() {
  return this.id;
 }

 public void setId(Long id) {
  this.id = id;
 }

 @Column(name = "name", unique = true, nullable = false, length = 50)
 public String getName() {
  return this.name;
 }

 public void setName(String name) {
  this.name = name;
 }

 @Column(name = "description", nullable = false, length = 100)
 public String getDescription() {
  return this.description;
 }

 public void setDescription(String description) {
  this.description = description;
 }

 @ManyToMany(fetch = FetchType.LAZY)
 @JoinTable(name = "user_authority", joinColumns = {
   @JoinColumn(name = "authority_id", nullable = false, updatable = false) }, inverseJoinColumns = {
     @JoinColumn(name = "user_id", nullable = false, updatable = false) })
 @JsonBackReference
 public Set getUsers() {
  return this.users;
 }

 public void setUsers(Set users) {
  this.users = users;
 }

}
Control: AuthorityController.java
@Controller
@RequestMapping(path = "/security")
public class AuthorityController {

 private final Logger logger = LoggerFactory.getLogger(this.getClass());
 
 @Autowired
 private AuthorityRepository authorityRepository;
 
 public void init(Model model) {
  model.addAttribute("programName", "Authority");
 }
 
 @RequestMapping("authority")
 public String crudAuthority(Model model) {
  init(model);
  model.addAttribute("authority", new Authority());
  model.addAttribute("authorityList",authorityRepository.findAll());  
  return "/security/authority";
 }
 
 /**
  * Get Authority Role from id
  * @param model
  * @param id
  * @return
  */
 @RequestMapping(value= {"authorityEdit","authorityEdit/{id}"} , method = RequestMethod.GET)
 public String crudAuthority(Model model, @PathVariable( required = false , name="id") Long id) {
  init(model);
  if (Objects.isNull(id)) {
   model.addAttribute("authority", new Authority());
  } else {
   model.addAttribute("authority", authorityRepository.findById(id).get());
  }  
  return "/security/authority";
 }
 
 /**
  * Save Authority Role
  * @param model
  * @param authority
  * @return
  */
 @RequestMapping(value="authorityEdit" , method = RequestMethod.POST)
 public String crudAuthority(Model model, @Valid Authority authority, BindingResult bindingResult) {
  init(model);
  if (bindingResult.hasErrors()) {
   return "/security/authority";
  }
   
  authorityRepository.save(authority);
  model.addAttribute("authority", new Authority());
  return "/security/authority";
 }
 
 /**
  * Delete Authority Role
  * @param model
  * @param id
  * @return
  */
 @RequestMapping(value="authorityDelete/{id}" , method = RequestMethod.GET)
 public String authorityDelete(Model model, @PathVariable( required = true, name = "id") Long id) {
  init(model);
  authorityRepository.deleteById(id);
  model.addAttribute("authority", new Authority());
  return "/security/authority";
 }
 
}
加入了@Valid注解 是為了實現JSR-303的驗證。

(繼續閱讀...)

2018年9月18日 星期二

pdfmake 實現中文字支援,解決中文亂碼問題,以 DataTables 為例

From: Polin Wei
實現pdfmake使用中文本體主要就是編譯新的vfs_fonts.js代替原來vfs_fonts.js文檔引入到前端頁面中,為了編譯出新的字體文檔,下列是中文顯示的解決方法,供大家參考:
操作系統:Windows
操作步驟:
1. 安裝node.js
2. 下載pdfmake的源代碼、下載地址https://github.com/bpampuch/pdfmake
3. 在源代碼根目錄下安裝gulp:
打開cmd命令窗口,定位到源代碼根目錄,如:cd V:\pdfmake-master

npm install gulp
npm install -g gulp
npm i -g gulp-cli
執行gulp -v顯示gulp版本號則説明安裝成功
4. 利用gulp打包字體ttf文檔到vfs_fonts.js文檔中
gulp安裝成功後,查看源代碼根目錄下的gulpfile.js,發現gulp編譯還依賴了很多其他的模塊,所以需要一一安裝,沒辦法所以得一一安裝:npm install webpack-stream、npm install gulp-uglify 等等

V:\pdfmake-master>npm i -g gulp-cli

V:\node-v8.12.0-win-x64\gulp -> V:\node-v8.12.0-win-x64\node_modules\gulp-cli\bin\gulp.js

+ gulp-cli@2.0.1

added 235 packages from 147 contributors in 6.71s

V:\pdfmake-master>gulp buildFonts

[13:48:16] Using gulpfile V:\pdfmake-master\gulpfile.js

[13:48:16] Starting 'buildFonts'...

[13:48:17] Finished 'buildFonts' after 382 ms

從本地 C:\Windows\Fonts 下拷貝一箇中文本體到D:\download\chrome\pdfmake-master\examples\fonts目錄下,並刪除fonts目錄下原來的ttf文檔,這裏有一個問題,中文本體都很大,囧。
請選了一個最小的字體上:標楷體-標準 ,這一步完成之後,字體打包生成的vfs_fonts.js會覆蓋源代碼根目錄的build目錄下的vfs_fonts.js文檔,這時需要將vfs_fonts.js拷貝到你的項目當中替換原來的vfs_fonts.js,並引用的html頁面中

(繼續閱讀...)

WordPress 架站必備外掛