728x90

Spring Security

출처 : 사랑이 고픈 프로그래머.. - 흔히 보게되는 절대 써먹을 수 없는 Spring Security의 초간단 셋팅
jjeong :: Spring Security login/logout 관련 글

web.xml 내용추가(WebContent\WEB-INF\web.xml)

	<!-- 스프링 스큐리티 관련 설정 / 시작 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			/WEB-INF/iot-datasource.xml
			/WEB-INF/iot-mybatis.xml
			/WEB-INF/iot-security.xml
		</param-value>
	</context-param>
	
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
	<filter>
		<filter-name>springSecurityFilterChain</filter-name>
		<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>springSecurityFilterChain</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<!-- 스프링 스큐리티 관련 설정 / 끝 -->

iot-security.xml 파일 생성(WebContent\WEB-INF\iot-security.xml)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:security="http://www.springframework.org/schema/security"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
           http://www.springframework.org/schema/security
           http://www.springframework.org/schema/security/spring-security-3.2.xsd">

	<!-- 확장자를 이용해 패턴을 걸때는 /**/*.해당 확장자 로 할 것(Ant Pattern 규칙) -->
	<security:http pattern="/**/*.js" security="none"/>
	<security:http pattern="/**/*.css" security="none"/>
	<security:http pattern="/images/*" security="none"/>
	
	<!--
	<security:http pattern="/user_add_ajax.iot" security="none" //>
	<security:http pattern="/login.iot" security="none" />
	<security:http pattern="/loginfailed.iot" security="none" />
	<security:http pattern="/logout.iot" security="none" />
	-->
	
	<security:http auto-config="true">
		<security:intercept-url pattern="/json.iot" access="ROLE_ANONYMOUS,ROLE_USER" />
		<security:intercept-url pattern="/**/*.iot" access="ROLE_USER"/>
		
		<!-- 
		<security:form-login login-page="/login.iot" default-target-url="/hello_world.iot" authentication-failure-url="/loginfailed.iot" />
		<security:logout logout-success-url="/login.iot" />
		-->
	</security:http>

	<security:authentication-manager>
		<security:authentication-provider>

			<security:jdbc-user-service
				data-source-ref="dataSource"
				users-by-username-query="select user_id username, password password, 1 as enabled from user_info where user_id = ?"
				authorities-by-username-query="select name username, 'ROLE_USER' authority from user_info where user_id = ?" />

		</security:authentication-provider>
	</security:authentication-manager>

</beans>

로그인 페이지 만들기

package com.iot.controller;

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

@Controller
public class LoginController {

	@RequestMapping(value = "/login.iot", method = RequestMethod.GET)
	public String login(ModelMap modelMap) throws Exception {
		return "/login";
	}
}

로그인페이지 추가 (WebContent\WEB-INF\jsp\login.jsp)

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

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login Page</title>
<style>
.errorblock {
	color: #ff0000;
	background-color: #ffEEEE;
	border: 3px solid #ff0000;
	padding: 8px;
	margin: 16px;
}
</style>
</head>
<body onload='document.f.j_username.focus();'>

 <div id="background_login">
	<h3>IOT 로그인 페이지</h3>
 
	<c:if test="${not empty error}">
		<div class="errorblock">
			Your login attempt was not successful, try again.<br /> Caused :
			${sessionScope["SPRING_SECURITY_LAST_EXCEPTION"].message}
		</div>
	</c:if>

	<form name='f' action="<c:url value='j_spring_security_check' />" method='POST'>
		<table>
			<tr>
				<td>사용자ID : </td>
				<td><input type='text' name='j_username' value=''>
				</td>
			</tr>
			<tr>
				<td>PassWord : </td>
				<td><input type='password' name='j_password' />
				</td>
			</tr>
			<tr>
				<td><input name="submit" type="submit" value="로그인" />
				<td><input name="reset" type="reset" />
				</td>
			</tr>
		</table>
	</form>
</div>

<a href="<c:url value='/user_add_ajax.iot' />">사용자추가</a>

</body>
</html>

시큐리티 제외 주소 설정

iot-security.xml 파일 내용 추가(WebContent\WEB-INF\iot-security.xml)

로그인 관련 주소는 시큐리티 설정에서 제외 시킨다.

	<security:http pattern="/user_add_ajax.iot" security="none" />
	<security:http pattern="/login.iot" security="none" />
	<security:http pattern="/loginfailed.iot" security="none" />
	<security:http pattern="/logout.iot" security="none" />

로그인 페이지 설정

iot-security.xml 파일 내용 추가(WebContent\WEB-INF\iot-security.xml)

		<security:form-login login-page="/login.iot" 
			default-target-url="/hello_world.iot" 
			authentication-failure-url="/loginfailed.iot" />
		<security:logout logout-success-url="/logout.iot" />

