하루의 일상💜

[게시판만들기] 게시글 등록하기 본문

전자정부프레임워크

[게시판만들기] 게시글 등록하기

도하루박 2024. 3. 5. 10:23
반응형

1.  입력 폼

게시글을 입력할 수 있는 폼jsp 생성한다.

bbsInsert.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form id="insertForm" name="insertForm" method="post" action="/insertBbs.do">
	
		<label>제목</label>
		<input name="title" value=""/><br/>
		
		<label>내용</label>
		<textarea name="content"></textarea><br/>
		
		<input type="submit" value="등록"/>
		
	</form>
</body>
</html>

 

 

앞에 만들었던 게시글 목록에 입력 폼으로 이동할 수 있는 버튼을 생성해준다,

<div>
	<button><a href="/bbsForm.do">게시글 등록</a></button>	
</div>
bbsList.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<table>
	<caption>게시판테이블</caption>
	<thead>
		<tr>
			<th>번호</th>
			<th>제목</th>
			<th>내용</th>
			<th>등록일</th>
		</tr>
	</thead>
	<tbody>
		<c:forEach var="rs" items="${resultList}" varStatus="status">
		<tr>
			<td>${rs.seq}</td>
			<td>${rs.title}</td>
			<td>${rs.content}</td>
			<td>${rs.regDate}</td>
		</tr>
		</c:forEach>
	</tbody>
</table>

<div>
	<button><a href="/bbsForm.do">게시글 등록</a></button>	
</div>
</body>
</html>

위와 같이 게시글 입력 폼으로 이동할 수 있는 버튼 생성!!

 

 

 

2. insert SQL 생성하기

BbsSQL.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="BbsDAO">

	<select id="selectBbsList" resultType="bbsVO">
		SELECT * 
		FROM BOARD
	</select>
	
	<insert id="insertBbs">
		INSERT INTO board
		(TITLE, CONTENT, REG_DATE, MOD_DATE)
		VALUES(#{title}, #{content}, NOW(), NOW())
	</insert>
	
</mapper>
<insert id="insertBbs">
		INSERT INTO board
		(TITLE, CONTENT, REG_DATE, MOD_DATE)
		VALUES(#{title}, #{content}, NOW(), NOW())
</insert>

 

 

3. insert java, interface생성

BbsDAO
package haru.com.bbs.service.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import egovframework.rte.psl.dataaccess.EgovAbstractMapper;
import haru.com.bbs.service.BbsVO;

@Repository("bbsDAO")
public class BbsDAO extends EgovAbstractMapper {
	
	public List<?> selectList(BbsVO vo) throws Exception {
		return selectList("selectBbsList", vo);
	}
	
	public void insertBbs(BbsVO vo) throws Exception {
		insert("insertBbs", vo);
	}
}
public void insertBbs(BbsVO vo) throws Exception {
		insert("insertBbs", vo);
}

 

 

BbsService
package haru.com.bbs.service;

import java.util.List;

public interface BbsService {
	
	List<?> selectList(BbsVO vo) throws Exception;
	
	void insertBbs(BbsVO vo) throws Exception;

}
void insertBbs(BbsVO vo) throws Exception;

 

 

BbsServiceImpl
package haru.com.bbs.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import haru.com.bbs.service.BbsService;
import haru.com.bbs.service.BbsVO;

@Service("bbsService")
public class BbsServiceImpl implements BbsService {
	
	@Resource(name = "bbsDAO")
	private BbsDAO bbsDAO;

	public List<?> selectList(BbsVO vo) throws Exception {
		return bbsDAO.selectList(vo);
	}

	@Override
	public void insertBbs(BbsVO vo) throws Exception {
		bbsDAO.insertBbs(vo);
	}
}
@Override
	public void insertBbs(BbsVO vo) throws Exception {
		bbsDAO.insertBbs(vo);
}

 

 

BbsController
package haru.com.bbs.web;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

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 haru.com.bbs.service.BbsService;
import haru.com.bbs.service.BbsVO;

@Controller
public class BbsController {
	
	@Resource(name = "bbsService")
    private BbsService bbsService;
	
	@RequestMapping(value = "/index.do")
	public String main() throws Exception {
		return "main";
	}
	
	//게시글 목록
	@RequestMapping(value = "/bbsList.do")
	public String bbsList(BbsVO vo, Model model) throws Exception {
		
		List<?> bbsList = bbsService.selectList(vo);
		
		model.addAttribute("resultList", bbsList);
		return "/bbs/bbsList";
	}
	
	//게시글등록form
	@RequestMapping(value = "/bbsForm.do")
	public String bbsForm() throws Exception {
		
		return "/bbs/bbsInsert";
	}
	
	//게시글등록
	@RequestMapping(value = "/insertBbs.do", method = RequestMethod.POST)
	public String insertBbs(@RequestParam("title") String title, @RequestParam("content") String content, BbsVO vo) throws Exception {
		
	    vo.setTitle(title); 
	    vo.setContent(content); 
	    
	    bbsService.insertBbs(vo); 
	    return "redirect:/bbsList.do"; 
	}

}
	//게시글등록form
	@RequestMapping(value = "/bbsForm.do")
	public String bbsForm() throws Exception {
		
		return "/bbs/bbsInsert";
	}
	
	//게시글등록
	@RequestMapping(value = "/insertBbs.do", method = RequestMethod.POST)
	public String insertBbs(@RequestParam("title") String title, @RequestParam("content") String content, BbsVO vo) throws Exception {
		
	    vo.setTitle(title); 
	    vo.setContent(content); 
	    
	    bbsService.insertBbs(vo); 
	    return "redirect:/bbsList.do"; 
	}

 

 

 

3. 폼에 데이터 입력

폼에 데이터를 입력하여 저장했더니 한글이 깨진다.

처음 web에 필터설정을 하지 않았기 때문....

web.xml에서 필터 설정을 해준다

 

web.xml
<filter>
		<filter-name>encodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>utf-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>*.do</url-pattern>
	</filter-mapping> 
    
	 <filter>
		<filter-name>HTMLTagFilter</filter-name>
		<filter-class>egovframework.rte.ptl.mvc.filter.HTMLTagFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>HTMLTagFilter</filter-name>
		<url-pattern>*.do</url-pattern>
	</filter-mapping>

 

반응형