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>
=================================================================================================================================================
write.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 %>/user/write.action" method="post">
이름 : <input type="text" name="user.name"/><br/>
나이 : <input type="text" name="user.age"/><br/>
전화번호 : <input type="text" name="user.tel"/><br/>
<input type="hidden" name="user.mode" value="zzz">
<input type="submit" value="전송하기"/><br/>
</form>
</body>
</html>
UserAction.java
package com.user;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport{
private static final long serialVersionUID = 1L;
private User user;
public User getUser() { // set.Attribute // write_ok.jsp로 User.java에 있는 데이터를 보내줌.
return user;
}
public void setUser(User user) { // get.parameter // write.jsp에서 클라이언트가 입력한 데이터를 가져와 User.java에 셋팅해줌 => ModelDriven방식에서는 필요없음.
this.user = user;
}
public String created() throws Exception{
if(user==null || user.getMode()==null)
return INPUT; // 여기가 실행되면<result name="input">/user1/write.jsp</result> 여기로 간다
// 처음 접속시에는 User.java에 데이터가 없으므로 이곳으로 진입하여 INPUT을 return한다.
user.setName(user.getName()+"님 babo gg");
return SUCCESS; // 여기가 실행되면 <result name="success">/user1/write_ok.jsp</result> 여기로 간다
// 클라이언트가 데이터를 입력하여 넘기면 이곳으로 진입하여 SUCCESS를 return한다.
}
}
// INPUT과 SUCCESS는 ActionSupport가 제공하는 거라 별도의 선언따위는 필요없음.
User.java
package com.user;
public class User {
private String name, tel;
private int age;
private String mode;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getMode() {
return mode;
}
public void setMode(String mode) {
this.mode = mode;
}
}
write_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>
이름 : ${user.name }<br/><!-- 도메인으로 불러와야만 가능하다. -->
나이 : ${user.age }<br/>
전화 : ${user.tel }
</body>
</html>
=================================================================================================================================================
=================================================================================================================================================
결과: localhost:9090/user/write.action을 주소창에 치고 이름과 나이,전화번호를 클라이언트에게 입력받아 submit으로 전송하면
이름 : 홍길동
나이 : 17
전화 : 1234-1234
와 같이 출력된다.
'sturts2' 카테고리의 다른 글
struts2 ModelDriven (0) | 2013.07.21 |
---|---|
struts 2 구조를 이해하는 예제 (0) | 2013.07.21 |