๐ป
์คํ๋ง ํ๋ ์์ํฌ - [Spring] ์ด๋ ธํ ์ด์ (Annotation) ๋ณธ๋ฌธ
์คํ๋ง ํ๋ ์์ํฌ - [Spring] ์ด๋ ธํ ์ด์ (Annotation)
๋ํจ๋ 2020. 7. 31. 13:24Annotation?
์ด๋ ธํ ์ด์ ์ ๋ฉํ๋ฐ์ดํฐ๋ผ๊ณ ๋ณผ ์ ์๋ค.
์ ํ๋ฆฌ์ผ์ด์ ์ด ์ฒ๋ฆฌํด์ผ๋ ๋ฐ์ดํฐ๊ฐ ์๋๋ผ ์ปดํ์ผ ๊ณผ์ ๊ณผ ์คํ๊ณผ์ ์์ ์ฝ๋๋ฅผ ์ด๋ป๊ฒ ์ปดํ์ผํ๊ณ ์ฒ๋ฆฌํ ์ง๋ฅผ ์๋ ค์ค๋ค.(์ฐ์ด์ ์ฌ์ฉํ๋ ์ผ์ ์์)
1. ์ฉ๋
1) ์ปดํ์ผ๋ฌ์๊ฒ ์ฝ๋ ๋ฌธ๋ฒ ์๋ฌ๋ฅผ ์ฒดํฌํ๋๋ก ์ ๋ณด๋ฅผ ์ ๊ณต @Overrid
2) ์ํํธ์จ์ด ๊ฐ๋ฐ์ ๋น๋๋ ๋ฐฐํ์ฝ๋๋ฅผ ์๋์ผ๋ก ์์ฑํ ์ ์๋๋ก ์ ๋ณด๋ฅผ ์ ๊ณต (bean, aop๋ฑ)
3) ์คํ์ ํน์ ๊ธฐ๋ฅ์ ์คํํ๋๋ก ์ ๋ณด๋ฅผ ์ ๊ณต
โ
2. ์ข ๋ฅ
1) ์์คํ ์ ์ ์ด๋ ธํ ์ด์ : @Overrid @Deprecated @SupperssWarning
2) ์ปค์คํ (๊ฐ๋ฐ์) ์ด๋ ธํ ์ด์ :
2-1) ํ์ ๊ณผ ์ ์ - ํ์ , ์ด๋ฆ, ๋ํดํธ ๊ฐ
2-2) ์ ์ฉ ๋์ - METHOD, FLED, TYPE(CLASS), ์์ฑ์, PARAMETER ๋ฑ์ด ์๋ค.
2-3) ์ ์ง ์ ์ฑ - SOURCE, CLASS, RUNTIME
[Annotation ์์ฑ]
โ
โ
MainClass.java
package com.java.anno01;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class MainClass {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
Target t= new Target();
t.sub();
String className = "com.java.anno01.Target";
Class<?> cls = Class.forName(className); //ํด๋์ค
Target a = (Target) cls.getDeclaredConstructor().newInstance(); //๋์ ๊ฐ์ฒด ์์ฑ
a.sub();
Method[] methods = cls.getMethods();
for(int i=0; i<methods.length; i++) {
if(methods[i].isAnnotationPresent(MyAnno.class)) {
MyAnno myAnno = methods[i].getAnnotation(MyAnno.class);
System.out.println(myAnno.value());
}
}
}
}
[Annotation file] MyAnno.java
package com.java.anno01;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnno {
//ํ์
๊ณผ ์ ์
public String value();
}
Target.java
package com.java.anno01;
public class Target {
@MyAnno(value="์ฐ์ต์
๋๋ค.")
public void sub() {
System.out.println("Target: sub~~~~~~~~~~~~~~~~~");
}
}
[Annotation file] MyAnno.java
package com.java.anno02;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnno {
public String name() default "ํ๊ธธ๋";
public int age() default 27;
}
Target.java
package com.java.anno02;
public class Target {
@MyAnno //ํ๊ธธ๋, 27
public void subA() {
System.out.println("Target SubA Function ~~~~~~~~~~~~~~~");
}
@MyAnno(name="์ด์์") //์ด์์, 27
public void subB() {
System.out.println("Target SubB Function ~~~~~~~~~~~~~~~");
}
@MyAnno(age=33) //ํ๊ธธ๋, 33
public void subC() {
System.out.println("Target SubC Function ~~~~~~~~~~~~~~~");
}
@MyAnno(name="์กฐ์ธ์ฑ", age=40)
public void subD() {
System.out.println("Target SubD Function ~~~~~~~~~~~~~~~");
}
}
โ
MyAnno.java(Annotation file)
package com.java.anno03;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.TYPE, ElementType.CONSTRUCTOR, ElementType.FIELD,
ElementType.PARAMETER, ElementType.LOCAL_VARIABLE})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnno {
String value() default "Spring";
}
Target.java
package com.java.anno03;
@MyAnno()
public class Target {
private @MyAnno() String str;
@MyAnno("์ฐํํํ")
public Target() {}
public void sub(@MyAnno() String value) {
@MyAnno() int x = 10;
}
}
๊ฒ์ํ ํ๋ก์ ํธ Annotation ์ฌ์ฉํ๊ธฐ
xmlํ์ผ์ annotationํํ๋ก ๊ณ ์ณ ๊ฐ๋จํ๊ฒ ๋ฐ๊พผ๋ค.
- ๊ฒฐํฉ๋๋ฅผ ๋ฎ์ถ๊ณ ์ ์ง๋ณด์์ฑ์ ๋์ด๊ธฐ ์ํด xml๋ก ์ค์ ํ์์ผ๋ xml์ด ๋๋ฌด ๋ง์์ง๋ฉด ์คํ๋ ค ์ ์ง๋ณด์์ฑ์ด ๋ฎ์์ง๋ ํ์์ด ๋ฐ์
- ์ ์ง๋ณด์์ ์ค์ ์ ๋ง์ถ๊ธฐ ์ํด ์์คํ ์ ์ฒด์ ์ํฅ์ ์ฃผ๊ณ ์ดํ์ ๋ณ๊ฒฝ ๊ฐ๋ฅ์ฑ์ด ์๋ ๊ฒ์ xml๋ก ์ค์
โ
โ
SpringController-servlet.xmlโ
<aop:aspectj-autoproxy/> ํ๊ทธ๋ฅผ ์ค์ ํ์ผ์ ์ถ๊ฐํ๋ฉด @Aspect ์ด๋ ธํ ์ด์ ์ด ์ ์ฉ๋ ๋น์ Aspect๋ก ์ฌ์ฉํ ์ ์๊ฒ ๋๋ค.
โ
context:component-scan
: @Controller, @Service, @Repository, @Component๋ฑ์ ์ด๋ ธํ ์ด์ ์ component-scan์ xml์ ์ ์ธํด์ ์ฌ์ฉํจ.
base-package๋ ํจํค์ง๋ฅผ ์ด๋๋ถํฐ ์ค์บํ ์ง ์ง์ ํด์ฃผ๋ ๋ถ๋ถ. ์ค์บํ ํจํค์ง๋ฅผ ์ฌ๋ฌ๊ฐ๋ก ์ง์ ํด ์ค ์ ์๋ค.
๋ํดํธ ๊ฐ์ผ๋ก @Controller, @Service, @Repository, @Component๋ก ๋ฑ๋ก๋ ๋น์ ์ค์บํด์ค๋ค.
โ
๊ธฐ์กด XML์ ์ฌ์ฉํ ๋๋ HandlerMapping์ ์ฌ์ฉํ์์ง๋ง ์ด์ ๋ context:component-scan ๊ฐ ๊ทธ ์ญํ ์ ๋์ ํ๋ค.
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
<!-- Controller, Service, Dao -->
<context:component-scan base-package="com.java.*"/>
<!-- ViewResolver : MVC RequestDispatcher / forward -->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- AOP -->
<aop:aspectj-autoproxy/>
</beans>
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<!-- Spring DB -->
<bean id="driverManagerDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"/>
<property name="username" value="mvc"/>
<property name="password" value = "1234"/>
</bean>
<!-- MyBatis : sqlSessionFactoryBean-->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="driverManagerDataSource"/>
<property name="mapperLocations">
<list>
<value>classpath:com/java/member/dao/mapper/MemberMapper.xml</value>
<value>classpath:com/java/fileBoard/dao/mapper/FileBoardMapper.xml</value>
</list>
</property>
<!-- <property name="mapperLocations" value="classpath:com/java/member/dao/mapper/MemberMapper.xml"/> -->
</bean>
<!-- MyBatis : sqlsessionTemplate-->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg ref="sqlSessionFactoryBean"/>
</bean>
<!-- Transaction ๊ณตํตํด๋์ค -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="driverManagerDataSource"/>
</bean>
<!--transactionAOP -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" rollback-for="Exception"/>
</tx:attributes>
</tx:advice>
<!--propagation="REQUIRED" ์ด๋ฏธ ํ๋์ ํธ๋์ญ์
์ด ์กด์ฌํ๋ฉด ์ง์ํ๊ณ ์๋ค๋ฉด ์๋ก ์ง์ -->
<aop:config>
<aop:pointcut expression="within(com.java.*)" id="txPublic"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPublic"/>
</aop:config>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
<display-name>05_SpringMVC</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- DispatcherServlet -->
<servlet>
<servlet-name>SpringController</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/SpringController-servlet.xml
/WEB-INF/mybatis.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringController</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/SpringController-servlet.xml
/WEB-INF/mybatis.xml
</param-value>
</context-param>
<!-- ํ๊ธ ์์
-->
<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>/*</url-pattern>
</filter-mapping>
</web-app>
MemberController.java
@Autowired : ๊ฐ ์ํฉ์ ํ์ ์ ๋ง๋ IoC์ปจํ ์ด๋ ์์ ์กด์ฌํ๋ Bean์ ์๋์ผ๋ก ์ฃผ์ ํด์ค
๊ฐ์ฒด์ ์์กด์ฑ์ ๊ฐ์ง๋ ๋ถ๋ถ์ ๊ฐ๋จํ @Autowired์ด๋ ธํ ์ด์ ์ ์ฌ์ฉํ๋ฉด ์ฝ๊ฒ ์์กด์ฑ ์ฃผ์ ์ ๋ฐ์ ์ ์๊ฒ ๋จ.
์ฃผ์ ํ๋ ค๊ณ ํ๋ ๊ฐ์ฒด์ ํ์ ์ด ์ผ์นํ๋ ๊ฐ์ฒด๋ฅผ ์๋์ผ๋ก ์ฃผ์ ํ๋ค.
โ
์์กด๊ฐ์ฒด ์๋ ์ฃผ์ ?
์คํ๋ง ์ค์ ํ์ผ์์ ์์กด ๊ฐ์ฒด๋ฅผ ์ฃผ์ ํ ๋ <constructor-org> ๋๋ <property>ํ๊ทธ๋ก ์์กด ๋์ ๊ฐ์ฒด๋ฅผ ๋ช ์ํ์ง ์์๋ ์คํ๋ง ์ปจํ ์ด๋๊ฐ ์๋์ผ๋ก ํ์ํ ์์กด ๋์ ๊ฐ์ฒด๋ฅผ ์ฐพ์์ ์์กด ๋์ ๊ฐ์ฒด๊ฐ ํ์ํ ๊ฐ์ฒด์ ์ฃผ์ ํด ์ฃผ๋ ๊ธฐ๋ฅ์ด๋ค.
๊ตฌํ๋ฐฉ๋ฒ์ @Autowired์ @Resource ์ด๋ ธํ ์ด์ ์ด ์๋ค.
@Controller : ์ฃผ๋ก View๋ฅผ ๋ฐํํ๊ธฐ ์ํด ์ฌ์ฉ,
ํด๋น ํด๋์ค๊ฐ Controller์์ ๋ํ๋ด๊ธฐ ์ํ ์ด๋ ธํ ์ด์
@RequestMapping : ์์ฒญ์ ๋ํด ์ด๋ค Controller, ์ด๋ค ๋ฉ์๋๊ฐ ์ฒ๋ฆฌํ ์ง๋ฅผ ๋งคํํ๊ธฐ ์ํ ์ด๋ ธํ ์ด์ , URL์ ์ปจํธ๋กค๋ฌ์ ๋ฉ์๋์ ๋งคํํ ๋ ์ฌ์ฉํ๋ ์คํ๋ง ํ๋ ์ ์ํฌ์ ์ด๋ ธํ ์ด์
์ฆ, ํด๋ผ์ด์ธํธ๋ URL๋ก ์์ฒญ์ ์ ์กํ๊ณ ์์ฒญ URL์ ์ด๋ค ๋ฉ์๋๊ฐ ์ฒ๋ฆฌํ ์ง ์ฌ๋ถ๋ฅผ ๊ฒฐ์ ํ๋ ๊ฒ
@RequestMapping์ด ์ฌ์ฉํ๋ ์์ฑ
์ด๋ฆ |
ํ์ |
์ค๋ช |
value |
String[] |
URL ๊ฐ์ผ๋ก ๋งคํ ์กฐ๊ฑด์ ๋ถ์ฌ (default) |
method |
RequetMethod[] |
HTTP Request ๋ฉ์๋ ๊ฐ์ ๋งคํ ์กฐ๊ฑด์ผ๋ก ๋ถ์ฌ ์ฌ์ฉ ๊ฐ๋ฅํ ๋ฉ์๋๋ GET, POST, HEAD, OPTIONS, PUT, DELETE, TRACE (7๊ฐ) |
params |
String[] |
HTTP Request ํ๋ผ๋ฏธํฐ๋ฅผ ๋งคํ ์กฐ๊ฑด์ผ๋ก ๋ถ์ฌ |
consumes |
String[] |
์ค์ ๊ณผ Content-Type request ํค๋๊ฐ ์ผ์นํ ๊ฒฝ์ฐ์๋ง URL์ด ํธ์ถ๋จ |
produces |
String[] |
์ค์ ๊ณผ Accept request ํค๋๊ฐ ์ผ์นํ ๊ฒฝ์ฐ์๋ง URL์ด ํธ์ถ๋จ |
URL
https://blog.naver.com/yunnie222/221856149699
package com.java.member.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.servlet.view.InternalResourceView;
import com.java.aop.HAspect;
import com.java.member.dto.MemberDto;
import com.java.member.service.MemberService;
@Controller
public class MemberController{ //Command
@Autowired
private MemberService memberService;
@RequestMapping(value="/member/test.do", method=RequestMethod.GET)
public ModelAndView testing(HttpServletRequest request, HttpServletResponse response) {
System.out.println("OK");
//Service - Dao / Dto - Service
//์ต์์ ํด๋์ค๋ View
/*
InternalResourceView view = new InternalResourceView("/WEB-INF/member/test.jsp");
ModelAndView mav = new ModelAndView();
mav.addObject("msg", "hi");
mav.setView(view);
*/
//๋ค์ํ ์์ฑ์๊ฐ ์๊ธฐ์ ๊ทธ์ ๋ง์ถฐ์ ์ฌ์ฉ!!!
//ModelAndView mav = new ModelAndView(view);
ModelAndView mav = new ModelAndView("member/test");
mav.addObject("msg", "hi");
return mav;
}
@RequestMapping(value="/member/register.do", method=RequestMethod.GET)
public ModelAndView memberRegister(HttpServletRequest request, HttpServletResponse response) {
return new ModelAndView("member/register");
}
@RequestMapping(value="/member/registerOk.do", method=RequestMethod.POST)
public ModelAndView memberRegisterOk(HttpServletRequest request, HttpServletResponse response, MemberDto memberDto) {
HAspect.logger.info(HAspect.logMsg + memberDto.toString());
ModelAndView mav = new ModelAndView();
mav.addObject("memberDto", memberDto);
memberService.memberRegisterOk(mav);
return mav;
}
@RequestMapping(value="/member/idCheck.do", method=RequestMethod.GET)
public ModelAndView memberidCheck(HttpServletRequest request, HttpServletResponse response) {
ModelAndView mav = new ModelAndView();
mav.addObject("request", request);
memberService.memberidCheck(mav);
return mav;
}
@RequestMapping(value="/member/zipcode.do", method=RequestMethod.POST)
public ModelAndView memberZipcode(HttpServletRequest request, HttpServletResponse response) {
ModelAndView mav = new ModelAndView();
mav.addObject("request", request);
memberService.memberZipcode(mav);
return mav;
}
@RequestMapping(value="/member/login.do", method=RequestMethod.GET)
public ModelAndView memberLogin(HttpServletRequest request, HttpServletResponse response) {
return new ModelAndView("member/login");
}
@RequestMapping(value="/member/loginOk.do", method=RequestMethod.POST)
public ModelAndView memberLoginOk(HttpServletRequest request, HttpServletResponse response) {
ModelAndView mav = new ModelAndView();
mav.addObject("request", request);
memberService.memberloginOk(mav);
return mav;
}
@RequestMapping(value="/member/main.do", method=RequestMethod.GET)
public ModelAndView main(HttpServletRequest request, HttpServletResponse response) {
ModelAndView mav = new ModelAndView();
mav.addObject("request", request);
memberService.main(mav);
return mav;
}
@RequestMapping(value="/member/logout.do", method=RequestMethod.GET)
public ModelAndView memberLogout(HttpServletRequest request, HttpServletResponse response) {
ModelAndView mav = new ModelAndView();
mav.addObject("request", request);
memberService.memberloginOut(mav);
return mav;
}
@RequestMapping(value="/member/update.do", method=RequestMethod.GET)
public ModelAndView memberUpdate(HttpServletRequest request, HttpServletResponse response) {
//select ์ถ๊ฐ
ModelAndView mav = new ModelAndView();
mav.addObject("request", request);
memberService.memberUpdate(mav);
return mav;
}
@RequestMapping(value="/member/updateOk.do", method=RequestMethod.POST)
public ModelAndView memberUpdateOk(HttpServletRequest request, HttpServletResponse response) {
ModelAndView mav = new ModelAndView();
mav.addObject("request", request);
//memberService.memberUpdate(mav);
return mav;
}
}
[Interface] MemberService.java
package com.java.member.service;
import org.springframework.web.servlet.ModelAndView;
public interface MemberService {
public void memberRegisterOk(ModelAndView mav);
public void memberidCheck(ModelAndView mav);
public void memberZipcode(ModelAndView mav);
public void memberlogin(ModelAndView mav);
public void memberloginOk(ModelAndView mav);
public void main(ModelAndView mav);
public void memberloginOut(ModelAndView mav);
public void memberUpdateOk(ModelAndView mav);
public void memberUpdate(ModelAndView mav);
}
MemberServiceImp.java
@Component : bean์ผ๋ก ๋ฑ๋ก๋๊ธธ ์ํ๋ ํด๋์ค์ ์ถ๊ฐ
package com.java.member.service;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import com.java.aop.HAspect;
import com.java.member.dao.MemberDao;
import com.java.member.dto.MemberDto;
import com.java.member.dto.ZipcodeDto;
//๊ตฌํ ํด๋์ค
@Component
public class MemberServiceImp implements MemberService {
@Autowired
private MemberDao memberDao;
@Override
public void memberRegisterOk(ModelAndView mav) {
Map<String, Object> map = mav.getModelMap();
MemberDto memberDto = (MemberDto)map.get("memberDto");
memberDto.setMemberLevel("BA");
int check = memberDao.memberinsert(memberDto);
HAspect.logger.info(HAspect.logMsg + check);
mav.addObject("check", check);
mav.setViewName("member/registerOk");
}
@Override
public void memberidCheck(ModelAndView mav) {
Map<String, Object> map = mav.getModelMap();
HttpServletRequest request = (HttpServletRequest)map.get("request");
String id = request.getParameter("id");
HAspect.logger.info(HAspect.logMsg + id);
int check = memberDao.memberidCheck(id);
HAspect.logger.info(HAspect.logMsg + check);
mav.addObject("check", check);
mav.addObject("id", id);
mav.setViewName("member/idCheck");
}
@Override
public void memberZipcode(ModelAndView mav) {
Map<String, Object> map = mav.getModelMap();
HttpServletRequest request = (HttpServletRequest) map.get("request");
String dong = request.getParameter("dong");
if(dong!=null) {
List <ZipcodeDto> zipcodeDto = memberDao.zipcode(dong);
HAspect.logger.info(HAspect.logMsg + zipcodeDto.size());
mav.addObject("zipcodeList", zipcodeDto);
}
mav.setViewName("member/zipcode");
}
@Override
public void memberlogin(ModelAndView mav) {
// TODO Auto-generated method stub
}
@Override
public void memberloginOk(ModelAndView mav) {
Map<String, Object> map = mav.getModelMap();
HttpServletRequest request = (HttpServletRequest)map.get("request");
String id = request.getParameter("id");
String password=request.getParameter("password");
int check = memberDao.loginOk(id, password);
HAspect.logger.info(HAspect.logMsg + check);
mav.addObject("id", id);
mav.addObject("password", password);
mav.addObject("memberLevel", "BB");
mav.setViewName("member/loginOk");
}
@Override
public void main(ModelAndView mav) {
mav.setViewName("member/main");
}
@Override
public void memberloginOut(ModelAndView mav) {
Map<String, Object> map = mav.getModelMap();
HttpServletRequest request = (HttpServletRequest)map.get("request");
String id = request.getParameter("id");
String password=request.getParameter("password");
int check = memberDao.loginOk(id, password);
HAspect.logger.info(HAspect.logMsg + check);
mav.addObject("id", id);
mav.addObject("password", password);
mav.addObject("memberLevel", "BB");
mav.setViewName("member/logout");
}
@Override
public void memberUpdate(ModelAndView mav) {
Map<String, Object> map = mav.getModelMap();
HttpServletRequest request = (HttpServletRequest)map.get("request");
String id = request.getParameter("id");
String password=request.getParameter("password");
//int check = memberDao.loginOk(id, password);
HAspect.logger.info(HAspect.logMsg + id + "@@" + password);
mav.addObject("id", id);
mav.setViewName("member/update");
}
@Override
public void memberUpdateOk(ModelAndView mav) {
Map<String, Object> map = mav.getModelMap();
HttpServletRequest request = (HttpServletRequest)map.get("request");
String id = request.getParameter("id");
String password=request.getParameter("password");
int check = memberDao.loginOk(id, password);
HAspect.logger.info(HAspect.logMsg + check);
mav.addObject("id", id);
mav.addObject("password", password);
mav.addObject("memberLevel", "BB");
mav.setViewName("member/updateOk");
}
}
MemberDao.java
package com.java.member.dao;
import java.util.List;
import com.java.member.dto.MemberDto;
import com.java.member.dto.ZipcodeDto;
public interface MemberDao {
public int memberinsert(MemberDto memberDto);
public int memberidCheck(String id);
public List<ZipcodeDto> zipcode(String dong);
public int loginOk(String id, String password);
}
MemberDaoImp.java
package com.java.member.dao;
import java.util.List;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.java.member.dto.MemberDto;
import com.java.member.dto.ZipcodeDto;
@Component
public class MemberDaoImp implements MemberDao {
//My batis SqlSessionTemplate
@Autowired
private SqlSessionTemplate sqlSessionTemplate;
@Override
public int memberinsert(MemberDto memberDto) {
return sqlSessionTemplate.insert("member_insert", memberDto);
}
@Override
public int memberidCheck(String id) {
String value = sqlSessionTemplate.selectOne("id_check",id);
int check = 0;
if(value!=null) check=1;
return check;
}
@Override
public List<ZipcodeDto> zipcode(String dong) {
return sqlSessionTemplate.selectList("dto.memberDto.member_zipcode", dong);
}
@Override
public int loginOk(String id, String password) {
return 0;
}
}
ํ์ผ ๊ฒ์ํ ์ด๋ ธํ ์ด์ ์ผ๋ก ์์
fileBoardController.java
package com.java.fileBoard.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.java.fileBoard.dto.FileBoardDto;
import com.java.fileBoard.service.FileBoardService;
@Controller
public class FileBoardController{
@Autowired
private FileBoardService fileBoardService;
@RequestMapping(value="/fileBoard/write.do", method=RequestMethod.GET)
public ModelAndView fileBoardWrite(HttpServletRequest request, HttpServletResponse response) {
ModelAndView mav = new ModelAndView();
mav.addObject("request" , request);
fileBoardService.fileBoardWrite(mav);
return mav;
}
@RequestMapping(value="/fileBoard/writeOk.do", method=RequestMethod.POST)
public ModelAndView fileBoardWriteOk(HttpServletRequest request, HttpServletResponse response,FileBoardDto fileBoardDto) {
ModelAndView mav = new ModelAndView();
mav.addObject("request", request);
mav.addObject("fileBoardDto", fileBoardDto);
fileBoardService.fileBoardWriteOk(mav);
return mav;
}
@RequestMapping(value="/fileBoard/list.do", method=RequestMethod.GET)
public ModelAndView fileBoardList(HttpServletRequest request, HttpServletResponse response) {
ModelAndView mav = new ModelAndView();
mav.addObject("request", request);
fileBoardService.fileBoardList(mav);
return mav;
}
@RequestMapping(value="/fileBoard/read.do", method=RequestMethod.GET)
public ModelAndView fileBoardRead(HttpServletRequest request, HttpServletResponse response) {
ModelAndView mav = new ModelAndView();
mav.addObject("request", request);
fileBoardService.fileBoardRead(mav);
return mav;
}
@RequestMapping(value="/fileBoard/downLoad.do", method=RequestMethod.GET)
public void fileDownLoad(HttpServletRequest request, HttpServletResponse response) {
ModelAndView mav = new ModelAndView();
mav.addObject("request", request);
mav.addObject("response", response);
fileBoardService.fileDownLoad(mav);
}
@RequestMapping(value="/fileBoard/update.do", method=RequestMethod.GET)
public ModelAndView fileUpdate(HttpServletRequest request, HttpServletResponse response) {
ModelAndView mav = new ModelAndView();
mav.addObject("request", request);
fileBoardService.fileBoardUpdate(mav);
return mav;
}
@RequestMapping(value="/fileBoard/updateOk.do", method=RequestMethod.POST)
public ModelAndView fileUpdateOk(HttpServletRequest request, HttpServletResponse response,FileBoardDto fileBoardDto) {
ModelAndView mav = new ModelAndView();
mav.addObject("request", request);
fileBoardService.fileBoardUpdateOk(mav);
return mav;
}
}
[Interface] fileBoardService.java
package com.java.fileBoard.service;
import org.springframework.web.servlet.ModelAndView;
public interface FileBoardService {
public void fileBoardWrite(ModelAndView mav);
public void fileBoardWriteOk(ModelAndView mav);
public void fileBoardList(ModelAndView mav);
public void fileBoardRead(ModelAndView mav);
public void fileDownLoad(ModelAndView mav);
public void fileBoardUpdate(ModelAndView mav);
public void fileBoardUpdateOk(ModelAndView mav);
}
fileBoardServiceImp.java
package com.java.fileBoard.service;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.java.aop.HAspect;
import com.java.fileBoard.dao.FileBoardDao;
import com.java.fileBoard.dto.FileBoardDto;
@Component
public class FileBoardServiceImp implements FileBoardService {
@Autowired
private FileBoardDao fileBoardDao;
@Override
public void fileBoardWrite(ModelAndView mav) {
//๋ถ๋ชจ๊ธ(ROOT)
int boardNumber = 0; //ROOT๊ธ์ด๋ฉด 0
int groupNumber = 1; //๊ทธ๋ฃน๋ฒํธ
int sequenceNumber = 0; //๊ธ์์
int sequenceLevel = 0; //๊ธ๋ ๋ฒจ
Map<String, Object> map = mav.getModelMap();
HttpServletRequest request = (HttpServletRequest)map.get("request");
//๋ต๊ธ์ธ๊ฒฝ์ฐ ๋ถ๋ชจ๊ธ์ DB ๊ธ๋ฒํธ, ๊ทธ๋ฃน๋ฒํธ, ๊ธ์์, ๊ธ๋ ๋ฒจ
if(request.getParameter("boardNumber")!=null) {
boardNumber = Integer.parseInt(request.getParameter("boardNumber"));
groupNumber = Integer.parseInt(request.getParameter("groupNumber"));
sequenceNumber = Integer.parseInt(request.getParameter("sequenceNumber"));
sequenceLevel = Integer.parseInt(request.getParameter("sequenceLevel"));
}
mav.addObject("boardNumber", boardNumber);
mav.addObject("groupNumber", groupNumber);
mav.addObject("sequenceNumber", sequenceNumber);
mav.addObject("sequenceLevel", sequenceLevel);
mav.setViewName("fileBoard/write");
}
@Override
public void fileBoardWriteOk(ModelAndView mav) {
Map<String, Object> map = mav.getModelMap();
FileBoardDto fileBoardDto = (FileBoardDto) map.get("fileBoardDto");
MultipartHttpServletRequest request = (MultipartHttpServletRequest)map.get("request");
writeNumber(fileBoardDto);
fileBoardDto.setWriteDate(new Date());
fileBoardDto.setReadCount(0);
MultipartFile upFile = request.getFile("file");
HAspect.logger.info(HAspect.logMsg + fileBoardDto);
if(upFile!=null) {
//์ ์ฅ๊ฒฝ๋ก, ํ์ผ๋ช
, ์ฌ์ด์ฆ
String fileName = Long.toBinaryString(System.currentTimeMillis())+ "_" + upFile.getOriginalFilename();
long fileSize = upFile.getSize();
File path = new File("C:\\pds\\");
path.mkdir();
if(path.exists() && path.isDirectory()) {
File file = new File(path, fileName);
try {
upFile.transferTo(file);
fileBoardDto.setPath(file.getAbsolutePath());
fileBoardDto.setFileName(fileName);
fileBoardDto.setFileSize(fileSize);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
HAspect.logger.info(HAspect.logMsg +fileBoardDto);
int check = fileBoardDao.fileBoardWrite(fileBoardDto);
HAspect.logger.info(HAspect.logMsg + check);
mav.addObject("check", check);
mav.setViewName("fileBoard/writeOk");
}
public void writeNumber(FileBoardDto boardDto) {
// ๊ทธ๋ฃน๋ฒํธ(ROOT), ๊ธ์์(์์), ๊ธ๋ ๋ฒจ(์์)
int boardNumber = boardDto.getBoardNumber(); // 0
int groupNumber = boardDto.getGroupNumber(); // 1
int sequenceNumber = boardDto.getSequenceNumber(); // 0
int sequenceLevel = boardDto.getSequenceLevel(); // 0
if (boardNumber == 0) { // ROOT : ๊ทธ๋ฃน๋ฒํธ
int max = fileBoardDao.fileBoardGroupNumberMax();
if (max != 0)
boardDto.setGroupNumber(max + 1);
} else { // ๋ต๊ธ : ๊ธ ์์, ๊ธ ๋ ๋ฒจ
HashMap<String, Integer> hMap = new HashMap<String, Integer>();
hMap.put("groupNumber", groupNumber);
hMap.put("sequenceNumber", sequenceNumber);
int check = fileBoardDao.fileBoardWriteNumber(hMap);
sequenceNumber = sequenceNumber + 1;
sequenceLevel = sequenceLevel + 1;
boardDto.setSequenceNumber(sequenceNumber);
boardDto.setSequenceLevel(sequenceLevel);
}
}
@Override
public void fileBoardList(ModelAndView mav) {
Map<String, Object> map = mav.getModelMap();
HttpServletRequest request = (HttpServletRequest)map.get("request");
String pageNumber = request.getParameter("pageNumber");
if (pageNumber == null)
pageNumber = "1";
int currentPage = Integer.parseInt(pageNumber); // ์์ฒญ 1page
HAspect.logger.info(HAspect.logMsg +"currentPage" + currentPage);
int boardSize = 5; // view์ ๋ช๊ฐ์ ๊ธ์ ๋ณด์ฌ์ค์ง
int startRow = (currentPage - 1) * boardSize + 1; // ์์๋ฒํธ 1 11
int endRow = currentPage * boardSize; // ๋๋ฒํธ 10 20
int count = fileBoardDao.fileBoardCount();
HAspect.logger.info(HAspect.logMsg + count);
List<FileBoardDto> boardList = null;
if (count > 0) {
// startRow, endRow
boardList = fileBoardDao.fileBoardList(startRow, endRow);
HAspect.logger.info(HAspect.logMsg + boardList.size());
}
mav.addObject("boardList", boardList);
mav.addObject("boardSize", boardSize);
mav.addObject("currentPage", currentPage);
mav.addObject("count", count);
mav.setViewName("fileBoard/list");
}
@Override
public void fileBoardRead(ModelAndView mav) {
Map<String, Object> map = mav.getModelMap();
HttpServletRequest request = (HttpServletRequest)map.get("request");
int boardNumber = Integer.parseInt(request.getParameter("boardNumber"));
int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
HAspect.logger.info(HAspect.logMsg + boardNumber + "," + pageNumber);
FileBoardDto fileBoardDto = fileBoardDao.fileBoardRead(boardNumber);
HAspect.logger.info(HAspect.logMsg + fileBoardDto);
if(fileBoardDto.getFileName() !=null) {
int index=fileBoardDto.getFileName().indexOf("_")+1;
fileBoardDto.setFileName(fileBoardDto.getFileName().substring(index));
}
mav.addObject("boardDto", fileBoardDto);
mav.addObject("pageNumber", pageNumber);
mav.setViewName("fileBoard/read");
}
@Override
public void fileDownLoad(ModelAndView mav) {
Map<String, Object> map = mav.getModelMap();
HttpServletRequest request = (HttpServletRequest)map.get("request");
HttpServletResponse response = (HttpServletResponse)map.get("response");
int boardNumber = Integer.parseInt(request.getParameter("boardNumber"));
HAspect.logger.info(HAspect.logMsg + boardNumber);
FileBoardDto fileBoardDto = fileBoardDao.filBoardSelect(boardNumber);
HAspect.logger.info(HAspect.logMsg + fileBoardDto);
int index = fileBoardDto.getFileName().indexOf("_") + 1;
String fName = fileBoardDto.getFileName().substring(index);
String fileName;
try {
fileName = new String(fName.getBytes("utf-8"), "ISO-8859-1");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
long fileSize = fileBoardDto.getFileSize();
String path = fileBoardDto.getPath();
// ์ธํ
์ด ํ์
response.setHeader("Content-Disposition", "attachment;filename=" + fName);
response.setContentType("application/octet-stream");
response.setContentLengthLong(fileSize);
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream(path), 1024);
bos = new BufferedOutputStream(response.getOutputStream(), 1024);
while(true) {
int data = bis.read();
if(data == -1) break;
bos.write(data);
}
bos.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(bis!=null) bis.close();
if(bos!=null) bos.close();
}catch(Exception e) {
e.printStackTrace();
}
}
}
@Override
public void fileBoardUpdate(ModelAndView mav) {
Map<String, Object> map = mav.getModelMap();
HttpServletRequest request = (HttpServletRequest)map.get("request");
int boardNumber = Integer.parseInt(request.getParameter("boardNumber"));
int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
if(request.getParameter("boardNumber")!=null) {
boardNumber=Integer.parseInt(request.getParameter("boardNumber"));
pageNumber=Integer.parseInt(request.getParameter("pageNumber"));
}
FileBoardDto fileBoardDto = fileBoardDao.fileBoardUpdate(boardNumber);
HAspect.logger.info(HAspect.logMsg + fileBoardDto);
mav.addObject("boardNumber", boardNumber);
mav.addObject("pageNumber", pageNumber);
mav.addObject("boardDto", fileBoardDto);
HAspect.logger.info(HAspect.logMsg + "boardNumber" + pageNumber);
mav.setViewName("fileBoard/update");
}
@Override
public void fileBoardUpdateOk(ModelAndView mav) {
System.out.println("dddddd");
Map<String, Object> map = mav.getModelMap();
HttpServletRequest request = (HttpServletRequest)map.get("request");
int boardNumber = Integer.parseInt(request.getParameter("boardNumber"));
int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
FileBoardDto fileBoardDto = fileBoardDao.filBoardSelect(boardNumber);
fileBoardDto.setWriter(request.getParameter("name"));
fileBoardDto.setSubject(request.getParameter("subject"));
fileBoardDto.setEmail(request.getParameter("email"));
fileBoardDto.setContent(request.getParameter("content"));
fileBoardDto.setPassword(request.getParameter("password"));
fileBoardDto.setWriteDate(new Date()); //์๊ฐ
int check = fileBoardDao.fileBoardupdateOk(boardNumber, fileBoardDto);
HashMap<String, Object> hMap = new HashMap<String, Object>();
hMap.put("boardNumber", boardNumber);
hMap.put("fileBoardDto", fileBoardDto);
mav.addObject("check", check);
mav.addObject("pageNumber", pageNumber);
mav.setViewName("fileBoard/updateOk");
}
}
fileBoardDao.java
package com.java.fileBoard.dao;
import java.util.HashMap;
import java.util.List;
import com.java.fileBoard.dto.FileBoardDto;
public interface FileBoardDao {
public int fileBoardGroupNumberMax();
public int fileBoardWriteNumber(HashMap<String, Integer> hMap);
public int fileBoardWrite(FileBoardDto fileBoardDto);
public int fileBoardCount();
public List<FileBoardDto> fileBoardList(int startRow, int endRow);
public FileBoardDto fileBoardRead(int boardNumber);
public FileBoardDto filBoardSelect(int boardNumber);
public FileBoardDto fileBoardUpdate(int boardNumber);
public int fileBoardupdateOk(int boardNumber, FileBoardDto fileBoardDto);
}
fileBoardDaoImp.java
package com.java.fileBoard.dao;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.java.fileBoard.dto.FileBoardDto;
@Component
public class FileBoardDaoImp implements FileBoardDao {
@Autowired
private SqlSessionTemplate sqlSessionTemplate;
@Override
public int fileBoardGroupNumberMax() {
return sqlSessionTemplate.selectOne("board_group_number_max");
}
@Override
public int fileBoardWriteNumber(HashMap<String, Integer> hMap) {
return sqlSessionTemplate.update("board_writer_number", hMap);
}
@Override
public int fileBoardWrite(FileBoardDto fileBoardDto) {
return sqlSessionTemplate.insert("board_insert",fileBoardDto);
}
@Override
public int fileBoardCount() {
return sqlSessionTemplate.selectOne("board_count");
}
@Override
public List<FileBoardDto> fileBoardList(int startRow, int endRow) {
Map<String, Integer> hMap = new HashMap<String, Integer>();
hMap.put("startRow", startRow);
hMap.put("endRow", endRow);
return sqlSessionTemplate.selectList("board_list", hMap);
}
@Override
public FileBoardDto fileBoardRead(int boardNumber) {
sqlSessionTemplate.update("board_read_count", boardNumber);
return sqlSessionTemplate.selectOne("board_read", boardNumber);
}
@Override
public FileBoardDto filBoardSelect(int boardNumber) {
return sqlSessionTemplate.selectOne("board_read", boardNumber);
}
@Override
public FileBoardDto fileBoardUpdate(int boardNumber) {
return sqlSessionTemplate.selectOne("board_read", boardNumber);
}
@Override
public int fileBoardupdateOk(int boardNumber, FileBoardDto fileBoardDto) {
Map<String, Object> hMap = new HashMap<String, Object>();
hMap.put("boardNumber", boardNumber);
hMap.put("boardDto", fileBoardDto);
//sqlSessionTemplate.update("board_read_count", boardNumber);
return sqlSessionTemplate.update("board_update", hMap);
}
}