스프링 폼 태그를 사용하다보면 전화번호 / 이메일 처럼
000-000-000 혹은 이메일@email.com 을 DB에 어떻게 넣어야 할지 고민할때가 있다
그때는 아래와 같이 적용하면 자바스트립트를 사용하지 않고 코드 몇줄로 위에 같은 형식으로 저장 할 수 있다.
#TABLE
CREATE TABLE SPRING_FORM(
NUM INT(100) UNSIGNED AUTO_INCREMENT,
Name CHAR(255),
PHONE CHAR(100),
EMAIL CHAR(255),
PRIMARY KEY(NUM)
)ENGINE=INNODB DEFAULT CHARSET=utf8;
스프링 폼 태그
<form.jsp>
<%@ 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="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<fmt:requestEncoding value="utf-8"/>
<% request.setCharacterEncoding("UTF-8");%>
<% response.setContentType("text/html; charset=UTF-8");%>
<form:form commandName="form" action="form.do" method="post">
<table>
<colgroup><col width="95" /><col width="280" /><col width="300"/></colgroup>
<tr>
<th>이름</th>
<td><form:input path="name" /></td>
<td><font color="red"><form:errors path="name" /></font></td>
</tr>
<tr>
<th>연락처</th>
<td>
<form:select path="phone1" items="${select_phone}" /> -
<form:input path="phone2" cssStyle="width: 50px;" onkeydown="this.value=this.value.replace(/[^0-9]/g,'');"/> -
<form:input path="phone3" cssStyle="width: 50px;" onkeydown="this.value=this.value.replace(/[^0-9]/g,'');"/>
<td><font color="red"><form:errors path="phone" /></font></td>
</tr>
<tr>
<th>이메일</th>
<td>
<form:input path="email1" cssStyle="width: 100px; ime-mode:disabled;"/>@
<form:select path="email2" items="${select_email}" cssStyle="ime-mode:disabled;"/>
</td>
<td><font color="red"><form:errors path="email" /></font></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="입력" /></td>
</tr>
</table>
</form:form>
Controller
package com.controller;
import java.util.LinkedHashMap;
import java.util.Map;
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.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.service.IFormService;
import com.vo.FormVO;
@Controller
@RequestMapping("/form")
public class FormController {
@Autowired
private IFormService iformService;
//phone
static Map<String, String> select_phone = new LinkedHashMap<String, String>();
static {
select_phone.put("010", "010");
select_phone.put("011", "011");
select_phone.put("016", "016");
select_phone.put("017", "017");
select_phone.put("018", "018");
select_phone.put("019", "019");
}
//email
static Map<String, String> select_email = new LinkedHashMap<String, String>();
static {
select_email.put("naver","naver.com");
select_email.put("nate","nate.com");
select_email.put("dreamwiz","dreamwiz.com");
select_email.put("yahoo","yahoo.co.kr");
select_email.put("empal","empal.com");
select_email.put("unitel","unitel.co.kr");
select_email.put("gmail","gmail.com");
select_email.put("korea","korea.com");
select_email.put("chol","chol.com");
select_email.put("paran","paran.com");
select_email.put("freechal","freechal.com");
select_email.put("hanmail","hanmail.net");
select_email.put("hotmail","hotmail.com");
}
@RequestMapping(method = {RequestMethod.GET})
public String formwrite(@ModelAttribute("form") FormVO form, Model model) {
model.addAttribute("form", form);
model.addAttribute("select_phone", select_phone);
model.addAttribute("select_email", select_email);
return "form";
}
@RequestMapping(method = {RequestMethod.POST})
public String formadd(@ModelAttribute("form") @Valid FormVO form, BindingResult result, Model model) {
if (result.hasErrors()) {
System.out.println(result);
model.addAttribute("form", form);
model.addAttribute("select_phone", select_phone);
model.addAttribute("select_email", select_email);
return "form";
}
iformService.formAdd(form);
return "redirect:/form.do";
}
}
Getter / Setter
<FormVO>
package com.vo;
import java.io.Serializable;
import org.hibernate.validator.constraints.NotEmpty;
@SuppressWarnings("serial")
public class FormVO implements Serializable {
private String num;
@NotEmpty
private String name;
private String phone;
private String phone1;
private String phone2;
private String phone3;
private String email;
private String email1;
private String email2;
public FormVO() {
}
public FormVO(String name, String phone1, String phone2, String phone3, String num, String email, String email1, String email2, String phone) {
this.num = num;
this.name = name;
this.phone = phone;
this.phone1 = phone1;
this.phone2 = phone2;
this.phone3 = phone3;
this.email = email;
this.email1 = email1;
this.email2 = email2;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@NotEmpty
public String getPhone() {
if (phone1=="" || phone2=="" || phone3==""){
phone="";
}else{
phone=phone1+"-"+phone2+"-"+phone3;
}
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getPhone1() {
return phone1;
}
public void setPhone1(String phone1) {
this.phone1 = phone1;
}
public String getPhone2() {
return phone2;
}
public void setPhone2(String phone2) {
this.phone2 = phone2;
}
public String getPhone3() {
return phone3;
}
public void setPhone3(String phone3) {
this.phone3 = phone3;
}
@NotEmpty
public String getEmail() {
if (email1=="" || email2==""){
email="";
}else{
email=email1+"@"+email2;
}
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getEmail1() {
return email1;
}
public void setEmail1(String email1) {
this.email1 = email1;
}
public String getEmail2() {
return email2;
}
public void setEmail2(String email2) {
this.email2 = email2;
}
}
위에 같이 설정하면 JavaScript를 사용하지 않고 손쉽게 DB에는 연락처 형식 (예:123-456 -789)과 이메일 형식(예:abc@naver.com)으로 Insert 할 수 있다.