[ Spring ] 프레임워크 의 일종.(Enterprises Java Bean : EJB - Spring 최초버전) 모든 라이브러리는 API 중심 (점프 하지 않아도 되도록)
[Spring 과 Servlet의 차이점] 1. Spring 모두 class <=> Servlet Module : Class, Controller : servlet 2. GET 과 POST 에 대한 parameter 값 - Spring : ①HttpServletRequest, ②인자값, ③DAO(Setter) - Servlet : HttpServletRequest 3. Database 연결구조 - Spring : class, xml(bean), properties - Servlet : class, properties 4. MVC 형태 - Spring : Integer, int, long...(숫자 관련 메소드 외에 모두 사용가능) - Servlet : doPost, doGet, doService(Servlet에서 정해진 메소드만 활용) 5. 라이브러리 형태 - Spring : pom.xml 전체 로드하여 라이브러리 활용 - Servlet : 직접 프로젝트에 라이브러리를 load해서 사용해야함 6. View 형태 - Spring : JSP, JSTL - Servlet : JSP
[ STS2 와 STS3의 차이점 ] - STS 2 web client => pom.xml => web.xml => Dispatcher(접속보안, 언어셋, 파일첨부) => bean.xml => java class 파일 여러개 웹 파일 (만들떄 마다 bean에 추가해야함) =>model + view + controller => jsp, jstl, jspx
기존의 프레임워크는 .java 파일은 class 혹은 sevlet을 쓰임에 따라 따로 생성해줘야했지만 Spring의 Dispatcher를 사용하는것으로 class 만 만들면 web으로 바로 출력할 수 있게 함
[ Maven Project, Legacy Project ]
- Maven : 개발자가 모든 라이브러리 부터 모든 XML 까지 직접 구성 - Legacy : 기본적인 Controller View을 미리 세팅 단, 그 외 DB, DAO, Bean, Properties 직접 추가 구성
● Spring 기초 1-1. STS 버전별 구조 차이1 ( STS 3 )
- STS3 방식 ⑴ webpage.xml 에서 mainpage.java 를 핸들링 ⑵ webpage.xml 구성
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:beans="http://www.springframework.org/schema/beans"
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.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- @를 일반 class 파일에 사용할 수 있도록 하겠다는 것 -->
<annotation-driven></annotation-driven>
<resources location="/" mapping="/**"></resources>
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- prefix : view 페이지를 보여줄 경로, suffix : view 페이지 파일 속성명 -->
<beans:property name="prefix" value="/"></beans:property>
<beans:property name="suffix" value=".jsp"></beans:property>
</beans:bean>
<!-- class를 로드하는 부분(controller) -->
<beans:bean class="webspring.mainpage"></beans:bean>
</beans:beans>
⑶ mainpage.java 코드 - do가 먼저 실행해야 jsp를 불러오는 것
- 가상 웹 페이지로는 int, Integer, float, long 안됨, ArrayList<Integer> 는 가능 - void , String , StringBuffer, ModelandView 는 작동 가능함 - 배열클래스도 작동가능함(ArrayList, List, Map, LinkedList)
@Controller
public class mainpage {
//가상의 웹 페이지1
@RequestMapping("/mainpage.do")
public void main() {
}
//가상의 웹 페이지2
@RequestMapping("/mainpage2.do")
public void main2() {
}
//가상의 웹 페이지 3
@RequestMapping("/mainpage3.do")
public ModelAndView main3() {
return null;
}
//가상의 웹 페이지 4
@RequestMapping("/mainpage4.do")
public ArrayList<Integer> main4() {
return null;
}
}
⑷ jsp 와 do 파일 연결 방법 - ⓐ 파란 박스 : 최상위 디렉토리에서 .jsp 파일을 찾아냄 - ⓑ 빨간 박스 : ①에 .jsp 를 입력하지 않아도 jsp를 찾음 =>suffix에 .jsp를 썻기 때문에 return에는 jsp를 쓰면 안됨webpage.xmlmainpage.java
⑸ do 에서 data 전송
① attribute 로 data 전송 - spring 은 response 가 없음 (script찍을 때는 사용함 - PrintWriter 사용시, 단 View 페이지는 제작하지 않음) - response 를 사용시 JSTL 을 사용하지 못함. 일반 JSP를 사용해야함(Model 사용못함) 결과 출력 ∴ STS3 은 Controller 를 @로 불러와서 간편하게 사용가능함
●Spring 기초 1-2. STS 버전별 구조 차이 2 ( STS 2 )
- STS2 방식(sts3이해를 위해) ⑴ webpage2.xml 생성 ⑵ webpage2.xml 에 test.do 올리기
⑶ test.java 생성 - do 파일을 먼저 실행 시켜야 jsp 에서 do를 가져올 수 있음 - POST. GET 모두 사용가능한 핸들러임
public class test implements Controller {
//POST, GET을 모두 사용가능한 핸들러임
@Override
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView mv = new ModelAndView();
mv.setViewName("test.jsp");
return mv;
}
}
∴ 1-1 의 STS3과 비교해서 STS2는 Controller를 implements 해서 Override 시켜서 ModelAndView 사용하여 return 시켜줘야했음 . ∴ servlet 과 별반 차이 없음
⑷ JSTL 형태로 출력해보기 - do 가 먼저 실행되어야 jsp에서 do를 가져 올 수 있으므로 실행할 때 .jsp 가 아니라 .do를 url에 넣고 실행시켜야함 ① test.java
public class test implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
//Attribute를 보다 쉽게 사용할 수 있도록 변환된 클래스임(Spring 전용)
ModelAndView mv = new ModelAndView();
String username = "홍길동";
int age = 33;
//해당 변수값 및 key이름으로 배열 형태로 추가 됨
mv.addObject("username", username);
mv.addObject("userage", age);
mv.setViewName("test.jsp");
return mv;
}
}
public class test implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
//Attribute를 보다 쉽게 사용할 수 있도록 변환된 클래스임(Spring 전용)
ModelAndView mv = new ModelAndView();
String search = request.getParameter("search");
System.out.println(search);
return mv;
}
}