Programming     Travel Logs     Life Is Good     Surfing Online     About Me
Set and enforce an aspirational personal hourly rate. If fixing a problem will save less than your hourly rate, ignore it. If outsourcing a task will cost less than your hourly rate, outsource it.
-Naval Ravikant
2018-07-22 22:43:18

Copy this link when reproducing:

From today on, I'm going to develop a Java web site to replace my current personal web site. It'll probably involve the following techniques:

  • Java Spring MVC
  • MySQL
  • Bootstrap
  • Wechat4j

Just like before, let's take it easy and enjoy a few beautiful photos first.

To setup the development environment, I only need to follow the steps which I used to create the Today In History application. Please check the following links for the details:


Now, let me ignore the complex details and just list the specific changes for my personal web site.

1. "WebContent\WEB-INF\web.xml":

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xsi:schemaLocation="" id="WebApp_ID" version="3.1">

2. "WebContent\WEB-INF\config\springmvc-config.xml":

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
    xsi:schemaLocation="            ">
    <!-- Indicate to scan all controllers in the base-package -->
    <context:component-scan base-package=""/>
    <context:component-scan base-package=""/>
    <tx:annotation-driven transaction-manager="transactionManager" />
    <!-- Indicate these are static resources -->
    <mvc:resources mapping="/css/**" location="/css/"/>
    <mvc:resources mapping="/js/**" location="/js/"/>
    <mvc:resources mapping="/*.html" location="/"/>
    <mvc:resources mapping="/Images/**" location="/Images/"/>
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- With prefix and suffix, we can refer to the view name itself -->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
      <property name="driverClassName" value="com.mysql.jdbc.Driver" />
      <property name="url" value="jdbc:mysql://localhost:3306/BlogDB?autoReconnect=true&useSSL=false&useUnicode=true&characterEncoding=UTF-8" />
      <property name="username" value="root" />
      <property name="password" value="password" />
    <bean id="hibernateAnnotatedSessionFactory"
      <property name="dataSource" ref="dataSource" />
      <property name="annotatedClasses">
      <property name="hibernateProperties">
          <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
          <prop key="hibernate.current_session_context_class">thread</prop> 
          <prop key="hibernate.show_sql">false</prop>
    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
      <property name="sessionFactory" ref="hibernateAnnotatedSessionFactory"></property>
    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
      <property name="sessionFactory" ref="hibernateAnnotatedSessionFactory"></property>

3. For convenience, I copied all .jar files from my Today In History project:

4. "src\com\casperlee\blog\controller\":


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;


public class BlogController {

	private SiteService siteService;
	@RequestMapping(value = "/")
	public String index(Model model) {
		model.addAttribute(CConstants.IMAGE, siteService.getSiteConfig(CConstants.IMAGE));
		model.addAttribute(CConstants.OWNER, siteService.getSiteConfig(CConstants.OWNER));
		model.addAttribute(CConstants.MOTTO, siteService.getSiteConfig(CConstants.MOTTO));
	    return "Index";

5. "src\com\casperlee\blog\domain\":



import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

public class SiteConfig implements Serializable {

	private static final long serialVersionUID = 10000001L;

	private int id;
	private String key;
	private String value;

	public int getId() {
		return id;

	public void setId(int id) { = id;

	public String getKey() {
		return key;

	public void setKey(String key) {
		this.key = key;

	public String getValue() {
		return value;

	public void setValue(String value) {
		this.value = value;
	public String toString() {
		return this.getKey() + "=" + this.getValue();

6. "src\com\casperlee\blog\global\":


public class CConstants {

	// for main page
	public static final String OWNER = "OWNER"; 
	public static final String MOTTO = "MOTTO"; 
	public static final String IMAGE = "IMAGE"; 

7. "src\com\casperlee\blog\service\":


import org.hibernate.Session;
import org.springframework.orm.hibernate5.HibernateTemplate;

public class HibernateTemplateHelper {

	private static Session session = null;
	public static Session getSession(HibernateTemplate aTemplate) {
		if (session != null) {
		session = aTemplate.getSessionFactory().openSession();
		return session;

8. "src\com\casperlee\blog\service\":


public interface SiteService {

	public String getSiteConfig(String aKey);

9. "src\com\casperlee\blog\service\":


import java.util.List;

import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


public class SiteServiceImpl implements SiteService {

	private HibernateTemplate template;

	public String getSiteConfig(String aKey) {
		String hql = "from SiteConfig " + "where key = :key ";
		Query<SiteConfig> query = HibernateTemplateHelper.getSession(template).createQuery(
				hql, SiteConfig.class);
		query.setParameter("key", aKey);
		List<SiteConfig> results = query.list();
		if (results.size() > 0) {
			return results.get(0).getValue();
		return "";

10. "WebContent\WEB-INF\jsp\Index.jsp":

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="form" uri="" %>
<%@ taglib prefix="c" uri="" %>
<html lang="zh-CN">
<title>Insert title here</title>
    <img alt="" src="${pageContext.request.contextPath}/Images/${IMAGE}">

11. The SQL scripts for the MySQL database:

create database BlogDB;
use BlogDB;

create table SiteConfig (
scId int(4) primary key not null auto_increment,
scKey varchar(20) not null,
scValue varchar(2000) null
alter table SiteConfig convert to character set utf8;
alter table SiteConfig add unique (scKey);

insert into SiteConfig(scKey, scValue) values ("OWNER", "Casper Lee");
insert into SiteConfig(scKey, scValue) values ("MOTTO", "When you affirm big, believe big and pray big, putting faith into action, big things happen.");
insert into SiteConfig(scKey, scValue) values ("IMAGE", "Icons/site.png");

12. Done!