행복하게사는게꿈 2020. 7. 6. 17:17

struts-2.0.14 버전을 사용

 

struts2를 사용하기 위한 jar파일

여기있는 것들을 extends라는 키워드를 통해 사용 가능

 

필터는 싱글톤 패턴으로 관리

 


액션 클래스 생성해보기

 

1. struts.properties 파일생성

# struts.properties

struts.i18n.encoding=UTF-8

# struts.multipart.parser=pell

# struts.multipart.parser=jakarta

# uses javax.servlet.context.tempdir by default

# struts.multipart.saveDir=

# 100MB

struts.multipart.maxSize=104857600

#do로 확장자 변경할려면 이거 필요

struts.action.extension=do

#쓸데없는 로그 안보려고? 개발자모드로

struts.devMode = false 

#struts.configuration.xml.reload=true

struts.ui.theme=simple

 

2.struts.xml 파일 생성

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC

    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

  <!-- <constant name="struts.enable.DynamicMethodInvocation" value="false" />
  
  <constant name="struts.devMode" value="false" />
  
  <constant name="struts.i18n.encoding" value="UTF-8" />
  
  <constant name="struts.action.extension" value="do" /> -->
<!-- 
	스트럿츠 설정파일의 규칙 : 반드시 한개 이상의 패키지가 존재해야하고, 해당 패키지 내에는 반드시 한개의 액션이 선언되어있어야 한다.
  	
  	package : 모듈 단위.
    
  			  name - 다른 패키지와 구분을 위한 구분자(유일한 이름으로 명명
              
  			  extends - 스트럿츠 프레임워크가 제공하는 자원들을 상속
              
  			  			다른 기타 패키지의 상속을 위해서도 활용.
                        
  			  			상속가능한 패키지가 별도로 존재. - (abstact=true 일때만 가능)
                        
  			  						                  - 액션이 포함되지 않아야함
                                               
  			  namespace - 클라이언트 요청시 URI에서 해당 패키지에 접근이 가능한 URI 맵핑과 해당 패키지 내에 선언된 액션의 접근 맵핑
  			  							       
  			  action - 클라이언트 요청시 URI에서 해당 패키지에 접근으로  해당 패키지에 선언된 액션 접근 맵핑
  			  			name - 클라이언트 요청시의 서블릿 매핑 처리
  			  			class - 클라이언트 요청시 해당 요청을 처리하기 위한 비즈니스 로직이 요구 될 때 패키지명, 클래스명 선언 (필요없으면 생략가능)
  			  			        * 단순한 정적인 응답 컨텐츠만을 클라이언트 제공시 생략 가능
  			  			result - view를 결정, 다른 액션 대상의 리다이렉트 및 포워딩 처리 가능
  			  					 name="success", type="dispatcher" -> 포워딩 처리가 default값
  			  					 
  			  					  
  			 .do라는 확장자로 요청이 들어오면
  			 web.xml에서 필터가 돌아가면서 해당 클래스의 인스턴스화된거를 가지고 있다가 namespace가 /hello인걸 찾고 action name이 hello인걸 찾는다. 									       
 -->  		
 <!-- http://localhost/StrutsToddler/hello/hello.do 를 클라이언트가 요청   중간 hello가 namespace-->
 	<package name ="helloPKG" extends="struts-default" namespace="/hello">
 		<action name="hello" class="kr.or.ddit.hello.controller.HelloController">
 			<result name="success" type="dispatcher">/WEB-INF/views/user/hello/hello.jsp</result>
 		</action>
 	</package>
 	
 	<!-- package를 만들건데 이게 많아지면 코드가 길어지고 유지보수가 힘드니까 외부에 선언해서 레퍼런스한다 -->
 	<include file="kr/or/ddit/struts/config/join.xml"></include>
</struts>

 

3. web.xml에 struts2 쓰려고 선언

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>StrutsToddler</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>
  
  
  <!-- 스트럿츠의 프론트 앤드 컨트롤러 선언  : 스트럿츠 프렝미워크의 default url pattern의 확장자는 *.action
  								  url pattern의 호가장자 변경시 설정파일에서 설정
  								  
  		스트럿츠의 설정 파일(xml) : (빌드 패스|클래스 패스 루트) 하위에 존재
  							   파일명 - struts.xml
  							   외부 설정 파일 존재시 - struts.properties								  
  -->
  <filter>
  	<filter-name>struts2</filter-name>
  	<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
  
  <filter-mapping>
  	<filter-name>struts2</filter-name>
  	<url-pattern>*.do</url-pattern>
  </filter-mapping>
  
  
  <listener>
   	<listener-class>kr.or.ddit.listener.Log4jInitializeListener</listener-class>
   </listener>
   
</web-app>