로그아웃

웹페이지 주소에 /j_spring_security_logout 이동하면 로그아웃

로그인 아웃 페이지 만들기(src\com\iot\controller\LoginController.java)

	@RequestMapping(value = "/logout.iot", method = RequestMethod.GET)
	public String logout(ModelMap modelMap) throws Exception {
		return "/logout";
	}

로그인아웃 페이지 추가 (WebContent\WEB-INF\jsp\logout.jsp)

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login Page</title>
</head>
<body>
	<h3>로그아웃</h3>	
	<a href="<c:url value="j_spring_security_logout" />" >로그아웃</a>
</body>
</html>

컨트롤러에서 로그인 아이디 얻기

	Authentication auth = SecurityContextHolder.getContext().getAuthentication();
	String user_id = auth.getName();


728x90
728x90

삭제 CheckBox 추가(hello_world.jsp)

hello_world.jsp에서 목록을 form으로 감싸고, 사용자 마다 삭제 checkbox를 추가

<h1>사용자 조회</h1>
<form action="delete_user.iot" method="post">
	<table border="1">
		<c:forEach items="${userlist}" var="data" varStatus="loop">
			<tr>
				<td><input type="checkbox" name="delete_user_ids" value="${data.user_id}"/></td>
				<td><a href="<c:url value="/edit_user.iot"/>?user_id=${data.user_id}">${data.user_id}</a></td>
				<td>${data.password}</td>
				<td>${data.name}</td>
				<td>${data.nickname}</td>
			</tr>
		</c:forEach>
	</table>
	<br/>
	<input type="submit" value="삭제"/>
</form>

src\com\iot\controller\UserController.java 클래스 내용 추가

	@RequestMapping(value = "/delete_user.iot", method = RequestMethod.POST)
	public String delete_user(@RequestParam("delete_user_ids") String[] delete_user_ids, ModelMap modelMap) throws Exception {
		// 삭제할 사용자 ID마다 반복해서 사용자 삭제
		for (String user_id : delete_user_ids) {
			System.out.println("사용자 삭제 = " + user_id);
			int delete_count = service.deleteUser(user_id);
		}
		// 목록 페이지로 이동
		return "redirect:/hello_world.iot";
	}

src\com\iot\db\service\UserInfoService.java 클래스 내용 추가

	public int deleteUser(String user_id) {
		return userInfoMapper.deleteUser(user_id);
	}

src\com\iot\db\mapper\UserInfoMapper.java 인터페이스 내용 추가

	public int deleteUser(String user_id);

src\com\iot\db\mapper\UserInfoMapper.xml 내용 추가

	<!-- deleteUser -->
	<delete id="deleteUser" parameterType="java.lang.String" statementType="PREPARED">
		delete from user_info where user_id = #{user_id}
	</delete>
728x90
728x90

수정페이지 이동(hello_world.jsp)

hello_world.jsp에서 사용자 아이디에 수정페이지로 이동하도록 내용 추가

<td><a href="<c:url value="/edit_user.iot"/>?user_id=${data.user_id}">${data.user_id}</a></td>

src\com\iot\controller\UserController.java 클래스 내용 추가

	@Autowired
	private UserInfoService service;

	@RequestMapping(value = "/edit_user.iot", method = RequestMethod.GET)
	public String edit_user(@RequestParam("user_id") String user_id, ModelMap modelMap) throws Exception {
		
		UserInfo userinfo = service.selectUser(user_id);
		
		// 사용자 수정용 객체 설정
		modelMap.addAttribute("userinfo", userinfo);
		
		return "/edit_user";
	}
	
	@RequestMapping(value = "/edit_user_apply.iot", method = RequestMethod.POST)
	public String edit_user_apply(UserInfo userinfo, ModelMap modelMap) throws Exception {
		int updateCount = service.updatetUser(userinfo);
		return "redirect:/hello_world.iot";
	}

src\com\iot\db\service\UserInfoService.java 클래스 내용 추가

	public UserInfo selectUser(String user_id) {
		return userInfoMapper.selectUser(user_id);
	}

	public int updatetUser(UserInfo userinfo) {
		return userInfoMapper.updatetUser(userinfo);
	}

src\com\iot\db\mapper\UserInfoMapper.java 인터페이스 내용 추가

	public UserInfo selectUser(String user_id);

	public int updatetUser(UserInfo userinfo);

src\com\iot\db\mapper\UserInfoMapper.xml 내용 추가

	<!-- selectUser -->
	<select id="selectUser" parameterType="java.lang.String" resultType="com.iot.db.domain.UserInfo">
		select *
		  from user_info
		 where user_id = #{user_id}
	</select>
	
	<!-- updatetUser -->
	<update id="updatetUser" parameterType="com.iot.db.domain.UserInfo" statementType="PREPARED">
		update user_info
		<trim prefix="SET" suffixOverrides=",">
			<if test="password != null">password = #{password, jdbcType=VARCHAR} ,</if>
			<if test="name != null">name = #{name, jdbcType=VARCHAR} ,</if>
			<if test="nickname != null">nickname = #{nickname, jdbcType=VARCHAR} ,</if>
		</trim>
		 where user_id = #{user_id}
	</update>

