Programming     Travel Logs     Life Is Good     Surfing Online     About Me
Learn to sell. Learn to build. If you can do both, you will be unstoppable.
-Naval Ravikant
2018-06-16 22:44:06

Copy this link when reproducing:
http://www.casperlee.com/en/y/blog/184

Time to implement and finish the management function now. It is not very complex, so I'll just post codes here without explaining too much.

/Images/20170921/01.jpg

/Images/20170921/02.jpg

/Images/20170921/03.jpg

/Images/20170921/04.jpg

/Images/20170921/05.jpg

/Images/20170921/06.jpg

1. Add a folder named "js" in the directory "WebContent", and create a file named "common.js" in the "js" folder we've just created.

Here is the content of the file "common.js":

/**
 * 
 */

function del () {
	
	var msg = "您真的要删除此项么?\n\n请三思!";
	if (confirm(msg) == true) {
		
		return true;
		
	} else {
		
		return false;
	}
}

2. Open the configuration file "WebContent\WEB-INF\config\springmvc-config.xml", and put the following text in:

<mvc:resources mapping="/js/**" location="/js/"/>

3. Open the file "WebContent\css\main.css", and put the following text in:

.textarea-large {

	width: 400px;
	height: 100px;
}

.textbox-long {
	
	width: 400px;
}

.textbox-med {
	
	width: 160px;
}

.textbox-short {
	
	width: 80px;
}

4. Create a package named "com.casperlee.today.form", and create a class named "DaySelect.java" in the package just created. Here is the content of the class:

package com.casperlee.today.form;

public class DaySelect {

	private String key;

	public String getKey() {
		return key;
	}

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

5. Modify the controller class "com.casperlee.today.controller.DayController":

package com.casperlee.today.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

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

import com.casperlee.today.domain.Day;
import com.casperlee.today.domain.DayBirth;
import com.casperlee.today.domain.DayDeath;
import com.casperlee.today.domain.DayEvent;
import com.casperlee.today.domain.DayFestival;
import com.casperlee.today.form.DaySelect;
import com.casperlee.today.service.DayService;

@Controller
public class DayController {

	@Autowired
    private DayService dayService;
	
	@RequestMapping(value = "/")
	public String index(Model model) {
		
	    Date today = new Date();
	    SimpleDateFormat format = new SimpleDateFormat("MMdd");
	    String key = format.format(today);
	    return "redirect:/day/" + key;
	}
	
	@RequestMapping(value = "/day/{key}")
	public String viewDay(@PathVariable String key, Model model) {
		
		Day day = dayService.getByKey(key);
	    if (day == null) {
	    	
	    	return "DayNotFound";
	    }
	    else {
	    	
	    	model.addAttribute("adminMode", false);
		    model.addAttribute(day);
	        return "DayForm";
	    }
	}
	
	@RequestMapping(value = "/admin/day", method=RequestMethod.GET)
	public String daySelect(Model model) {
		
		model.addAttribute(new DaySelect());
		return "DaySelectForm";
	}
	
	@RequestMapping(value = "/admin/day", method=RequestMethod.POST)
	public String daySelect(@ModelAttribute DaySelect day) {
		
	    return "redirect:/admin/day/" + day.getKey();
	}
	
	@RequestMapping(value = "/admin/day/{key}")
	public String dayManage(@PathVariable String key, Model model) {
		
		Day day = dayService.getByKey(key);
	    if (day == null) {
	    	
	    	day = new Day();
	    	day.setId(0);
	    	day.setKey(key);
	    	model.addAttribute(day);
	    	return "redirect:/admin/day/edit/" + key;
	    }
	    
	    model.addAttribute("adminMode", true);
        model.addAttribute(day);
	    return "DayForm";
	}
	
	@RequestMapping(value = "/admin/day/edit/{key}", method=RequestMethod.GET)
	public String dayEdit(@PathVariable String key, Model model) {
		
		Day day = dayService.getByKey(key);
	    if (day == null) {
	    	
	    	day = new Day();
	    	day.setId(0);
	    	day.setKey(key);
	    }
	    
	    model.addAttribute(day);
	    return "DayEditForm";
	}
	
	@RequestMapping(value = "/admin/day/edit/{key}", method=RequestMethod.POST)
	public String dayEdit(@PathVariable String key, @ModelAttribute Day day) {
		
		if (day.getId() == 0) {
			
			dayService.add(day);
			
		} else {
		
			dayService.update(day);
		}
		
		return "redirect:/admin/day/" + key;
	}
	
