Permalink
Browse files

lecture10

  • Loading branch information...
teachingprogramming committed Jun 20, 2017
1 parent 205bf86 commit d1436aa7a492bf6cc118188b3106ce0e4a6f0b85
View
@@ -26,4 +26,6 @@ dependencies {
compile('org.springframework.boot:spring-boot-starter-thymeleaf')
compile('org.springframework.boot:spring-boot-starter-web')
testCompile('org.springframework.boot:spring-boot-starter-test')
compile('org.springframework.boot:spring-boot-starter-jdbc')
compile('com.h2database:h2')
}
@@ -0,0 +1,62 @@
package net.teachingprogramming.webapp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* 第10回で使うコントローラ
*/
@Controller
@RequestMapping("/lecture10")
public class Lecture10Controller {
private final JdbcTemplate jdbcTemplate;
/**
* コンストラクタ
*/
@Autowired
public Lecture10Controller(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
/**
* 掲示板: get
*/
@GetMapping("/bbs")
public String bbsGet(ModelMap modelMap) {
List<BbsComment> commentList = new ArrayList<>(); // 表示するコメントのリスト
List<Map<String, Object>> dataList = jdbcTemplate.queryForList("SELECT * FROM bbs_comment"); // データベースから取り出す。
for (Map<String, Object> data : dataList) {
BbsComment comment = new BbsComment();
comment.body = (String)data.get("body");
comment.name = (String)data.get("name");
comment.date = (Date)data.get("date");
commentList.add(comment);
}
modelMap.addAttribute("commentList", commentList);
return "lecture05/bbs";
}
/**
* 掲示板: post
*/
@PostMapping("/bbs")
public String bbsGet(@RequestParam("body") String body, @RequestParam("name") String name) {
Date date = new Date();
jdbcTemplate.update("INSERT INTO bbs_comment ( body , name, date ) VALUES (?, ?, ?)", body, name, date);
return "redirect:/lecture10/bbs"; // http://localhost:18080/lecture10/bbsにリダイレクト(GET)
}
}
@@ -1 +1,6 @@
server.port = 18080
server.port = 18080
spring.h2.console.enabled = true
spring.datasource.driver-class-name = org.h2.Driver
spring.datasource.url = jdbc:h2:~/webappdb
spring.datasource.username = sa
spring.datasource.password = sa

0 comments on commit d1436aa

Please sign in to comment.