[스프링] CREATE - 글 입력하기


JSP 파일 만들기


src/main/webapp/WEB-INF/views 하위에 /board/create.jsp 생성

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
<head>
<style>
	.center{
		margin: 5px 25px; padding: 20px
	}
</style>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<meta charset="UTF-8">
<title>create</title>
</head>
<body>
    <h2>Create Page</h2>

	<form class="center" method="post">	
        <div class="form-group">
              <label>이름</label>
            <input type="text" class="form-control" name="uName">
        </div>
        <div class="form-group">
            <label>제목</label>
            <input type="text" class="form-control" name="subject">
        </div>
        <div class="form-group">
            <label>내용</label>
            <input class="form-control" name="content">
        </div>
       
    <button type="submit" class="btn btn-outline-info">등록</button>
    <button type="button" class="btn btn-outline-info"><a href="/board/list">돌아가기</a></button>
    
    </form>
</body>
</html>



Controller 작성하기


src/main/java/com.springtest.controller/TestController.java

package com.springtest.controller;

import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.springtest.domain.TestVO;
import com.springtest.service.testService;

@Controller
@RequestMapping("/board/*")
public class TestController {

	@Inject
	private testService service;
	
	// list.jsp
	@RequestMapping(value="/list", method = RequestMethod.GET)
	public String getList(Model model) throws Exception{
		List<TestVO> list = service.list();
		model.addAttribute("list", list);
		return "board/list";
	}
	
	// create.jsp
	@RequestMapping(value="/create", method = RequestMethod.GET)
	public String getCreate() throws Exception{
		return "board/create";
	}
	
}



Mapper 작성하기


src/main/resources/mappers/springTestMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.springtest.mappers.board">

	<!-- 리스트 출력 -->
	<select id="list" resultType="com.springtest.domain.TestVO">
		select
	 		num, uName, subject, content, date
	 	from springtest
	</select>
	
	<!-- 게시글 등록 -->
	<insert id="create">
		insert into springtest
		(uName, subject, content, date)
		values
		(#{uName}, #{subject}, #{content}, NOW())
	</insert>
</mapper>



DAO 작성하기


  • src/main/java/com.springtest.dao/testDAO.java
package com.springtest.dao;

import java.util.List;

import com.springtest.domain.TestVO;

public interface testDAO {
	public List<TestVO> list();
	public void create(TestVO vo);
}
  • src/main/java/com.springtest.dao/testDAOImpl.java
package com.springtest.dao;

import java.util.List;

import javax.inject.Inject;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;

import com.springtest.domain.TestVO;

@Repository
public class testDAOImpl implements testDAO {

	
	@Inject
	private SqlSession sqlSession;
	
	private static String namespace = "com.springtest.mappers.board";
	
	// list
	@Override
	public List<TestVO> list() {
		return sqlSession.selectList(namespace + ".list");
	}

	// create
	@Override
	public void create(TestVO vo) {
		sqlSession.insert(namespace + ".create", vo);
	}

}



Service 작성하기


  • src/main/java/com.springtest.service/testService.java
package com.springtest.service;

import java.util.List;

import com.springtest.domain.TestVO;

public interface testService {
	public List<TestVO> list();

	public void create(TestVO vo);
}
  • src/main/java/com.springtest.service/testServiceImpl.java
package com.springtest.service;

import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Service;

import com.springtest.dao.testDAO;
import com.springtest.domain.TestVO;
@Service
public class testServiceImpl implements testService {

	@Inject
	private testDAO dao;
	
	@Override
	public List<TestVO> list() {
		return dao.list();
	}
	
	@Override
	public void create(TestVO vo) {
		dao.create(vo);
	}
	
}



Controller


package com.springtest.controller;

import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.springtest.domain.TestVO;
import com.springtest.service.testService;

@Controller
@RequestMapping("/board/*")
public class TestController {

	@Inject
	private testService service;
	
	// list.jsp
	@RequestMapping(value="/list", method = RequestMethod.GET)
	public String getList(Model model) throws Exception{
		List<TestVO> list = service.list();
		model.addAttribute("list", list);
		return "board/list";
	}
	
	// create.jsp 페이지로 이동
	@RequestMapping(value="/create", method = RequestMethod.GET)
	public String getCreate() throws Exception{
		return "board/create";
	}
	
	// create.jsp 게시물 작성하기
	@RequestMapping(value="/create", method = RequestMethod.POST)
	public String postCreate(TestVO vo) throws Exception{
		service.create(vo);
		return "redirect:list";
	}
	
}



글 작성해보기


Categories:

Spring