AOP
순차 적용
advisor1 advisor2 execute1
여러 클래스 적용
어제한 AOPEx02의 context에 order 순서를 적용해 보면
AOPEx02.spring.context
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<bean id="action" class="spring.WriteAction">
<property name="name">
<value>박문수</value>
</property>
</bean>
<bean id="LogginAdvice1" class="aop.LogginAdvice1" />
<bean id="LogginAdvice2" class="aop.LogginAdvice2" />
<aop:config>
<aop:aspect ref="LogginAdvice1" order="2">
<aop:pointcut id="LogPointCut" expression="execution(* execute1())" />
<aop:around method="logAround" pointcut-ref="LogPointCut"/>
</aop:aspect>
<aop:aspect ref="LogginAdvice2" order="1">
<aop:pointcut id="LogPointCut" expression="execution(* execute1())" />
<aop:around method="logAround" pointcut-ref="LogPointCut"/>
</aop:aspect>
</aop:config>
</beans>
loginPointCut으로 순서를 넣어서 별도로 적용할 경우
AOPEx02.spring.context
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<bean id="action" class="spring.WriteAction">
<property name="name">
<value>박문수</value>
</property>
</bean>
<bean id="LogginAdvice1" class="aop.LogginAdvice1" />
<bean id="LogginAdvice2" class="aop.LogginAdvice2" />
<aop:config>
<aop:aspect ref="LogginAdvice1" order="1">
<aop:pointcut id="LogPointCut1" expression="execution(* execute1())" />
<aop:around method="logAround" pointcut-ref="LogPointCut1"/>
</aop:aspect>
<aop:aspect ref="LogginAdvice2" order="2">
<aop:pointcut id="LogPointCut2" expression="execution(* execute2())" />
<aop:around method="logAround" pointcut-ref="LogPointCut2"/>
</aop:aspect>
</aop:config>
</beans>
Advice 타입별 클래스 작성
before 메서드 (method=before)
AOPEx02.aop.LogginAdvice1
package aop;
import org.aspectj.lang.ProceedingJoinPoint;
public class LogginAdvice1 {
public Object logAround(ProceedingJoinPoint joinpoint) throws Throwable {
System.out.println("1 전처리 구간");
Object rtnObj = joinpoint.proceed();
System.out.println("1 후처리 구간");
return rtnObj;
}
public void before() throws Throwable {
//사전 처리만 실행
System.out.println("before() 실행");
}
}
AOPEx02.spring.context
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<bean id="action" class="spring.WriteAction">
<property name="name">
<value>박문수</value>
</property>
</bean>
<bean id="LogginAdvice1" class="aop.LogginAdvice1" />
<bean id="LogginAdvice2" class="aop.LogginAdvice2" />
<aop:config>
<!--
<aop:aspect ref="LogginAdvice1" order="1">
<aop:pointcut id="LogPointCut1" expression="execution(* execute1())" />
<aop:around method="logAround" pointcut-ref="LogPointCut1"/>
</aop:aspect>
<aop:aspect ref="LogginAdvice2" order="2">
<aop:pointcut id="LogPointCut2" expression="execution(* execute2())" />
<aop:around method="logAround" pointcut-ref="LogPointCut2"/>
</aop:aspect>
-->
<aop:aspect ref="LogginAdvice1">
<aop:before method="before" pointcut="execution(* execute1())"/>
</aop:aspect>
</aop:config>
</beans>
after 메서드(method=after)
AOPEx02.aop.LogginAdvice1
package aop;
import org.aspectj.lang.ProceedingJoinPoint;
public class LogginAdvice1 {
public Object logAround(ProceedingJoinPoint joinpoint) throws Throwable {
System.out.println("1 전처리 구간");
Object rtnObj = joinpoint.proceed();
System.out.println("1 후처리 구간");
return rtnObj;
}
public void before() throws Throwable {
//사전 처리만 실행
System.out.println("before() 실행");
}
public void after() throws Throwable {
// 사전 처리만 실행
System.out.println("after() 실행");
}
}
AOPEx02.spring.context
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<bean id="action" class="spring.WriteAction">
<property name="name">
<value>박문수</value>
</property>
</bean>
<bean id="LogginAdvice1" class="aop.LogginAdvice1" />
<bean id="LogginAdvice2" class="aop.LogginAdvice2" />
<aop:config>
<!--
<aop:aspect ref="LogginAdvice1" order="1">
<aop:pointcut id="LogPointCut1" expression="execution(* execute1())" />
<aop:around method="logAround" pointcut-ref="LogPointCut1"/>
</aop:aspect>
<aop:aspect ref="LogginAdvice2" order="2">
<aop:pointcut id="LogPointCut2" expression="execution(* execute2())" />
<aop:around method="logAround" pointcut-ref="LogPointCut2"/>
</aop:aspect>
-->
<aop:aspect ref="LogginAdvice1">
<aop:before method="before" pointcut="execution(* execute1())"/>
<aop:after method="after" pointcut="execution(* execute1())"/>
</aop:aspect>
</aop:config>
</beans>
aop:around 한것과 같은결과가 나온다. 하는 방법이 다를 뿐이다.
어떤 클래스에서 어떤 메서드를 사용했는지 확인하는 법
AOPEx02.aop.LogginAdvice1
package aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
public class LogginAdvice1 {
public Object logAround(ProceedingJoinPoint joinpoint) throws Throwable {
System.out.println("1 전처리 구간");
Object rtnObj = joinpoint.proceed();
System.out.println("1 후처리 구간");
return rtnObj;
}
public void before(JoinPoint joinPoint) throws Throwable {
//사전 처리만 실행
String signatureString = joinPoint.getSignature().toShortString();
System.out.println("before() 실행 " + signatureString);
}
public void after() throws Throwable {
// 사전 처리만 실행
System.out.println("after() 실행");
}
}
Action.execute1() 라며 Action 인터페이스의 execute1() 메소드가 실행 되었음을 확인할 수 있다.
어노테이션 기법으로 가기전 단계
앞에서 사용했던 방식인 legacy project를 maven기반으로 선택 후 pom.xml에서 Spring 버전을 5.0.8로 수정후 아래 dependency에 AspectJ를 아래와 같이 추가한다.
AOPEx03.pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.samples</groupId>
<artifactId>AOPEx03</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<!-- Generic properties -->
<java.version>1.6</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- Spring -->
<spring-framework.version>5.0.8.RELEASE</spring-framework.version>
<!-- Hibernate / JPA -->
<hibernate.version>4.2.1.Final</hibernate.version>
<!-- Logging -->
<logback.version>1.0.13</logback.version>
<slf4j.version>1.7.5</slf4j.version>
<!-- Test -->
<junit.version>4.11</junit.version>
</properties>
<dependencies>
<!-- Spring and Transactions -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<!-- Logging with SLF4J & LogBack -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
<scope>runtime</scope>
</dependency>
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- Test Artifacts -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring-framework.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- AspectJ -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
</dependency>
</dependencies>
</project>
AOPEx03.spring.Action
package spring;
public interface Action {
public abstract void execute1();
public abstract void execute2();
}
AOPEx03.spring.WriteAction
package spring;
public class WriteAction implements Action {
private String name;
public void setName(String name) {
this.name = name;
}
@Override
public void execute1() {
// TODO Auto-generated method stub
System.out.println("execute1() 시작");
for(int i=0; i<=10; i++) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("Hello " + name);
System.out.println("execute1() 끝");
}
@Override
public void execute2() {
// TODO Auto-generated method stub
System.out.println("execute2() 시작");
for(int i=0; i<=10; i++) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("Hello " + name);
System.out.println("execute2() 끝");
}
}
AOPEx03.spring.ApplicationMain
package spring;
import org.springframework.context.support.GenericXmlApplicationContext;
public class ApplicationMain {
public static void main(String[] args) {
// TODO Auto-generated method stub
GenericXmlApplicationContext ctx
= new GenericXmlApplicationContext("classpath:spring/context.xml");
Action action = (Action)ctx.getBean("action");
//Action action = (Action)ctx.getBean("proxy");
System.out.println("시작");
action.execute1();
action.execute2();
System.out.println("끝");
}
}
AOPEx03.spring.context
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<bean id="action" class="spring.WriteAction">
<property name="name">
<value>박문수</value>
</property>
</bean>
</beans>
AOPEx03.aop.LogginAdvice1
package aop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class LogginAdvice1 {
@Pointcut("execution(* execute*())")
private void myTarget() {}
@Around("myTarget()")
public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("1. 전처리 구간");
Object rtnObj = joinPoint.proceed();
System.out.println("1. 후처리 구간");
return rtnObj;
}
}
AOPEx03.spring.context
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<bean id="action" class="spring.WriteAction">
<property name="name">
<value>박문수</value>
</property>
</bean>
<bean id="LogginAdvice1" class="aop.LogginAdvice1" />
<aop:aspectj-autoproxy />
</beans>
AOPEx03.aop.LogginAdvice2
package aop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class LogginAdvice2 {
@Pointcut("execution(* execute*())")
private void myTarget() {}
@Around("myTarget()")
public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("2. 전처리 구간");
Object rtnObj = joinPoint.proceed();
System.out.println("2. 후처리 구간");
return rtnObj;
}
}
AOPEx03.spring.context
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<bean id="action" class="spring.WriteAction">
<property name="name">
<value>박문수</value>
</property>
</bean>
<bean id="LogginAdvice1" class="aop.LogginAdvice1" />
<bean id="LogginAdvice2" class="aop.LogginAdvice2" />
<aop:aspectj-autoproxy />
</beans>
어노테이션 기법 활용
AOPEx03.di.BeanConfig
package di;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import aop.LogginAdvice1;
import spring.WriteAction;
@Configuration
@EnableAspectJAutoProxy
public class BeanConfig {
@Bean
public WriteAction action() {
WriteAction action = new WriteAction();
action.setName("박지호");
return action;
}
@Bean
public LogginAdvice1 logginAdvice1() {
return new LogginAdvice1();
}
}
AOPEx03.spring.ApplicationMain
package spring;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import di.BeanConfig;
public class ApplicationMain {
public static void main(String[] args) {
// TODO Auto-generated method stub
/*
GenericXmlApplicationContext ctx
= new GenericXmlApplicationContext("classpath:spring/context.xml");
*/
AnnotationConfigApplicationContext ctx
= new AnnotationConfigApplicationContext(BeanConfig.class);
Action action = (Action)ctx.getBean("action");
//Action action = (Action)ctx.getBean("proxy");
System.out.println("시작");
action.execute1();
action.execute2();
System.out.println("끝");
}
}
BeanConfig에 Advice2 추가
AOPEx03.di.BeanConfig
package di;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import aop.LogginAdvice1;
import aop.LogginAdvice2;
import spring.WriteAction;
@Configuration
@EnableAspectJAutoProxy
public class BeanConfig {
@Bean
public WriteAction action() {
WriteAction action = new WriteAction();
action.setName("박지호");
return action;
}
@Bean
public LogginAdvice1 logginAdvice1() {
return new LogginAdvice1();
}
@Bean
public LogginAdvice2 logginAdvice2() {
return new LogginAdvice2();
}
}
before, after을 사용해서 해보자
AOPEx03.aop.LogginAdvice1
package aop;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.context.annotation.Bean;
@Aspect
public class LogginAdvice1 {
/*
@Pointcut("execution(* execute*())")
private void myTarget() {}
@Around("myTarget()")
public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("1. 전처리 구간");
Object rtnObj = joinPoint.proceed();
System.out.println("1. 후처리 구간");
return rtnObj;
}
*/
@Bean("execution(* execute1())")
public void before() {
System.out.println("전처리");
}
@After("execution(* execute1())")
public void after() {
System.out.println("후처리");
}
}
스프링 MVC
model
view - spring을해서
controller - library가 필요
1. 기존 Dynamiac Web
- spring library
2. Maven (편리하지만, 복잡)
MVCEx01.Webcontent.WEB-INF.lib에 스프링 라이브러리 추가해주기
MVCEx01.webContext.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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
<!-- Controller 설정하는 문서 -->
<bean name="/list1.do" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
<property name="viewName" value="listview1.jsp" />
</bean>
</beans>
c. v (컨트롤러, 뷰)
list1.do -> listview1.jsp
list2.do -> listview2.jsp
MVCEx01.WebContent.index.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<a href="list1.do">list1.do</a><br />
</body>
</html>
MVCEx01.WebContent.listview1.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
listview1.jsp
</body>
</html>
MVCEx01.WebContent.index.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<a href="list1.do">list1.do</a><br />
<a href="./list2.do">list2.do</a><br />
</body>
</html>
MVCEx01.WebContent.listview2.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
listview2.jsp
</body>
</html>
MVCEx01.WebContent.webContext.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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
<!-- Controller 설정하는 문서 -->
<bean name="/list1.do" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
<property name="viewName" value="listview1.jsp" />
</bean>
<bean name="/list2.do" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
<property name="viewName" value="listview2.jsp" />
</bean>
</beans>
똑같은 파일을 디렉토리로 실행해보자
listview1과 listview2를 view 폴더로 옮겨주자
webContext.xml파일내 listview1과 listview2의 경로를 올바르게 수정해주자
MVCEx01.WebContent.WEB-INF.webContext.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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
<!-- Controller 설정하는 문서 -->
<bean name="/list1.do" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
<property name="viewName" value="./view/listview1.jsp" />
</bean>
<bean name="/list2.do" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
<property name="viewName" value="./view/listview2.jsp" />
</bean>
</beans>
위와 같이 url에 경로를 직접 작성하여 접근하고자 하면 에러가 뜬다. 이는 보안상의 이유로 WEB-INF 아래는 보안구역으로서 직접 들어가서 읽을수 없기 때문이다. 그러므로 스프링을 통해서 *.do로만 접근이 가능하다
MVCEx01.WebContent.WEB-INF.webContext.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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
<!-- Controller 설정하는 문서 -->
<bean name="/list1.do" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
<property name="viewName" value="/WEB-INF/view/listview1.jsp" />
</bean>
<bean name="/list2.do" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
<property name="viewName" value="/WEB-INF/view/listview2.jsp" />
</bean>
</beans>
인코딩을 설정해 줄때 매번 window -> preference -> workspace, css, html, jsp를 utf-8로 변경해주었는데 이걸 매번 하는게 귀찮으니 이것을 bean화 해보자
MVCEx01.WebContent.WEB-INF.webContext.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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
<!-- Controller 설정하는 문서 -->
<bean name="/list1.do" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
<property name="viewName" value="listview1" />
</bean>
<bean name="/list2.do" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
<property name="viewName" value="listview2" />
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="WEB-INF/view/" />
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
결과 화면 같음.
c. m. v (컨트롤러, 모델, 뷰)
list1.do -> ListAction1 -> listview1.jsp
list2.do -> ListAction2 -> listview2.jsp
MVCEx02.WebContent.WEB-INF.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_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>MVCEx</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>
<!-- Spring Controller 설정파일 지정 -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/webContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
MVCEx02.WebContent.WEB-INF.webContext.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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
</beans>
스프링 라이브러리 추가해주고 나머지도 위와 같이 세팅한다
MVCEx02.Java Resources.src.spring.ListAction1
package spring;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class ListAction1 implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
// TODO Auto-generated method stub
System.out.println("ListAction1() 생성");
return new ModelAndView("listview1");
}
}
MVCEx02.Java Resources.src.spring.ListAction2
package spring;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class ListAction2 implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
// TODO Auto-generated method stub
System.out.println("ListAction2() 생성");
return new ModelAndView("listview2");
}
}
MVCEx02.WebContent.WEB-INF.webContext.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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
<bean name="/list1.do" class="spring.ListAction1" />
<bean name="/list2.do" class="spring.ListAction2" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
MVCEx02.WebContent.index
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<a href="list1.do">list1.do</a><br />
<a href="./list2.do">list2.do</a><br />
</body>
게시판처럼 해보기
write.do
-> WriteAction
-> write.jsp
=> /WEB-INF/views/write.jsp
write_ok.do
-> WriteOkAction
-> write_ok.jsp
1. lib의 스프링 라이브러리 복사붙여넣기
2, web.xml 가져오기
3. view 폴더안에 있는 write_ok.jsp, write.jsp 두개 파일 복사붙여넣기
-> 내용은 수정
4. webContext.xml 생성 (내용만 붙이거나, 아니면 파일자체를 복사해서 내용을 수정)
5. index.jsp 가져오기
MVCEx03.WebContent.WEB-INF.view.write.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
write.jsp
</body>
</html>
MVCEx03.WebContent.WEB-INF.view.write_ok.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
write_ok.jsp
</body>
</html>
MVCEx03.WebContent.WEB-INF.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_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>MVCEx</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>
<!-- Spring Controller 설정파일 지정 -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/webContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
MVCEx03.WebContent.WEB-INF.webContext.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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
<bean name="/write.do" class="spring.WriteAction" />
<bean name="/write_ok.do" class="spring.WriteOkAction" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
MVCEx03.Java Resource.src.spring.WriteAction
package spring;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class WriteAction implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
// TODO Auto-generated method stub
System.out.println("WriteAction() 생성");
return new ModelAndView("write");
}
}
MVCEx03.Java Resource.src.spring.WriteOkAction
package spring;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class WriteOkAction implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
// TODO Auto-generated method stub
System.out.println("WriteOkAction() 생성");
return new ModelAndView("write_ok");
}
}
MVCEx03.WebContent.index
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<a href="./write.do">write.do</a><br />
</body>
MVCEx03.WebContent.WEB-INF.view.write.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- write.jsp -->
<form action="write_ok.do" method="get">
데이터 : <input type="text" name="data" />
<input type="submit" value="전송" />
</form>
<form action="write_ok.do" method="post">
데이터 : <input type="text" name="data" />
<input type="submit" value="전송" />
</form>
</body>
</html>
MVCEx03.Java Resource.src.spring.WriteOkAction
package spring;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class WriteOkAction implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
// TODO Auto-generated method stub
System.out.println("WriteOkAction() 생성");
System.out.println(arg0.getParameter("data"));
return new ModelAndView("write_ok");
}
}
한글인코딩필터(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_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>MVCEx</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>
<!-- 한글 인코딩 필터 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<!-- Spring Controller 설정파일 지정 -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/webContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
MVCEx03.Java Resource.src.spring.WriteOkAction
package spring;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class WriteOkAction implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
// TODO Auto-generated method stub
//arg0.setCharacterEncoding("utf-8");
System.out.println("WriteOkAction() 생성");
System.out.println(arg0.getParameter("data"));
return new ModelAndView("write_ok");
}
}
모델거쳐서 뷰로 가기
MVCEx03.Java Resource.src.spring.WriteOkAction
package spring;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class WriteOkAction implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
// TODO Auto-generated method stub
//arg0.setCharacterEncoding("utf-8");
System.out.println("WriteOkAction() 생성");
System.out.println(arg0.getParameter("data"));
arg0.setAttribute("data", arg0.getParameter("data"));
return new ModelAndView("write_ok");
}
}
MVCEx03.WebContent.WEB-INF.view.write_ok.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
write_ok.jsp
<br /><hr /><br />
받은 데이터 : <%=request.getAttribute("data") %><br />
</body>
</html>
=> 컨트롤러를 통과하고 모델을 통과해서 뷰로 간것임(편법)
제대로된 스프링의 MVC
MVCEx03.Java Resource.src.spring.WriteOkAction
package spring;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class WriteOkAction implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
// TODO Auto-generated method stub
//arg0.setCharacterEncoding("utf-8");
System.out.println("WriteOkAction() 생성");
System.out.println(arg0.getParameter("data"));
//arg0.setAttribute("data", arg0.getParameter("data"));
//return new ModelAndView("write_ok");
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("write_ok");
modelAndView.addObject("data", arg0.getParameter("data"));
return modelAndView;
}
}
EL(Expression Language) 식으로 해보자
MVCEx03.WebContent.WEB-INF.view.write_ok.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
write_ok.jsp
<br /><hr /><br />
받은 데이터 : <%=request.getAttribute("data") %><br />
받은 데이터 : ${ data } <br />
</body>
</html>
=> 스프링 MVC 웹 요청 처리 과정 - 그림참조 (page 268)
=> 스프링 MVC의 주요 구성요소 - 표에대한 설명 (page 269)
여지껏 한 게시판을 스프링형태로 바뀌어라
MVC
model2
* Action.java - 수정 (2)
execute() handleRequest()
model1
BoardTO.java
BoardDAO.java - 복사 (1)
servlet
BoardController.java - webContext.xml (3)
----------------------------
jsp파일
model2
* .jsp - 복사 (4)
MVCBoardEx01.WebContent.WEB-INF.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_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>MVCEx</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>
<!-- 한글 인코딩 필터 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<!-- Spring Controller 설정파일 지정 -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/webContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
MVCBoardEx01.WebContent.WEB-INF.webContext.xml - servlet 기능
<?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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
<bean id="/list.do" class="model2.ListAction"/>
<bean id="/write.do" class="model2.WriteAction"/>
<bean id="/write_ok.do" class="model2.WriteOkAction"/>
<bean id="/view.do" class="model2.ViewAction"/>
<bean id="/delete.do" class="model2.DeleteAction"/>
<bean id="/delete_ok.do" class="model2.DeleteOkAction"/>
<bean id="/modify.do" class="model2.ModifyAction"/>
<bean id="/modify_ok.do" class="model2.ModifyOkAction"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/paging/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
기존 model2 paging 폴더를 WebContent -> WEB-INF에 추가한다.
class 파일들은 MVCBoardEx01.src.model2 및 paging을 추가해준다.
ModelAndView를 통해서 jsp파일로 연결해주는 작업을 한다.
MVCBoardEx01.src.model2.ListAction
package model2;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import paging1.BoardDAO;
import paging1.BoardListTO;
public class ListAction implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse arg1) throws Exception {
// TODO Auto-generated method stub
int cpage = 1;
if(request.getParameter("cpage") != null
&& !request.getParameter("cpage").equals("")) {
cpage = Integer.parseInt(request.getParameter("cpage"));
}
BoardListTO listTO = new BoardListTO();
listTO.setCpage(cpage);
BoardDAO dao = new BoardDAO();
listTO = dao.boardList(listTO);
ModelAndView modelAndView = new ModelAndView("board_list1");
modelAndView.addObject("listTO", listTO);
return modelAndView;
}
}
MVCBoardEx01.src.model2.WriteAction
package model2;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class WriteAction implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
// TODO Auto-generated method stub
return new ModelAndView("board_write1");
}
}
MVCBoardEx01.src.model2.WriteOkAction
package model2;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import paging1.BoardDAO;
import paging1.BoardTO;
public class WriteOkAction implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse arg1) throws Exception {
// TODO Auto-generated method stub
BoardTO to = new BoardTO();
to.setSubject(request.getParameter("subject"));
to.setWriter(request.getParameter("writer"));
to.setMail(request.getParameter("mail1") + "@" + request.getParameter("mail2"));
to.setPassword(request.getParameter("password"));
to.setContent(request.getParameter("content"));
to.setWip(request.getRemoteAddr());
BoardDAO dao = new BoardDAO();
int flag = dao.boardWriteOk(to);
ModelAndView modelAndView = new ModelAndView("board_write1_ok");
modelAndView.addObject("flag",flag);
return modelAndView;
}
}
MVCBoardEx01.src.model2.viewAction
package model2;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import paging1.BoardDAO;
import paging1.BoardTO;
public class ViewAction implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse arg1) throws Exception {
// TODO Auto-generated method stub
String cpage = request.getParameter("cpage");
BoardTO to = new BoardTO();
to.setSeq(request.getParameter("seq"));
BoardDAO dao = new BoardDAO();
to = dao.boardView(to);
ModelAndView modelAndView = new ModelAndView("board_view1");
modelAndView.addObject("cpage", cpage);
modelAndView.addObject("to", to);
return modelAndView;
}
}
MVCBoardEx01.src.model2.DeleteAction
package model2;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import paging1.BoardDAO;
import paging1.BoardTO;
public class DeleteAction implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse arg1) throws Exception {
String cpage = request.getParameter("cpage");
BoardTO to = new BoardTO();
to.setSeq(request.getParameter("seq"));
BoardDAO dao = new BoardDAO();
to = dao.boardDelete(to);
ModelAndView modelAndView = new ModelAndView("board_delete1");
modelAndView.addObject("to", to);
modelAndView.addObject("cpage", cpage);
return modelAndView;
}
}
MVCBoardEx01.src.model2.DeleteOkAction
package model2;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import paging1.BoardDAO;
import paging1.BoardTO;
public class DeleteOkAction implements Controller{
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse arg1) throws Exception {
// TODO Auto-generated method stub
BoardTO to = new BoardTO();
to.setSeq(request.getParameter("seq"));
to.setPassword(request.getParameter("password"));
BoardDAO dao = new BoardDAO();
int flag = dao.boardDeleteOk(to);
ModelAndView modelAndView = new ModelAndView("board_delete1_ok");
modelAndView.addObject("flag", flag);
return modelAndView;
}
}
MVCBoardEx01.src.model2.ModifyAction
package model2;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import paging1.BoardDAO;
import paging1.BoardTO;
public class ModifyAction implements Controller{
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse arg1) throws Exception {
// TODO Auto-generated method stub
String cpage = request.getParameter("cpage");
BoardTO to = new BoardTO();
to.setSeq(request.getParameter("seq"));
BoardDAO dao = new BoardDAO();
to = dao.boardModify(to);
ModelAndView modelAndView = new ModelAndView("board_modify1");
modelAndView.addObject("to", to);
modelAndView.addObject("cpage", cpage);
return modelAndView;
}
}
MVCBoardEx01.src.model2.ModifyOkAction
package model2;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import paging1.BoardDAO;
import paging1.BoardTO;
public class ModifyOkAction implements Controller{
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse arg1) throws Exception {
// TODO Auto-generated method stub
String cpage = request.getParameter("cpage");
BoardTO to = new BoardTO();
to.setSeq(request.getParameter("seq"));
to.setPassword(request.getParameter("password"));
to.setSubject(request.getParameter("subject"));
to.setMail(request.getParameter("mail1") + "@" + request.getParameter("mail2"));
to.setContent(request.getParameter("content"));
BoardDAO dao = new BoardDAO();
int flag = dao.boardModifyOk(to);
ModelAndView modelAndView = new ModelAndView("board_modify1_ok");
modelAndView.addObject("flag", flag);
modelAndView.addObject("cpage", cpage);
modelAndView.addObject("seq", to.getSeq());
return modelAndView;
}
}
MVCBoardEx01.WebContent.index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%response.sendRedirect("./list.do"); %>
<a href="./list.do">list.do</a><br>
</body>
</html>
'Web & Mobile > Spring' 카테고리의 다른 글
Lecture 76 - Spring(6) Model2 게시판을 Spring으로, 전자정부프레임워크 설정법, 전자정부프레임워크 모바일 (0) | 2019.07.05 |
---|---|
Lecture 75 - Spring(5) Spring MVC 게시판(2), Maven (0) | 2019.07.04 |
Lecture 73 - Spring(3) 스프링 게시판 (0) | 2019.07.01 |
Lecture 72 - Spring(2) 스프링 프레임워크를 이용한 게시판 (0) | 2019.06.28 |
Lecture 71 - Spring(1) 스프링 프레임워크 설치법, (0) | 2019.06.27 |
댓글