WebContent\WEB-INF\jsp\edit_user.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="form" uri="http://www.springframework.org/tags/form"%>

<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>

<h1>사용자 수정</h1>
<form:form id="dataForm" name="dataForm" modelAttribute="userinfo" action="edit_user_apply.iot" method="post">
	<form:hidden path="user_id"/>
	<table border="1">
		<tr>
			<td>사용자ID</td>
			<td>
				${userinfo.user_id}
			</td>
		</tr>
		<tr>
			<td>암호</td>
			<td>
				<form:input path="password"/>
			</td>
		</tr>
		<tr>
			<td>사용자이름</td>
			<td>
				<form:input path="name"/>
			</td>
		</tr>
		<tr>
			<td>별명</td>
			<td>
				<form:input path="nickname"/>
			</td>
		</tr>
	</table>
	<br/>
	<input type="submit" value="사용자 수정"/>
</form:form>

</body>
</html>
728x90
728x90

JSONData 추상 클래스 생성

Factory 패턴에서 반환 가능한 형태로 만들기 위해서 추상 클래스를 사용하기때문에 아래와 같이 추상 클래스를 생성

package com.iot.json;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

public abstract class JSONData {
	
	private static SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
	
	public abstract JSONObject getJSON(HttpServletRequest request, HttpServletResponse response);
	
	public JSONObject getJSONObject(Map<String, Object> map) {
		JSONObject jobj = new JSONObject();
		//loop a Map
		for (Map.Entry<String, Object> entry : map.entrySet()) {
			if (entry.getValue() instanceof java.sql.Date) {
				//
				jobj.put(entry.getKey(), fromCalendar((Date) entry.getValue()));
			} else {
				jobj.put(entry.getKey(), entry.getValue());
			}
		}
		return jobj;
	}
	
	public static String fromCalendar(final Date date) {
		String formatted = formatter.format(date);
		return formatted.substring(0, 22) + ":" + formatted.substring(22);
	}
}

JSONSample 클래스 생성

간단하게 테스트나 이해를 돕기 위해서 JSONData 추상 클래스를 상속받아서 Sample 클래스를 생성

package com.iot.json;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
public class JSONSample extends JSONData {

	@Override
	public JSONObject getJSON(HttpServletRequest request, HttpServletResponse response) {
		JSONArray arrayObj = new JSONArray();
		
		JSONArray data = new JSONArray();
		data.add("Tiger Nixon");
		data.add("System Architect");
		data.add("Edinburgh");
		data.add("5421");
		data.add("2011/04/25");
		data.add("$320,800");
		arrayObj.add(data);
		
		data = new JSONArray();
		data.add("Garrett Winters");
		data.add("Accountant");
		data.add("Tokyo");
		data.add("8422");
		data.add("2011/07/25");
		data.add("$170,750");
		arrayObj.add(data);
		
		JSONObject jobj_data = new JSONObject();
		jobj_data.put("data", arrayObj);
		
		return jobj_data;
	}
}

JSONFactory 클래스 생성

Factory 패턴으로 cmd 값에 따라 해당 JSONData를 구현한 객체를 반환

package com.iot.json;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller
public class JSONFactory {
	
	@Autowired
	private JSONSample jSONSample;
	
	public JSONData getJSONData(String cmd) {
		
		System.out.println("cmd = " + cmd);
		
		if (true) {
			return jSONSample;
		}
		
		return jSONSample;
	}
}

JSONController 클래스 생성

json 형태로 데이터 반환

