Skip to content

Commit

Permalink
[블로그개발_10] 글에 부제목 추가 (Post Entity 모델링 +lombok, @Valid)
Browse files Browse the repository at this point in the history
  • Loading branch information
origoni committed Jun 10, 2015
1 parent 80883cc commit b4fe15b
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 43 deletions.
49 changes: 16 additions & 33 deletions src/main/java/com/millky/blog/domain/model/entity/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,32 @@
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Entity
public class Post {
@Id
@GeneratedValue
int id;

String subject;
@NotNull
@Size(min = 1, max = 255)
@Column(nullable = false)
String title;

@Size(max = 255)
String subtitle;

@NotNull
@Size(min = 1, max = 100000000)
@Column(length = 100000000)
String content;

Date regDate;

public int getId() {
return id;
}

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

public String getSubject() {
return subject;
}

public void setSubject(String subject) {
this.subject = subject;
}

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}

public Date getRegDate() {
return regDate;
}

public void setRegDate(Date regDate) {
this.regDate = regDate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import java.util.Date;
import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
Expand All @@ -26,7 +29,10 @@ public String form(Post post) {
}

@RequestMapping(value = "/write", method = RequestMethod.POST)
public String write(Post post) {
public String write(@Valid Post post, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
return "form";
}
post.setRegDate(new Date());
return "redirect:/post/" + postDao.save(post).getId();
}
Expand Down
22 changes: 17 additions & 5 deletions src/main/webapp/WEB-INF/jsp/form.jsp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html>
<html lang="ko">
<head>
Expand All @@ -21,6 +22,9 @@
#hinted{color:#1abf89;cursor:pointer;}
#hinted.disabled{color:#666;}
#hinted:before{content: '\e816';}
.errorblock {border: 2px solid red;}
.error {color: red;}
</style>

<link rel="stylesheet" href="/webjars/pen/0.1.0/src/pen.css" />
Expand Down Expand Up @@ -70,24 +74,32 @@
<i class="pen-icon icon-createlink" data-action="createlink"></i>
</div>

<form action="/post/write" onsubmit="$('#content').val($('#pen').html()); pen.destroy();" method="post">
<form:form action="/post/write" commandName="post" onsubmit="if($('#pen').html()!='<p><br></p>')$('#content').val($('#pen').html()); pen.destroy();" method="post">

<input type="text" name="subject" placeholder="Subject"
<form:errors path="*" cssClass="errorblock" element="div" />

<form:input type="text" path="title" placeholder="Title"
style="height: 70px; width: 100%; font-size: 55px;
border: none; border-right: 0px; border-top: 0px; boder-left: 0px; boder-bottom: 1px; outline-style: none;
font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-weight: 800;">
font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-weight: 800;" />
<form:errors path="title" cssClass="error" />

<input type="hidden" name="content" id="content">
<form:input type="text" path="subtitle" placeholder="Subtitle (option)"
style="height: 60px; width: 100%; font-size: 24px;
border: none; border-right: 0px; border-top: 0px; boder-left: 0px; boder-bottom: 1px; outline-style: none;
font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-weight: 600;" />

<hr style="margin-top: 2px; border-top: 1px solid #999;">

<div data-toggle="pen" data-placeholder="Content" id="pen" style="min-height: 200px;"></div>
<form:input type="hidden" path="content" id="content" />
<form:errors path="content" cssClass="error" />

<hr>

<button type="submit" class="btn btn-primary btn-lg btn-block">저장</button>

</form>
</form:form>

<p class="text-muted">Powered By <a href="http://millky.com">Millky</a> | WYSIWYG Editor by <a href="https://github.com/sofish/pen">Pen Editor</a></p>

Expand Down
4 changes: 2 additions & 2 deletions src/main/webapp/WEB-INF/jsp/list.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@
<div class="post-preview">
<a href="/post/${post.id}">
<h2 class="post-title">
<c:out value="${post.subject}" escapeXml="true"></c:out>
<c:out value="${post.title}" escapeXml="true"></c:out>
</h2>
<h3 class="post-subtitle">
부 제목
<c:out value="${post.subtitle}" escapeXml="true"></c:out>
</h3>
</a>
<p class="post-meta">Posted by <a href="#">Origoni</a> on ${post.regDate}</p>
Expand Down
4 changes: 2 additions & 2 deletions src/main/webapp/WEB-INF/jsp/post.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@
<div class="row">
<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
<div class="post-heading">
<h1><c:out value="${post.subject}" escapeXml="true"></c:out></h1>
<h2 class="subheading">부제목</h2>
<h1><c:out value="${post.title}" escapeXml="true"></c:out></h1>
<h2 class="subheading"><c:out value="${post.subtitle}" escapeXml="true"></c:out></h2>
<span class="meta">Posted by <a href="#">Origoni</a> on ${post.regDate}</span>
</div>
</div>
Expand Down

0 comments on commit b4fe15b

Please sign in to comment.