	@RequestMapping(value = "/admin/dayevent/add/{key}", method=RequestMethod.GET)
	public String dayEventAdd(@PathVariable String key, Model model) {
		
		Day day = dayService.getByKey(key);
		if (day == null) {
			
			return "redirect:/admin/day/" + key;
		}
		
        DayEvent dayEvent = new DayEvent(); 
        dayEvent.setId(0);
        dayEvent.setDay(day);
        
        model.addAttribute(day);
        model.addAttribute(dayEvent);
	    return "DayEventEditForm";
	}
	
	@RequestMapping(value = "/admin/dayevent/add/{key}", method=RequestMethod.POST)
	public String dayEventAdd(@PathVariable String key,
			@ModelAttribute DayEvent dayEvent) {
		
		dayService.addEvent(dayEvent);
		return "redirect:/admin/day/" + key;
	}

	@RequestMapping(value = "/admin/dayevent/edit/{id}", method=RequestMethod.GET)
	public String dayEventEdit(@PathVariable int id, Model model) {
		
		DayEvent dayEvent = dayService.getEventById(id);
        model.addAttribute(dayEvent);
	    return "DayEventEditForm";
	}
	
	@RequestMapping(value = "/admin/dayevent/edit/{id}", method=RequestMethod.POST)
	public String dayEventEdit(@PathVariable int id,
			@ModelAttribute DayEvent dayEvent) {
		
		String key = dayEvent.getDay().getKey();
		dayService.updateEvent(dayEvent);
		return "redirect:/admin/day/" + key;
	}
	
	@RequestMapping(value = "/admin/dayevent/delete/{id}", method=RequestMethod.GET)
	public String dayEventDelete(@PathVariable int id, Model model) {
		
		DayEvent dayEvent = dayService.getEventById(id);
		String key = dayEvent.getDay().getKey();
        dayService.deleteEvent(dayEvent);
        return "redirect:/admin/day/" + key;
	}
	
	@RequestMapping(value = "/admin/daybirth/add/{key}", method=RequestMethod.GET)
	public String dayBirthAdd(@PathVariable String key, Model model) {
		
		Day day = dayService.getByKey(key);
		if (day == null) {
			
			return "redirect:/admin/day/" + key;
		}
		
        DayBirth dayBirth = new DayBirth(); 
        dayBirth.setId(0);
        dayBirth.setDay(day);
        
        model.addAttribute(day);
        model.addAttribute(dayBirth);
	    return "DayBirthEditForm";
	}
	
	@RequestMapping(value = "/admin/daybirth/add/{key}", method=RequestMethod.POST)
	public String dayBirthAdd(@PathVariable String key,
			@ModelAttribute DayBirth dayBirth) {
		
		dayService.addBirth(dayBirth);
		return "redirect:/admin/day/" + key;
	}

	@RequestMapping(value = "/admin/daybirth/edit/{id}", method=RequestMethod.GET)
	public String dayBirthEdit(@PathVariable int id, Model model) {
		
		DayBirth dayBirth = dayService.getBirthById(id);
        model.addAttribute(dayBirth);
	    return "DayBirthEditForm";
	}
	
	@RequestMapping(value = "/admin/daybirth/edit/{id}", method=RequestMethod.POST)
	public String dayBirthEdit(@PathVariable int id,
			@ModelAttribute DayBirth dayBirth) {
		
		String key = dayBirth.getDay().getKey();
		dayService.updateBirth(dayBirth);
		return "redirect:/admin/day/" + key;
	}
	
	@RequestMapping(value = "/admin/daybirth/delete/{id}", method=RequestMethod.GET)
	public String dayBirthDelete(@PathVariable int id, Model model) {
		
		DayBirth dayBirth = dayService.getBirthById(id);
		String key = dayBirth.getDay().getKey();
        dayService.deleteBirth(dayBirth);
        return "redirect:/admin/day/" + key;
	}
	
	@RequestMapping(value = "/admin/daydeath/add/{key}", method=RequestMethod.GET)
	public String dayDeathAdd(@PathVariable String key, Model model) {
		
		Day day = dayService.getByKey(key);
		if (day == null) {
			
			return "redirect:/admin/day/" + key;
		}
		
        DayDeath dayDeath = new DayDeath(); 
        dayDeath.setId(0);
        dayDeath.setDay(day);
        
        model.addAttribute(day);
        model.addAttribute(dayDeath);
	    return "DayDeathEditForm";
	}
	
	@RequestMapping(value = "/admin/daydeath/add/{key}", method=RequestMethod.POST)
	public String dayDeathAdd(@PathVariable String key,
			@ModelAttribute DayDeath dayDeath) {
		
		dayService.addDeath(dayDeath);
		return "redirect:/admin/day/" + key;
	}

