스트럿츠2에서는 ActionSupport 클래스를 상속하여 액션 클래스를 만들 수 있습니다.
ActionSupport 클래스는 Action 인터페이스를 구현하기 때문에 execute() 메소드가 물론 제공됩니다.
리턴값으로는 SUCCESS 가 되겠습니다!!
많은 사람과 공유할 수 있도록 로그인 없이 추천 부탁드릴게요^^
양질의 정보로 보답하겠습니다.
일단 코드를 전부 적어놓고 중간중간 설명을 하겠습니다. 간단한 소스이기 떄문에 많은 설명은 필요하지
않을 것 같다는 생각입니다.
파일 작성 순서대로 적어보도록 하겠습니다.
궁금하신 것들은 댓글 남겨주시면 답변 드릴게요^^
프로젝트명 : Web0723
src 패키지 : action
WebContent 아래 jsp 폴더 안에 jsp파일 작성
userRegForm.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<html>
<head><title>[userRegForm.jsp]</title></head>
<body>
<h1>[userRegForm.jsp]</h1>
<form action="UserRegAction.action">
<table border=1>
<tr>
<td bgcolor=skyblue><b>아이디:</b> </td>
<td> <input type=text name="userId" value="${userId}"> </td>
<td align="left"> <font color=red> ${fieldErrors.userId}</font> </td>
</tr>
<tr>
<td bgcolor=skyblue><b>비밀번:</b> </td>
<td> <input type=text name="userPW" value="${userPW}"> </td>
<td align="left"> <font color=red> ${fieldErrors.userPW}</font> </td>
</tr>
<tr>
<td bgcolor=skyblue><b>이 름:</b> </td>
<td> <input type=text name="userName" value="${userName}"> </td>
<td align="left"> <font color=red> ${fieldErrors.userName}</font> </td>
</tr>
<tr>
<td colspan=3 align="center">
<input type="submit" value="보내기">
<input type="reset" value="취소">
</td>
</tr>
</table>
</form>
</body>
</html>
userRegSuccess.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<html>
<head><title>[userRegSuccess.jsp]</title></head>
<body>
<h1>[userRegSuccess.jsp]</h1>
<b><font color=blue size=6>회원가입이 성공</font></b><br>
<h1>
아이디: ${userId} <br>
비밀번: ${userPW} <br>
이 름: ${userName} <br>
</h1>
</body>
</html>
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="Web0723" extends="struts-default">
<!-- validate(), addFieldError( ) -->
<action name="userRegForm">
<result>/jsp/userRegForm.jsp</result>
</action>
<action name="UserRegAction" class="action.UserRegAction">
<interceptor-ref name="params" />
<interceptor-ref name="workflow" />
<result name="input">/jsp/userRegForm.jsp</result>
<result name="success">/jsp/userRegSuccess.jsp</result>
</action>
</package>
</struts>
UserRegAction.java
package action;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import common.DBbean;
import dao.Dao;
public class UserRegAction extends ActionSupport{
private String userId;
private String userPW ;
private String userName ;
public String getUserId() { return userId; }
public void setUserId(String userId) { this.userId = userId; }
public String getUserPW() { return userPW; }
public void setUserPW(String userPW) { this.userPW = userPW; }
public String getUserName() { return userName; }
public void setUserName(String userName) { this.userName = userName; }
@Override
public void validate() {
if(userId==null || "".equals(userId)){ addFieldError("userId","아이디입력") ; }
if(userPW==null || "".equals(userPW)){ addFieldError("userPW","비번입력") ; }
if(userName==null || "".equals(userName)){ addFieldError("userName","이름입력") ; }
} //end
public String execute() throws Exception {
System.out.println("UserRegAction.java execute() 실행완료") ;
return SUCCESS ;
} //end
} //class End
실행화면
url 창에 확장자를 *.action 으로 꼭 바꿔주세요.
데이터 3개를 입력하는 화면입니다
데이터 입력하지 않고 "보내기" 버튼 클릭하면 아래와 같이 메시지가 출력되네요.
출력성공^^
'IT > Struts2' 카테고리의 다른 글
chain result (체인리절트) 활용하기 - 1편 (0) | 2013.07.25 |
---|---|
스트럿츠2 한글 에러 발생 처리 (0) | 2013.07.25 |
struts.xml의 action 설정, 에러발생처리 (0) | 2013.07.24 |
<package/> 요소 사용법 (0) | 2013.07.24 |
스트럿츠2 다운로드 및 기초 웹 어플리케이션 만들기 (2) | 2013.07.23 |