web.xml(<url-pattern>/*</url-pattern> 때문에 클라이언트의 모든 요청은 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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>struts</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>
<!-- 스트러츠 환경 정의 시작 -->
<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>
<!-- 스트러츠 환경 정의 끝 -->
</web-app>
=================================================================================================================================================
struts.xml(여럿이 작업할 때 유지보수를 효율적으로 하기 위해서 이곳이 아닌 다른곳에서 struts.xml 작업을 하고 이곳에서는 include로 내용을 가져오기만 한다. 이번 예제 에서는 struts-test.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>
<include file="struts-test.xml"/>
</struts>
=================================================================================================================================================
tset.jsp
<%@ page contentType="text/html; charset=UTF-8"%>
<%@page trimDirectiveWhitespaces="true"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%String cp = request.getContextPath();%>
<!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>
<form action="<%=cp %>/test/test_ok.action" method="post">
이름 : <input type="text" name="name"><br/> <!-- TestAction.java에서 만든 getter setter 프로퍼티와 name이 똑같아야한다. -->
나이 : <input type="text" name="age"><br/>
<input type="submit" value="전송하기"><br/>
</form>
</body>
</html>
TestAction.java
package com.test;
import com.opensymphony.xwork2.ActionSupport;
/*
* Action 클래스
* 클라이언트 요청을 전달 받아 비지니스 로직을 처리하는 클래스로 서블릿과 유사한 기능을 수행
* Action 클래스는 POJO(순수한 자바형태) 형태로 작성해도 되며 일반적으로 ActionSupport 클래스를 상속받는다.
*/
public class TestAction extends ActionSupport{
private static final long serialVersionUID = 1L;
private String name;
private int age;
private String message;
// getter는 request.setAttribute("name",name); 역할 // 다른 jsp나 servlet으로 이곳의 프로퍼티 데이타를 넘겨줄 때 사용.
// 결과적으로 test_ok.jsp로 데이타를 넘겨준다.
public String getName() {
return name;
}
// setter은 String name = request.getParameter("name"); 역할 // 이곳의 클래스에서 해당 클라이언트가 넘겨준 데이터를 사용할 때.
// 결과적으로 test.jsp에서 클라이언트가 입력한 데이터를 가져오게 된다.
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getMessage() {// message의 setter가 없는 이유는 클라이언트가 넘겨준 값을 받아서 처리하는것이 아닌 이곳의 excute()메소드가 처리한 결과를 넘겨주기만 하는 역할을 하기 때문이다.
return message;
}
public String execute() throws Exception{
// 메소드 이름을 struts-test.xml 파일의 <action name="test_ok" class="com.test.TestAction"> 부분에서 <action name="test_ok" class="com.test.TestAction" method="test"> 처럼 메소드를 명시하지 않으면 기본적으로 execute() 메소드를 찾아서 실행하게됨. <action name="test_ok" class="com.test.TestAction" method="test"> 일 경우 test() 메소드를 찾아 실행한다.
if(age>=18)
message="성인입니다";
else
message="미성년자입니다";
return "ok"; // struts-test.xml의 <result name="ok">/test/test_ok.jsp</result> 여기서 받는다.
}
}
test_ok.jsp
<%@ page contentType="text/html; charset=UTF-8"%>
<%@page trimDirectiveWhitespaces="true"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%String cp = request.getContextPath();%>
<!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>
이름 : ${name}<br/>
나이 : ${age}<br/>
${message}
</body>
</html>
=================================================================================================================================================
=================================================================================================================================================
결과: localhost:9090/test/test.action을 주소창에 치면 이름과 나이를 클라이언트에게 입력받아 submit으로 전송하면
이름: 홍길동
나이: 17
미성년자입니다.
와 같이 출력된다.
'sturts2' 카테고리의 다른 글
struts2 ModelDriven (0) | 2013.07.21 |
---|---|
struts2 도메인을 이용한 사용법 (0) | 2013.07.21 |