	@RequestMapping(value = "/admin/daydeath/edit/{id}", method=RequestMethod.GET)
	public String dayDeathEdit(@PathVariable int id, Model model) {
		
		DayDeath dayDeath = dayService.getDeathById(id);
        model.addAttribute(dayDeath);
	    return "DayDeathEditForm";
	}
	
	@RequestMapping(value = "/admin/daydeath/edit/{id}", method=RequestMethod.POST)
	public String dayDeathEdit(@PathVariable int id,
			@ModelAttribute DayDeath dayDeath) {
		
		String key = dayDeath.getDay().getKey();
		dayService.updateDeath(dayDeath);
		return "redirect:/admin/day/" + key;
	}
	
	@RequestMapping(value = "/admin/daydeath/delete/{id}", method=RequestMethod.GET)
	public String dayDeathDelete(@PathVariable int id, Model model) {
		
		DayDeath dayDeath = dayService.getDeathById(id);
		String key = dayDeath.getDay().getKey();
        dayService.deleteDeath(dayDeath);
        return "redirect:/admin/day/" + key;
	}
	
	@RequestMapping(value = "/admin/dayfestival/add/{key}", method=RequestMethod.GET)
	public String dayFestivalAdd(@PathVariable String key, Model model) {
		
		Day day = dayService.getByKey(key);
		if (day == null) {
			
			return "redirect:/admin/day/" + key;
		}
		
        DayFestival dayFestival = new DayFestival(); 
        dayFestival.setId(0);
        dayFestival.setDay(day);
        
        model.addAttribute(day);
        model.addAttribute(dayFestival);
	    return "DayFestivalEditForm";
	}
	
	@RequestMapping(value = "/admin/dayfestival/add/{key}", method=RequestMethod.POST)
	public String dayFestivalAdd(@PathVariable String key,
			@ModelAttribute DayFestival dayFestival) {
		
		dayService.addFestival(dayFestival);
		return "redirect:/admin/day/" + key;
	}

	@RequestMapping(value = "/admin/dayfestival/edit/{id}", method=RequestMethod.GET)
	public String dayFestivalEdit(@PathVariable int id, Model model) {
		
		DayFestival dayFestival = dayService.getFestivalById(id);
        model.addAttribute(dayFestival);
	    return "DayFestivalEditForm";
	}
	
	@RequestMapping(value = "/admin/dayfestival/edit/{id}", method=RequestMethod.POST)
	public String dayFestivalEdit(@PathVariable int id,
			@ModelAttribute DayFestival dayFestival) {
		
		String key = dayFestival.getDay().getKey();
		dayService.updateFestival(dayFestival);
		return "redirect:/admin/day/" + key;
	}
	
	@RequestMapping(value = "/admin/dayfestival/delete/{id}", method=RequestMethod.GET)
	public String dayFestivalDelete(@PathVariable int id, Model model) {
		
		DayFestival dayFestival = dayService.getFestivalById(id);
		String key = dayFestival.getDay().getKey();
        dayService.deleteFestival(dayFestival);
        return "redirect:/admin/day/" + key;
	}
}

6. Modify the interface "com.casperlee.today.service.DayService":

package com.casperlee.today.service;

import com.casperlee.today.domain.Day;
import com.casperlee.today.domain.DayBirth;
import com.casperlee.today.domain.DayDeath;
import com.casperlee.today.domain.DayEvent;
import com.casperlee.today.domain.DayFestival;

public interface DayService {

	Day getByKey(String aKey);
	DayEvent getEventById(int aId);
	DayBirth getBirthById(int aId);
	DayDeath getDeathById(int aId);
	DayFestival getFestivalById(int aId);
	
	void add(Day aDay);
	void update(Day aDay);
	void delete(Day aDay);
	
	void addEvent(DayEvent aDayEvent);
	void updateEvent(DayEvent aDayEvent);
	void deleteEvent(DayEvent aDayEvent);
	
	void addBirth(DayBirth aDayBirth);
	void updateBirth(DayBirth aDayBirth);
	void deleteBirth(DayBirth aDayBirth);
	
	void addDeath(DayDeath aDayDeath);
	void updateDeath(DayDeath aDayDeath);
	void deleteDeath(DayDeath aDayDeath);
	
