Dev/Struts

Struts2 세팅

창문닦이 2019. 3. 21. 18:27

1. 다이나믹 웹 프로젝트 생성


생성 후

2. log4J, JSTL, OJDBC, iBatis 라이브러리 추가

3. 공통으로 사용하는 클래스 패키지 구성

com.util

com.util.dao

com.util.sqlMap

struts2는 파일을 File클래스가 관리함, filter를 사용하는 방식 또한 별도로 존재하므로 생성하지 않음

formFile 클래스를 Struts2는 지원하지 않으므로 수정해야 함



4. 라이브러리에 struts2 jar파일 추가

제일 핵심파일은 struts2-core.jar

프레임워크는 struts1 > struts2 > spring 순서로 개발되었음


가끔 javaassist 파일이 반영 안된 경우 apps에 struts2-blank.war파일에 있으니 찾아서 반영하면 된다.

5. web.xml 에 filter 작성

모든 URL이 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>struts2</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>

 <!-- Struts2 환경 -->

<filter>

<filter-name>struts2</filter-name>

<filter-class>org.apache.struts.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

</filter>

<filter-mapping>

<filter-name>struts2</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

 <!-- JSP파일이 보이는 것을 제한 -->

<security-constraint>

<web-resource-collection>

<web-resource-name>PreventViewingJSPs</web-resource-name>

<description>웹 사용자가 JSP파일로 직접 접근하지 못함</description>

<url-pattern>*.jsp</url-pattern>

<!-- GET방식, POST방식 둘중하나만 설정가능 -->

<http-method>GET</http-method>

</web-resource-collection>

</security-constraint>

</web-app>

6. WEB-INF에 classes폴더 생성 후 환경설정 파일 생성

6-1. 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>

 <!-- Configuration for the default package. -->

  <package name="default" extends="struts-default" namespace="" >       

      <global-results>

          <result name="error">/exception/error.jsp</result>

      </global-results>

 </package>

</struts>

6-2. struts-temp.xml 생성

struts-default는 library에 core안에 있음

<?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>

<package name="temp" extends="struts-default" namespace="/temp" >       

</package>

</struts>

6-3. struts.properties 환경설정 파일

action : 기본확장자 셋팅

c:\\temp : 임시로 저장해두는 디렉토리를 지정. 실제로 디렉토리를 생성해놔야한다.

struts-default.xml,struts.xml : core에 있는 파일, 내가 만든 환경설정 파일 들을 읽어라


struts.i18n.encoding=UTF-8

struts.action.extension=action

struts.multipart.saveDir=c:\\temp

struts.configuration.files=struts-default.xml,struts.xml

7. web.xml에 환경설정 정보등록

<?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>struts2</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>

 <!-- Struts2 환경 -->

<filter>

<filter-name>struts2</filter-name>

<filter-class>org.apache.struts.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

</filter>

<filter-mapping>

<filter-name>struts2</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

 <!-- JSP파일이 보이는 것을 제한 -->

<security-constraint>

<web-resource-collection>

<web-resource-name>PreventViewingJSPs</web-resource-name>

<description>웹 사용자가 JSP파일로 직접 접근하지 못함</description>

<url-pattern>*.jsp</url-pattern>

<!-- GET방식, POST방식 둘중하나만 설정가능 -->

<http-method>GET</http-method>

</web-resource-collection>

</security-constraint>

</web-app>

셋팅 완료!!