<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:jsp="http://java.sun.com/xml/ns/javaee/jsp" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<servlet>
<servlet-name>memberjoin</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>memberjoin</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 한글 처리 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>EUC-KR</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
- /WebContent/WEB-INF/memberjoin-servlet.xml 소스
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- Controller 작성 -->
<!-- MemberJong.do를 위해 작성 -->
<bean id="memberjoin" name="/MemberJoin.do" class="controller.MemberJoin"
p:memberDao-ref="memberDao"
>
</bean>
<bean id="memberDao" class="member.MemberDao"
p:dataSource-ref="dataSource"
>
</bean>
<!-- Controller 실행 후 결과를 result할 jsp 페이지를 설정 -->
<bean id = "internalResolver"
class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name = "viewClass">
<value>org.springframework.web.servlet.view.JstlView</value>
</property>
<property name="prefix">
<value>/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<!-- 데이터 베이스 객체를 사용할 수 있게 설정해주는 Datasource 선언 -->
<bean id = "dataSource"
class = "org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name = "driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@127.0.0.1:1521:XE</value>
</property>
<property name="username">
<value>system</value>
</property>
<property name="password">
<value>123456</value>
</property>
</bean>
</beans>
- /WebContent/jsp/MemberJoin.jsp 소스
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title> 회 원 가 입 </title>
</head>
<body>
<center>
<h2> 회 원 가 입 </h2>
<!-- HelloWorld.do라는 컨트롤러를 호출하라는 뜻 -->
<form action="../MemberJoin.do" method="post">
<table border = "1" bgcolor = "lightyellow" cellpadding = "5">
<tr>
<td> 아이디 </td>
<td> <input type = "text" name = id> </td>
<td colspan = "2" align = "right"><b> *10자이내 </b> </td>
</tr>
<tr>
<td> 비밀번호 </td>
<td> <input type = "password" name = pass></td>
<td> 성명 </td>
<td> <input type = "text" name = "name"></td>
</tr>
<tr>
<td> 성별 </td>
<td><input type = "radio" name = "gender" value = "남"> 남
<input type = "radio" name = "gender" value = "여"> 여 </td>
<td> 취미 </td>
<td><input type = "checkbox" name = "hobby1" value = "음악감상"> 음악감상
<input type = "checkbox" name = "hobby2" value = "영화감상"> 영화감상 </td>
</tr>
<tr>
<td> 주소 </td>
<td> <input type = "text" name = "address"> </td>
<td> 이메일 </td>
<td> <input type = "text" name = "email"> </td>
</tr>
<tr>
<td> H.P </td>
<td><select name = "phone1">
<option value = "010"> 010 </option>
<option value = "011"> 011 </option>
<option value = "070"> 070 </option>
</select>
- <input type = "text" name = "phone2" size = "4">
- <input type = "text" name = "phone3" size = "4"> </td>
<td colspan = "2" align = "right"> <b>*필수입력 </b> </td>
</tr>
<tr>
<td> 자기소개 </td>
<td colspan = "3">
<textarea cols = "50" rows = "3" name = "introduce"></textarea> </td>
</tr>
<tr>
<td colspan = "4" align = "center">
<input type = "submit" value = "전송">
<input type = "reset" value = "취소"> </td>
</tr>
</table>
</form>
</center>
</body>
</html>
- /src/controller/MemberJoin.java 소스
package controller;
import member.MemberBean;
import member.MemberDao;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class MemberJoin {
// memberJoin-servlet.xml 파일에서 MemberDao가 DataSource 객체를 참조하게 선언
MemberDao memberDao;
// memberDao를 참조하겠다고 선언 - memberDao를 별도의 선언 없이 사용 가능
public void setMemberDao(MemberDao memberDao) {
this.memberDao = memberDao;
}
@RequestMapping("/MemberJoin") // RequestMapping이 있어야 호출이 됨
// RequestMapping에 의해서 사용자가 요청을 실행하는 순간 실행되는 CallBack 메소드
// 데이터를 같이 떠넘기지 않으면 Model을 필요 없다.
public ModelAndView setMember(MemberBean mbean){
// 참조했기에 객체를 생성 시킬 필요없이 갖다 쓸 수 있음
memberDao.memberInsert(mbean);
// model과 view를 동시에 지정 가능한 객체 선언
ModelAndView mav = new ModelAndView();
return mav; // bean 클래스에 선언한 ViewResolver 클래스를 호출
}
}
- /src/member/MemberBean.java 소스
package member;
public class MemberBean {
// form 데이터의 name과 똑같은 이름의 멤버변수를 만듬
private String id;
private String pass;
private String name;
private String gender;
private String hobby1;
private String hobby2;
private String address;
private String email;
private String phone1;
private String phone2;
private String phone3;
private String introduce;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getHobby1() {
return hobby1;
}
public void setHobby1(String hobby1) {
this.hobby1 = hobby1;
}
public String getHobby2() {
return hobby2;
}
public void setHobby2(String hobby2) {
this.hobby2 = hobby2;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
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;
}
public String getIntroduce() {
return introduce;
}
public void setIntroduce(String introduce) {
this.introduce = introduce;
}
}
- /src/member/MemberDao.java 소스 (중요)
package member;
import javax.sql.DataSource;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
public class MemberDao {
// Statement 객체와 유사한 쿼리를 실행시켜주는 객체
SimpleJdbcTemplate template;
// 참조할땐 set을 사용 (자동 등록)
// memberJoin-servlet.xml 파일에서 MemberDao가 DataSource 객체를 참조하게 선언했기에
// 실제 MemberDao에서 DataSource를 사용할 수 있도록 선언
/* public void setDataSource(DataSource dataSource){
// SimpleJdbcTemplate를 사용하려면 dataSource 객체가 필요하기 때문에 받아와야 함
// 쿼리를 실행하기 위해서 커넥션과 드라이버 클래스가 저장되어있는 DataSource를 이용하여 객체를 생성
template = new SimpleJdbcTemplate(dataSource);
} */
// memberJoin-servlet.xml 파일에서 MemberDao가 DataSource 객체를 참조하게 선언
// 자동 매핑을 위해서 위 방식 보다는 아래 방식을 추천 (위 방식은 직접 받는 방식, 아래 방식은 인터페이스를 사용한 방식)
DataSource dataSource;
public void setDataSource(DataSource dataSource){
template = new SimpleJdbcTemplate(dataSource);
}
// 멤버 데이터를 입력할 메소드 선언
public void memberInsert(MemberBean mbean){
// String gender = bean.getGender(); // gender를 미리 선언해놓은 경우에 아래처럼 사용가능
// String sql = "insert into member values(:id, :pass, :name, '"+gender"', :hobby1, :hobby2, :address, :email, :phone1, :phone2, :phone3, :introduce)";
String sql = "insert into member values(:id, :pass, :name, :gender, :hobby1, :hobby2, :address, :email, :phone1, :phone2, :phone3, :introduce)";
// 준비된 쿼리를 스프링용 SimpleJdbcTemplate 형태로 바꿔줌
// 알아서 bean 클래스와 매핑 시킴
SqlParameterSource sqlsource = new BeanPropertySqlParameterSource(mbean);
// 쿼리 실행
template.update(sql, sqlsource);
}
}
- 결과