	void addFestival(DayFestival aDayFestival);
	void updateFestival(DayFestival aDayFestival);
	void deleteFestival(DayFestival aDayFestival);
}

7. Modify the class "com.casperlee.today.service.DayServiceImpl.java":

package com.casperlee.today.service;

import org.hibernate.Session;
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;

import com.casperlee.today.domain.Day;
import com.casperlee.today.domain.DayBirth;
import com.casperlee.today.domain.DayDeath;
import com.casperlee.today.domain.DayEvent;
import com.casperlee.today.domain.DayFestival;

@Service
@Transactional
public class DayServiceImpl implements DayService {
	
	@Autowired
	private HibernateTemplate template;

	@Override
	public Day getByKey(String aKey) {

		Session session = template.getSessionFactory().openSession();
		Query<Day> query = session.createQuery("from Day where key = :keyName", Day.class);
		query.setParameter("keyName", aKey);
		return query.uniqueResult();
	}

	@Override
	public DayEvent getEventById(int aId) {
		
		Session session = template.getSessionFactory().openSession();
		Query<DayEvent> query = session.createQuery("from DayEvent where id = :id", DayEvent.class);
		query.setParameter("id", aId);
		return query.uniqueResult();
	}

	@Override
	public DayBirth getBirthById(int aId) {

		Session session = template.getSessionFactory().openSession();
		Query<DayBirth> query = session.createQuery("from DayBirth where id = :id", DayBirth.class);
		query.setParameter("id", aId);
		return query.uniqueResult();
	}

	@Override
	public DayDeath getDeathById(int aId) {

		Session session = template.getSessionFactory().openSession();
		Query<DayDeath> query = session.createQuery("from DayDeath where id = :id", DayDeath.class);
		query.setParameter("id", aId);
		return query.uniqueResult();
	}

	@Override
	public DayFestival getFestivalById(int aId) {

		Session session = template.getSessionFactory().openSession();
		Query<DayFestival> query = session.createQuery("from DayFestival where id = :id", DayFestival.class);
		query.setParameter("id", aId);
		return query.uniqueResult();
	}

	@Override
	public void add(Day aDay) {
		
		template.save(aDay);
	}

	@Override
	public void update(Day aDay) {
		
		template.update(aDay);
	}

	@Override
	public void delete(Day aDay) {
		
		template.delete(aDay);
	}

	@Override
	public void addEvent(DayEvent aDayEvent) {
		
		template.save(aDayEvent);
	}

	@Override
	public void updateEvent(DayEvent aDayEvent) {
		
		template.update(aDayEvent);
	}

	@Override
	public void deleteEvent(DayEvent aDayEvent) {
		
		template.delete(aDayEvent);
	}

	@Override
	public void addBirth(DayBirth aDayBirth) {
		
		template.save(aDayBirth);
	}

	@Override
	public void updateBirth(DayBirth aDayBirth) {
		
		template.update(aDayBirth);
	}

	@Override
	public void deleteBirth(DayBirth aDayBirth) {
		
		template.delete(aDayBirth);		
	}

	@Override
	public void addDeath(DayDeath aDayDeath) {
		
		template.save(aDayDeath);
	}

	@Override
	public void updateDeath(DayDeath aDayDeath) {
		
		template.update(aDayDeath);
	}

	@Override
	public void deleteDeath(DayDeath aDayDeath) {
		
		template.delete(aDayDeath);
	}

	@Override
	public void addFestival(DayFestival aDayFestival) {
		
		template.save(aDayFestival);
	}

	@Override
	public void updateFestival(DayFestival aDayFestival) {
		
		template.update(aDayFestival);
	}

	@Override
	public void deleteFestival(DayFestival aDayFestival) {
		
		template.delete(aDayFestival);
	}
}

8. Modify the jsp file "WebContent\WEB-INF\jsp\DayForm.jsp":

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE HTML>
<html>
  <head>
    <title>历史上的今天</title>
    <style type="text/css">@import url("<c:url value="/css/main.css"/>");</style>
    <script type="text/javascript" src="<c:url value="/js/common.js"/>"></script>
  </head>
<body>

<c:if test="${adminMode}">
<div>
  <jsp:include page="LoggedInHeader.jsp"></jsp:include>
</div>
</c:if>

<div>
  <h1>${day.name}</h1>
  <c:if test="${adminMode}">
  <input id="btnDayInfo" type="button" value="编辑基本信息"  onclick="window.location.href='../../admin/day/edit/${day.key}'">
  </c:if>
</div>

<div>
  <p>
    ${day.description}
  </p>
</div>

<div id="divCatalog">
  <h3>目录</h3>
  <c:if test="${day.celebrityOfToday != null && day.celebrityOfToday != ''}">
    <a href="#divCelebOfToday">今日人物</a><br />
  </c:if>
  <a href="#divEvents">重大事件</a><br />
  <a href="#divCelebBorn">出生名人</a><br />
  <a href="#divCelebPassed">逝世名人</a><br />
  <a href="#divFestivals">节日风俗</a>
</div>

<c:if test="${day.celebrityOfToday != null && day.celebrityOfToday != ''}">
<div id="divCelebOfToday">
  <div>
    <h2>今日人物</h2>
  </div>
  <div>
    <img alt="${day.celebrityOfToday}" src="${pageContext.request.contextPath}/Images/${day.celebrityImagePath}" width="600px" height="100%">
  </div>
  <div>
    <p>
      ${day.celebrityDescription}
    </p>
  </div>
</div>
</c:if>

<div id="divEvents">
  <h2>重大事件</h2>
  <p>
  <c:forEach items="${day.historicalEvents}" var="dayEvent">
    <div>
      <c:if test="${adminMode}">
        <a href="../../admin/dayevent/edit/${dayEvent.id}">编辑</a>               
        <a href="../../admin/dayevent/delete/${dayEvent.id}" onclick="return del();">删除</a>
      </c:if>
      