package com.iot.json;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class JSONController {
	
	@Autowired
	private JSONFactory jSONFactory;

	@ResponseBody
	@RequestMapping(value="/json.iot", method=RequestMethod.GET, produces="application/json; charset=utf-8")
	public String service(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws IOException {
		
		/*
		Authentication auth = SecurityContextHolder.getContext().getAuthentication();
		String reg_id = auth.getName();
		*/
		
		/*
		Enumeration<String> keys = request.getParameterNames();
		while(keys.hasMoreElements()) {
			String key = keys.nextElement();
			System.out.println("param = " + key + "/" + request.getParameter(key));
		}
		*/
		
		String cmd = request.getParameter("cmd");
		if (cmd==null) {
			cmd = "";
		}
		
		JSONData json = jSONFactory.getJSONData(cmd);
		
		return json.getJSON(request, response).toString();
	}

}

iot-servlet.xml 내용 추가

json 관련 패키지가 자동으로 스프링컨테이너에 로딩 되도록 WEB-INF/iot-servlet.xml 파일에에 아래 내용을 추가

<context:component-scan base-package="com.iot.json"/>

	<!-- 컨트롤더 package 자동으로 스프링 컨테이너 등록 -->
	<context:component-scan base-package="com.iot.controller"/>
	<context:component-scan base-package="com.iot.json"/>

Sample 데이터 확인

JSONUser 클래스 추가

AJAX로 사용자 추가하는 클래스를 만들기 위해서  JSONData 추상 클래스를 상속받아서 JSONUser 클래스를 생성

package com.iot.json;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import com.iot.db.domain.UserInfo;
import com.iot.db.service.UserInfoService;

import net.sf.json.JSONObject;

@Controller
public class JSONUser extends JSONData {
	
	public static String COMMAND = "user";
	
	@Autowired
	private UserInfoService service;

	@Override
	public JSONObject getJSON(HttpServletRequest request, HttpServletResponse response) {
		String subcmd = request.getParameter("subcmd");
		System.out.println("subcmd = " + subcmd);	
		
		JSONObject jobj_data = null;
		if (subcmd.equals("add")) {
			jobj_data = getAddData(request, response);
		}
			
		return jobj_data;
	}

	private JSONObject getAddData(HttpServletRequest request, HttpServletResponse response) {
		
		String errStr = null;
		
		try {
			UserInfo userinfo = new UserInfo();
			userinfo.setUser_id(request.getParameter("user_id"));
			userinfo.setPassword(request.getParameter("password"));
			userinfo.setName(request.getParameter("name"));
			userinfo.setNickname(request.getParameter("nickname"));
			
			service.insertUserInfo(userinfo);
		} catch (Exception e) {
			errStr = e.toString();
			e.printStackTrace();
		}
		
		JSONObject jobj_data = new JSONObject();
		jobj_data.put("success", (errStr == null ? true : false));
		
		return jobj_data;
	}

}

JSONFactory 수정

파라미터 cmd의 값이 user인 경우 JSONUser의 객체가 반환 되도록 코드 추가

package com.iot.json;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller
public class JSONFactory {
	
	@Autowired
	private JSONUser jSONUser;
	
	@Autowired
	private JSONSample jSONSample;
	
	public JSONData getJSONData(String cmd) {
		
		System.out.println("cmd = " + cmd);
		
		if (JSONUser.COMMAND.equals(cmd)) { // JSONUser.COMMAND = user
			return jSONUser;
		} else {
			return jSONSample;
		}
		
		// return jSONSample;
	}
}

UserController 클래스 생성

사용자 관련 Controller 클래스 생성

package com.iot.controller;

import java.util.ArrayList;
import java.util.List;

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

@Controller
public class UserController {
	
	@RequestMapping(value = "/user_add_ajax.iot", method = RequestMethod.GET)
	public String user_add_ajax(ModelMap modelMap) throws Exception {
		return "/user_add_ajax";
	}

}

user_add_ajax.jsp 생성

사용자 추가 JSP 작성 - JQuery AJAX 통신

<%@ 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 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=UTF-8">
<script  src="http://code.jquery.com/jquery-latest.min.js"></script>
<title>사용자추가</title>
<script type="text/javascript">
function user_add() {
	var form = document.uploadForm;
    //var form = new FormData(document.getElementById('uploadForm'));
	$.ajax({
	  url: "<c:url value="/json.iot"/>",
	  data: {
		cmd : "user",
		subcmd : "add",
		user_id : form.user_id.value,
		password : form.password.value,
		name : form.name.value,
		nickname : form.nickname.value
	  },
	  // data: form,
	  dataType: 'json',
	  contentType: false,
	  type: 'GET',
	  success: function (response) {
	    console.log('success');
	    console.log(response);
	  },
	  error: function (jqXHR) {
	    console.log('error');
	  }
	});
}
</script>
</head>
<body>

<h1>사용자 추가</h1>
<form id="uploadForm" name="uploadForm" method="get">
	<input type="hidden" name="cmd" value="user">
	<input type="hidden" name="subcmd" value="add">
	<table border="1">
		<tr>
			<td>사용자ID</td>
			<td>
				<input type="text" name="user_id"/>
			</td>
		</tr>
		<tr>
			<td>암호</td>
			<td>
				<input type="text" name="password"/>
			</td>
		</tr>
		<tr>
			<td>사용자이름</td>
			<td>
				<input type="text" name="name"/>
			</td>
		</tr>
		<tr>
			<td>별명</td>
			<td>
				<input type="text" name="nickname"/>
			</td>
		</tr>
		<!-- 
		<tr>
			<td>사진</td>
			<td>
				<input type="file" name="photo"/>
			</td>
		</tr>
		-->
	</table>
	<br/>
	<input type="button" value="사용자추가" onclick="javascript:user_add();"/>
</form>

</body>
</html>


728x90
728x90

Spring MVC - JSTL Form radio 버튼

출처 : Spring MVC radiobutton and radiobuttons example

코드 객체 생성

package com.iot.db.domain;

public class CodeString {

	private String code;

	private String title;

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

}

jsp

<form:select path="breed">
	<form:options items="${allBreeds}" itemValue="code" itemLabel="title" />
</form:select>

select option

<select id="cmb_code_type" style="height:35px" onchange="javascript:listCode(1);">
	<c:forEach var="code" items="${list_code_type}" varStatus="status">
		<option value="<c:out value="${code.id}"/>" index="${status.index}">
			<c:out value="${code.title}"/>
		</option>
	</c:forEach>
</select>
728x90
728x90

Spring MVC Multipart Configuration (iot-servlet.xml 내용추가)

출처 : Spring MVC File Upload Example Tutorial - Spring MVC File Upload Example Tutorial – Single and Multiple Files
스프링(Spring) 파일 업로드(File Upload) :: 갱짱.study - 이갱짱 - Tistory

	<!-- ========================= Multipart Form-Data Resolver ========================= -->
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<!--
		<property name="maxUploadSize">
			<value>10000000</value>
		</property>
		-->
	</bean>

FileUploadController.java

package com.iot.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class FileUploadController {

	@RequestMapping(value = "/file_upload_form.iot", method = RequestMethod.GET)
	public String file_upload_form(ModelMap modelMap) {
		return "/file_upload_form";
	}
	
	@RequestMapping(value = "/file_upload_save.iot", method = RequestMethod.POST)
	public String file_upload_save(@RequestParam("uploadfile") MultipartFile uploadfile, ModelMap modelMap) {

		OutputStream out = null;
		PrintWriter printWriter = null;

		try {
			// 파일명 얻기
			String fileName = uploadfile.getOriginalFilename();
			// 파일의 바이트 정보 얻기
			byte[] bytes = uploadfile.getBytes();
			// 파일의 저장 경로 얻기
			String uploadPath = getDestinationLocation() + fileName;
			
			// 파일 객체 생성
			File file = new File(uploadPath);
			// 상위 폴더 존재 여부 확인
			if (!file.getParentFile().exists()) {
				// 상위 폴더가 존재 하지 않는 경우 상위 폴더 생성
				file.getParentFile().mkdirs();
			}
			
			// 파일 아웃풋 스트림 생성
			out = new FileOutputStream(file);
			// 파일 아웃풋 스트림에 파일의 바이트 쓰기
			out.write(bytes);
			
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (out != null) {
					out.close();
				}
				if (printWriter != null) {
					printWriter.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		return "/file_upload_form";
	}
	
	private String getDestinationLocation() {
		return "/업로드폴더/";
	}
}

file_upload_form.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="file_upload_save.iot" method="post" enctype="multipart/form-data">
		<fieldset>
			<table>
				<tr>
					<th>파일</th>
					<td><input type="file" name="uploadfile" required="required"></td>
				</tr>
				<tr>
					<td colspan="2">
						<input type="submit" value="작성">
						<input type="reset" value="취소">
					</td>
				</tr>
			</table>
		</fieldset>
	</form>
</body>
</html>

업로드 폴더 웹에 노출시키기

{tomcat_home}/conf/server.xml 내용 추가

<Context docBase="C:/업로드폴더" path="uploaddir" reloadable="true"/>

JQuery Ajax로 파일 업로드

출처 - Programming is Fun :: Spring MVC를 이용한 Ajax File upload

FileUploadController 내용 추가

	@RequestMapping(value = "/file_upload_form_ajax.iot", method = RequestMethod.GET)
	public String file_upload_form_ajax(ModelMap modelMap) {
		return "/file_upload_form_ajax";
	}

	@ResponseBody
	@RequestMapping(value = "/file_upload_save_ajax.iot", headers="Accept=*/*", method = RequestMethod.POST)
	public String file_upload_save_ajax(@RequestParam("uploadfile") MultipartFile uploadfile, ModelMap modelMap) {

		OutputStream out = null;

		try {
			String fileName = uploadfile.getOriginalFilename();
			byte[] bytes = uploadfile.getBytes();
			String uploadPath = getDestinationLocation() + fileName;// 저장경로
			
			File file = new File(uploadPath);
			if (!file.getParentFile().exists()) {
				file.getParentFile().mkdirs();
			}
			
			out = new FileOutputStream(file);
			out.write(bytes);
			
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (out != null) {
					out.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		return "true";
	}

file_upload_form_ajax.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script  src="http://code.jquery.com/jquery-latest.min.js"></script>
<title>Insert title here</title>
<script type="text/javascript">
$( document ).ready(function() {
  $('#btn-upload').on('click', function () {
    console.log('btn-upload');
    var form = new FormData(document.getElementById('uploadForm'));
    $.ajax({
      url: "file_upload_save_ajax.iot",
      data: form,
      dataType: 'text',
      processData: false,
      contentType: false,
      type: 'POST',
      success: function (response) {
        console.log('success');
        console.log(response);
      },
      error: function (jqXHR) {
        console.log('error');
      }
    });
  });
});
</script>
</head>
<body>
	<form action="file_upload_save.iot" method="POST" id="uploadForm" enctype="multipart/form-data">
		<fieldset>
			<table>
				<tr>
					<th>파일</th>
					<td><input type="file" name="uploadfile" required="required"></td>
				</tr>
				<tr>
					<td colspan="2">
						<input type="submit" value="작성">
						<input type="reset" value="취소">
					</td>
				</tr>
			</table>
		</fieldset>
	</form>
	<span id="btn-upload">파일업로드AJAX</span>
</body>
</html>


728x90
728x90

컨트롤러 클래스에서 입력받은 객체 전달

modelMap.addAttribute("userinfo", new UserInfo()); 내용 추가

package com.iot.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.iot.db.domain.UserInfo;
import com.iot.db.service.UserInfoService;

@Controller
public class IotController {

	@Autowired
	private UserInfoService service;

	@RequestMapping(value = "/hello_world.iot", method = RequestMethod.GET)
	public String hello_world(ModelMap modelMap) throws Exception {
		
		// 사용자 목록 출력
		List<UserInfo> userlist = service.listUserInfoFormData();
		modelMap.addAttribute("userlist", userlist);
		
		// 사용자 추가용 객체 설정
		modelMap.addAttribute("userinfo", new UserInfo());
		
		return "/hello_world";
	}

}

JSP에 Form 테그 추가

참고 : 쉼, 그리고 망설임 없는 마침표. :: [toby의스프링] 13장 - 스프링 @MVC #3

<%@ 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="form" uri="http://www.springframework.org/tags/form"%>

<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>

Hello World

<h1>사용자 조회</h1>
<table border="1">
	<c:forEach items="${userlist}" var="data" varStatus="loop">
		<tr>
			<td>${data.user_id}</td>
			<td>${data.password}</td>
			<td>${data.name}</td>
			<td>${data.nickname}</td>
		</tr>
	</c:forEach>
</table>

<h1>사용자 추가</h1>
<form:form id="dataForm" name="dataForm" modelAttribute="userinfo" action="user_add.iot" method="post">
	<table border="1">
		<tr>
			<td>사용자ID</td>
			<td>
				<form:input path="user_id"/>
			</td>
		</tr>
		<tr>
			<td>암호</td>
			<td>
				<form:input path="password"/>
			</td>
		</tr>
		<tr>
			<td>사용자이름</td>
			<td>
				<form:input path="name"/>
			</td>
		</tr>
		<tr>
			<td>별명</td>
			<td>
				<form:input path="nickname"/>
			</td>
		</tr>
	</table>
	<br/>
	<input type="submit"/>
</form:form>

</body>
</html>

컨트롤러 클래스에 사용자 추가 메소드 생성

UserInfo 객체를 인자로 받음

	// 사용자 추가
	@RequestMapping(value = "/user_add.iot", method = RequestMethod.POST)
	public String user_add(UserInfo userinfo, ModelMap modelMap) throws Exception {

		service.insertUserInfo(userinfo);

		// 사용자 목록 출력
		List<UserInfo> userlist = service.listUserInfoFormData();
		modelMap.addAttribute("userlist", userlist);

		// 사용자 추가용 객체 설정
		modelMap.addAttribute("userinfo", new UserInfo());

		return "/hello_world";
	}

서비스 클래스에 메소드 추가

	public void insertUserInfo(UserInfo userInfo) {
		userInfoMapper.insertUserInfo(userInfo);
	}

Mapper 인터페이스에 메소드 추가

public void insertUserInfo(UserInfo userInfo);

Mapper XML에 Insert질의문 추가

	<!-- insertUserInfo -->
	<insert id="insertUserInfo" parameterType="com.iot.db.domain.UserInfo"
		statementType="PREPARED">
		insert into user_info(
		<trim suffixOverrides=",">
			<if test="user_id != null">user_id ,</if>
			<if test="password != null">password ,</if>
			<if test="name != null">name ,</if>
			<if test="nickname != null">nickname ,</if>
		</trim>
		) values (
		<trim suffixOverrides=",">
			<if test="user_id != null">#{user_id, jdbcType=VARCHAR} ,</if>
			<if test="password != null">#{password, jdbcType=VARCHAR} ,</if>
			<if test="name != null">#{name, jdbcType=VARCHAR} ,</if>
			<if test="nickname != null">#{nickname, jdbcType=VARCHAR} ,</if>
		</trim>
		)
	</insert>

최종 IotController.java

package com.iot.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.iot.db.domain.UserInfo;
import com.iot.db.service.UserInfoService;

@Controller
public class IotController {

	@Autowired
	private UserInfoService service;

	@RequestMapping(value = "/hello_world.iot", method = RequestMethod.GET)
	public String hello_world(ModelMap modelMap) throws Exception {

		// 사용자 목록 출력
		List<UserInfo> userlist = service.listUserInfoFormData();
		modelMap.addAttribute("userlist", userlist);

		// 사용자 추가용 객체 설정
		modelMap.addAttribute("userinfo", new UserInfo());

		return "/hello_world";
	}

	// 사용자 추가
	@RequestMapping(value = "/user_add.iot", method = RequestMethod.POST)
	public String user_add(UserInfo userinfo, ModelMap modelMap) throws Exception {

		service.insertUserInfo(userinfo);

		// 사용자 목록 출력
		List<UserInfo> userlist = service.listUserInfoFormData();
		modelMap.addAttribute("userlist", userlist);

		// 사용자 추가용 객체 설정
		modelMap.addAttribute("userinfo", new UserInfo());

		return "/hello_world";
	}

}

최종 UserInfoMapper.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.iot.db.mapper.UserInfoMapper">

	<!-- insertUserInfo -->
	<insert id="insertUserInfo" parameterType="com.iot.db.domain.UserInfo"
		statementType="PREPARED">
		insert into user_info(
		<trim suffixOverrides=",">
			<if test="user_id != null">user_id ,</if>
			<if test="password != null">password ,</if>
			<if test="name != null">name ,</if>
			<if test="nickname != null">nickname ,</if>
		</trim>
		) values (
		<trim suffixOverrides=",">
			<if test="user_id != null">#{user_id, jdbcType=VARCHAR} ,</if>
			<if test="password != null">#{password, jdbcType=VARCHAR} ,</if>
			<if test="name != null">#{name, jdbcType=VARCHAR} ,</if>
			<if test="nickname != null">#{nickname, jdbcType=VARCHAR} ,</if>
		</trim>
		)
	</insert>

	<!-- listUserInfo -->
	<select id="listUserInfo" parameterType="map" resultType="com.iot.db.domain.UserInfo">
		select *
		  from user_info
	</select>

</mapper>
728x90
728x90

출처 : mybatis-spring – 마이바티스 스프링 연동모듈 | 매퍼 주입

DB 접속 설정(WebContent\WEB-INF\iot-datasource.xml)

데이터베이스 접속 정보(JDBC) 설정
JDBC는 각 데이터베이스 마다 제공되고 있고, 연결방식도 달라 따로 검색이 필요

<?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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:security="http://www.springframework.org/schema/security"
       xsi:schemaLocation="http://www.springframework.org/schema/aop       http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                           http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                           http://www.springframework.org/schema/context   http://www.springframework.org/schema/context/spring-context-2.5.xsd
                           http://www.springframework.org/schema/jee       http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
                           http://www.springframework.org/schema/tx        http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
                           http://www.springframework.org/schema/security  http://www.springframework.org/schema/security/spring-security-2.0.xsd">

	<!-- ========================= Resource Definitions ========================= --> 
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" >
		<property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>
		<property name="url" value="jdbc:mysql://localhost:3306/iot_db?user=iot&useUnicode=true&characterEncoding=UTF8&zeroDateTimeBehavior=convertToNull"/>
		<property name="username" value="iot"/>
		<property name="password" value="iot"/>
		<property name="maxActive" value="20"/>
		<property name="maxIdle" value="5"/>
		<property name="maxWait" value="2000"/>
		<property name="validationQuery" value="select 1"/>
		<property name="testWhileIdle" value="true"/>
		<property name="timeBetweenEvictionRunsMillis" value="7200000"/>        
	</bean>
	
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"/>
	</bean>	

	<tx:annotation-driven transaction-manager="transactionManager" />

	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="transactionFactory">
			<bean class="org.apache.ibatis.transaction.managed.ManagedTransactionFactory" />
		</property>
	</bean>

	<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
		<constructor-arg index="0" ref="sqlSessionFactory" />
	</bean>
	
</beans>

DB 접속정보 iot-servlet.xml에 추가

    <!-- ========================= Import Definitions ========================= -->
	<import resource="iot-datasource.xml" />

MyBatis Domain 패키지 생성

MyBatis Mapper 패키지 생성

MyBatis Service 패키지 생성

MyBatis 설정 파일 작성(WebContent\WEB-INF\iot-mybatis.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:aop="http://www.springframework.org/schema/aop"
     xmlns:tx="http://www.springframework.org/schema/tx"
     xmlns:jdbc="http://www.springframework.org/schema/jdbc"
     xmlns:context="http://www.springframework.org/schema/context"
     xsi:schemaLocation="
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
 
    <!-- enable component scanning (beware that this does not enable mapper scanning!) -->   
    <context:component-scan base-package="com.iot.db.service" />
         
    <!-- enable autowire -->
    <context:annotation-config />
 
    <!-- enable transaction demarcation with annotations -->
    <tx:annotation-driven />
 
    <!-- define the SqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="typeAliasesPackage" value="com.iot.db.domain" />
    </bean>
 
    <!-- scan for mappers and let them be autowired -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.iot.db.mapper" />
    </bean>
</beans>

user_info 예제 테이블 생성

CREATE TABLE `user_info` (
  `user_id` varchar(20) NOT NULL,
  `password` varchar(20) DEFAULT NULL,
  `name` varchar(20) DEFAULT NULL,
  `nickname` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`user_id`)
);

INSERT INTO `user_info` (`user_id`, `password`, `name`, `nickname`) VALUES
  ('test1', '1111', 'test1', NULL),
  ('test2', '1111', 'test2', NULL),
  ('test3', '1111', 'test3', NULL);

MyBatis Domain 클래스 생성

package com.iot.db.domain;

public class UserInfo {

	// pk
	private String user_id;

	private String password;
	private String name;
	private String nickname;

	public void setUser_id(String user_id) {
		this.user_id = user_id;
	}

	public String getUser_id() {
		return this.user_id;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPassword() {
		return this.password;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return this.name;
	}

	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

	public String getNickname() {
		return this.nickname;
	}
}

MyBatis Mapper 인터페이스 생성

package com.iot.db.mapper;

import java.util.List;

import com.iot.db.domain.UserInfo;

public interface UserInfoMapper {

	public List<UserInfo> listUserInfo();

}

MyBatis Mapper 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.iot.db.mapper.UserInfoMapper">

	<!-- listUserInfo -->
	<select id="listUserInfo" parameterType="map" resultType="com.iot.db.domain.UserInfo">
		select *
		  from user_info
	</select>

</mapper>

MyBatis Service 클래스 생성

package com.iot.db.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iot.db.domain.UserInfo;
import com.iot.db.mapper.UserInfoMapper;

@Service
public class UserInfoService {

	@Autowired
	private UserInfoMapper userInfoMapper;

	public List<UserInfo> listUserInfoFormData() throws Exception {
		return userInfoMapper.listUserInfo();
	}

}

컨트롤러 클래스 수정

서비스로 사용자리스트를 조회해서 modelMap에 사용자리스트(userlist)를 설정

package com.iot.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.iot.db.domain.UserInfo;
import com.iot.db.service.UserInfoService;

@Controller
public class IotController {

	@Autowired
	private UserInfoService service;

	@RequestMapping(value = "/hello_world.iot", method = RequestMethod.GET)
	public String hello_world(ModelMap modelMap) throws Exception {
		List<UserInfo> userlist = service.listUserInfoFormData();
		modelMap.addAttribute("userlist", userlist);
		return "/hello_world";
	}

}

hello_world.jsp에 데이터 전달

컨트롤러에서 전달받은 사용자리스트(userlist)를 반복해서 사용자정보를 출력

<%@ 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 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=UTF-8">
<title>Insert title here</title>
</head>
<body>

Hello World

<p></p>
<table border="1">
	<c:forEach items="${userlist}" var="data" varStatus="loop">
		<tr>
			<td>${data.user_id}</td>
			<td>${data.password}</td>
			<td>${data.name}</td>
			<td>${data.nickname}</td>
		</tr>
	</c:forEach>
</table>

</body>
</html>

실행결과

MyBatis 설정 파일 iot-servlet.xml에 추가

    <!-- ========================= Import Definitions ========================= -->
	<import resource="iot-mybatis.xml" />

최종 WebContent\WEB-INF\iot-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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:security="http://www.springframework.org/schema/security"
       xsi:schemaLocation="http://www.springframework.org/schema/aop       http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                           http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                           http://www.springframework.org/schema/context   http://www.springframework.org/schema/context/spring-context-2.5.xsd
                           http://www.springframework.org/schema/jee       http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
                           http://www.springframework.org/schema/tx        http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
                           http://www.springframework.org/schema/security  http://www.springframework.org/schema/security/spring-security-2.0.xsd">
 
 
	<!-- 컨트롤러 package 자동으로 스프링 컨테이너 등록 -->
	<context:component-scan base-package="com.iot.controller"/>
 
    <!-- ========================= JSP View Resolver ========================= -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
    
    <!-- ========================= Import Definitions ========================= -->
	<import resource="iot-datasource.xml" />
	<import resource="iot-mybatis.xml" />
 
</beans>


728x90

+ Recent posts