Latest Entry
Struts2 HelloWorld 프로젝트 세팅하기
2 20, 2012 java Leave a comment
Struts2를 시작하면서 가장 어려운것은 설정이 아닐까 한다.
자바에 정통하거나 많은 프로그램이 없다면 당연히 어디 위치에 어떻게 만들어야 하는지 최초 설정이 대단히 어렵게 느껴진다.
설정을 하고 정상적으로 작동하는 모습을 보면 다음부터는 좀더 빠른 스피드로 정리를 해나갈수 있으리라고 생각한다.
아래 내용은 Struts2의 HelloWorld를 출력하는 가장 기본적인 프로그램을 실행하는 방법이다.
아래 내용으로 부터 점차적으로 발전시켜 나아가길 바란다.
1. web.xml 세팅
/WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Struts Blank</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>
2. 액션클래스 생성
/src/HelloWord/HelloWorldAction.java
package HelloWorld;
public class HelloWorldAction {
private String hello;
public String execute() throws Exception{
hello = "Hello World!!!!!";
return "success";
}
public String getHello() {
return hello;
}
public void setHello(String hello) {
this.hello = hello;
}
}
3. 리절트페이지 생성
/hello.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <s:property value="hello" /> </body> </html>
4. struts.xml액션과 리절트 정의
/src/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>
<package name="hello" extends="struts-default" namespace="">
<action name="helloAction" class="HelloWorld.HelloWorldAction">
<result>/hello.jsp</result>
</action>
</package>
</struts>
출력결과
※ 액션과 사용자 요청과의 관계
- 액션은 하나의 작업단위.
- 액션은 하나의 URL
- 액션은 하나의 클래스 또는 메서드
- 액션은 한 종류의 비즈니스 로직을 수행하기 위한 통로
- 액션은 POHO클래스
- 액션은 struts.xml에서 정의
- 액션 메서드는 스트링 리절트 코드를 반환한다.