      ${dayEvent.year}, ${dayEvent.eventDescription}
    </div>
  </c:forEach>
  <div>
    <c:if test="${adminMode}">
      <input id="btnDayEvent" type="button" value="添加新项" 
        onclick="window.location.href='../../admin/dayevent/add/${day.key}'">
    </c:if>
  </div>
  </p>
</div>

<div id="divCelebBorn">
  <h2>出生名人</h2>
  <p>
  <c:forEach items="${day.celebrityBirthdays}" var="dayBirth">
    <div>
      <c:if test="${adminMode}">
        <a href="../../admin/daybirth/edit/${dayBirth.id}">编辑</a>               
        <a href="../../admin/daybirth/delete/${dayBirth.id}" onclick="return del();">删除</a>
      </c:if>
      ${dayBirth.year}, ${dayBirth.celebrityName}, ${dayBirth.celebrityTitle}
      <c:if test="${dayBirth.celebrityAchievement != null && dayBirth.celebrityAchievement != ''}">
              (主要成就:${dayBirth.celebrityAchievement})
      </c:if>
    </div>
  </c:forEach>
  <div>
    <c:if test="${adminMode}">
      <input id="btnDayBirth" type="button" value="添加新项" 
        onclick="window.location.href='../../admin/daybirth/add/${day.key}'">
    </c:if>
  </div>
  </p>
</div>

<div id="divCelebPassed">
  <h2>逝世名人</h2>
  <p>
  <c:forEach items="${day.celebrityDeaths}" var="dayDeath">
    <div>
      <c:if test="${adminMode}">
        <a href="../../admin/daydeath/edit/${dayDeath.id}">编辑</a>               
        <a href="../../admin/daydeath/delete/${dayDeath.id}" onclick="return del();">删除</a>
      </c:if>
      ${dayDeath.year}, ${dayDeath.celebrityName}, ${dayDeath.celebrityTitle}
      <c:if test="${dayDeath.celebrityAchievement != null && dayDeath.celebrityAchievement != ''}">
              (主要成就:${dayDeath.celebrityAchievement})
      </c:if>
    </div>
  </c:forEach>
  <div>
    <c:if test="${adminMode}">
      <input id="btnDayDeath" type="button" value="添加新项" 
        onclick="window.location.href='../../admin/daydeath/add/${day.key}'">
    </c:if>
  </div>
  </p>
</div>

<div id="divFestivals">
  <h2>节日风俗</h2>
  <p>
  <c:forEach items="${day.festivals}" var="dayFestival">
    <div>
      <h3>${dayFestival.festivalName}</h3>
      <c:if test="${adminMode}">
        <a href="../../admin/dayfestival/edit/${dayFestival.id}">编辑</a>               
        <a href="../../admin/dayfestival/delete/${dayFestival.id}" onclick="return del();">删除</a>
      </c:if>
    </div>
    <div>
    <p>
      ${dayFestival.festivalDescriptionShort}
    </p>
    </div>
    <div>
    <p>
      ${dayFestival.festivalDescription}
    </p>
    </div>
  </c:forEach>
  <div>
    <c:if test="${adminMode}">
      <input id="btnDayFestival" type="button" value="添加新项" 
        onclick="window.location.href='../../admin/dayfestival/add/${day.key}'">
    </c:if>
  </div>
  </p>
</div>

</body>
</html>

9. As for other JSP files, please check this link com.casperlee.today.zip for the whole source code.

10. Yeah! Done!