폴더구성
pom.xml 에서 라이브러리 추가
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.8</version>
</dependency>
Action.java
package spring;
public interface Action {
public abstract void sayHello1();
public abstract void sayHello2();
}
WriteAction.java
package spring;
public class WriteAction implements Action {
private String name;
public void setName(String name){
this.name = name;
}
@Override
public void sayHello1() {
// TODO Auto-generated method stub
System.out.println("sayHello1 시작");
System.out.println("Hello1 " + name);
System.out.println("sayHello1 끝");
}
@Override
public void sayHello2() {
// TODO Auto-generated method stub
System.out.println("sayHello2 시작");
System.out.println("Hello2 " + name);
System.out.println("sayHello2 끝");
}
}
LoginAdvice.java
package spring;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.util.StopWatch;
//xml에서 기술 했던부분을 java클래스에서 기술함
@Aspect
public class LoginAdvice {
@Pointcut("execution(* sayHello1())")
private void myTarget(){};
@Around("myTarget()")
public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable{
//전처리
String methodName = joinPoint.getKind();
StopWatch stopWatch = new StopWatch();
stopWatch.start(methodName);
System.out.println("[LOG] Method : "+methodName);
Object rtnObj =joinPoint.proceed();
//후처리
stopWatch.stop();
System.out.println("[LOG] Method : "+ methodName);
System.out.println("[LOG] 시간 : "+ stopWatch.getTotalTimeSeconds());
return rtnObj;
}
}
context.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
<bean id="action" class="spring.WriteAction">
<property name="name">
<value>홍길동</value>
</property>
</bean>
<bean id="loginAdvice" class="spring.LoginAdvice" />
<aop:aspectj-autoproxy />
</beans>
ApplicationMain.java
package spring;
import org.springframework.context.support.GenericXmlApplicationContext;
import spring.Action;
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"); //proxy 방식이 아니라서 여기서 코드 수정 안함
action.sayHello1();
action.sayHello2();
ctx.close();
}
}
LoginAdvice.java 사전처리만 추가
package spring;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.util.StopWatch;
//xml에서 기술 했던부분을 java클래스에서 기술함
@Aspect
public class LoginAdvice {
@Pointcut("execution(* sayHello1())")
private void myTarget(){};
@Around("myTarget()") //around 전후 처리
public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable{
//전처리
String methodName = joinPoint.getKind();
StopWatch stopWatch = new StopWatch();
stopWatch.start(methodName);
System.out.println("[LOG] Method : "+methodName);
Object rtnObj =joinPoint.proceed();
//후처리
stopWatch.stop();
System.out.println("[LOG] Method : "+ methodName);
System.out.println("[LOG] 시간 : "+ stopWatch.getTotalTimeSeconds());
return rtnObj;
}
@Before("execution(* sayHello2())") //before 사전처리만
public void before(){
System.out.println("[LOG] 전처리만 메서드 호출");
}
}
ApplicationMain.java
package spring;
import org.springframework.context.support.GenericXmlApplicationContext;
import spring.Action;
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"); //proxy 방식이 아니라서 여기서 코드 수정 안함
action.sayHello1();
System.out.println("---------------------");
action.sayHello2();
ctx.close();
}
}
2. MVC spring 서블릿프로젝트에 스프링 라이브러리를 복사하는 방법
폴더구성
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>MVCEx01</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>
<!-- 서블릿 설정 -->
<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>
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
<bean name="/list1.do" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
<property name="viewName" value="listView1.jsp"></property>
</bean>
</beans>
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
<!-- index.jsp -->
index.jsp<br />
<a href="list1.do">list1.do</a>
</body>
</html>
listView1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
<!-- listView1.jsp -->
listView1.jsp<br />
<a href="javascript:history.back()">뒤로가기</a>
</body>
</html>
3. webContext에 폴더 경로 설정
폴더구성
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>MVCEx01</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>
<!-- 서블릿 설정 -->
<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>
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
<!-- 가상 주소에 실제 주소를 연결함 -->
<bean name="/list1.do" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
<property name="viewName" value="listView1.jsp"></property>
</bean>
<bean name="/list2.do" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
<property name="viewName" value="listView2.jsp"></property>
</bean>
<bean name="/list3.do" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
<property name="viewName" value="./views/listView1.jsp"></property>
</bean>
<bean name="/list4.do" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
<property name="viewName" value="/WEB-INF/listView1.jsp"></property>
</bean>
<!-- "/board1/list1.do" 가상경로를 만듦 -->
<bean name="/board1/list1.do" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
<property name="viewName" value="/WEB-INF/listView1.jsp"></property>
</bean>
</beans>
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
<!-- index.jsp -->
index.jsp<br />
<a href="list1.do">list1.do</a>
<a href="list2.do">list2.do</a>
<a href="list3.do">list3.do</a>
<a href="list4.do">list4.do</a>
<a href="./board1/list1.do">list1.do</a>
</body>
</html>
listView1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
<!-- listView1.jsp -->
listView1.jsp<br />
<a href="javascript:history.back()">뒤로가기</a>
</body>
</html>
listView2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
<!-- listView1.jsp -->
listView2.jsp<br />
<a href="javascript:history.back()">뒤로가기</a>
</body>
</html>
views.listView1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
<!-- listView1.jsp -->
views.listView1.jsp<br />
<a href="javascript:history.back()">뒤로가기</a>
</body>
</html>
WEB-INF/listView1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
<!-- listView1.jsp -->
/WEB-INF/listView1.jsp<br />
<a href="javascript:history.back()">뒤로가기</a>
</body>
</html>
4. 실제주소 입력 단축
폴더구조
webContext2.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="org.springframework.web.servlet.mvc.ParameterizableViewController">
<property name="viewName" value="listView1"></property>
</bean>
<bean name="/list2.do" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
<property name="viewName" value="listView2"></property>
</bean>
<!-- 실제주소 앞뒤 문자열 연결 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".jsp" />
</bean>
</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_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>MVCEx01</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>
<!-- 서블릿 설정 -->
<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/webContext2.xml</param-value><!-- 사용할 webContext 파일 설정 -->
</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>
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
<!-- index.jsp -->
index.jsp<br />
<a href="list1.do">list1.do</a>
<a href="list2.do">list2.do</a>
</body>
</html>
5. 모델만들기
폴더구성
listAction1.java 만들때 Controller 상속받아야 함
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;
import com.sun.swing.internal.plaf.metal.resources.metal;
public class listAction1 implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
// TODO Auto-generated method stub
System.out.println("ListAction1 : handleRequest() 호출");
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("listView1");
//return new ModelAndView("listView1");
return modelAndView;
}
}
listAction2.java
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 : handleRequest() 호출");
return new ModelAndView("redirect:/index.jsp"); //바로 보냄
}
}
webContext3.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/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
web.xml 에서 사용할 webContext파일 명 바꿔줌
<?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>MVCEx01</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>
<!-- 서블릿 설정 -->
<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/webContext3.xml</param-value><!-- 사용할 webContext 파일 설정 -->
</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>
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
<!-- index.jsp -->
index.jsp<br />
<a href="list1.do">list1.do</a>
<a href="list2.do">list2.do</a>
</body>
</html>
/WEB-INF/listView1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
<!-- /WEB-INF/listView1.jsp -->
/WEB-INF/listView1.jsp<br />
<a href="javascript:history.back()">뒤로가기</a>
</body>
</html>
/WEB-INF/listView2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
<!-- /WEB-INF/listView2.jsp -->
/WEB-INF/listView2.jsp<br />
<a href="javascript:history.back()">뒤로가기</a>
</body>
</html>
댓글 없음:
댓글 쓰기