Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 9 | 10 | 11 | 12 | 13 | 14 | 15 |
| 16 | 17 | 18 | 19 | 20 | 21 | 22 |
| 23 | 24 | 25 | 26 | 27 | 28 | 29 |
| 30 |
Tags
- 전화걸기
- Android
- Spring
- HTML
- layout
- 예외처리
- JSP
- 에러페이지
- OGNL
- AWT
- 기본
- 어노테이션
- oracle
- 오버로딩
- Menu
- paint
- 생성자
- Eclips
- 안드로이드
- struts2
- 메서드
- 메소드
- 배열
- Graphic
- JavaScript
- 국제화
- Java
- 클래스
- 이클립스
- mybatis
Archives
- Today
- Total
note
Struts2 ActionSupport를 사용하여 유효성 검사(Interceptor) 본문
extends ActionSupport를 해준뒤에
Override 및 Implements 자동완성으로 넣기
struts-ch4.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="ch4-4" namespace="/ch4" extends="struts-default">
<!-- 데이터 전송 -->
<action name="helloWorld3" class="com.ch4.action.HelloWorld3">
<interceptor-ref name="params" />
<result name="success">hello.jsp</result>
</action>
<!-- Action 인터페이스 사용 및 유효성 검사 -->
<action name="helloWorld4" class="com.ch4.action.HelloWorld4">
<interceptor-ref name="params" />
<result name="input">name4.jsp</result>
<result name="success">hello.jsp</result>
</action>
<!-- ActionSupport 사용 및 유효성 검사 -->
<action name="helloWorld5" class="com.ch4.action.HelloWorld5">
<interceptor-ref name="params" />
<interceptor-ref name="workflow" />
<result name="input">name5.jsp</result>
<result name="success">hello.jsp</result>
</action>
</package>
</struts>
HelloWorld5.java
package com.ch4.action;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorld5 extends ActionSupport{
private String name;
private String message;
@Override
public void validate() {
if(name == null || "".equals(name) || "World".equals(name)){
//유효성 검사
//addFieldError는 ActionSupport가 가지고 있는 메소드
addFieldError("name","이름이 없거나 'World'라는 이름은 사용할 수 없습니다.");
}
}
@Override
public String execute() throws Exception {
message = "Hello!, "+name;
return SUCCESS;
}
public String getMessage() {
return message;
}
public void setName(String name) {
this.name = name;
}
}
name5.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>데이터 전송</title>
</head>
<body>
<font color="blue">${fieldErrors.name}</font>
<form action="helloWorld5.action">
<input type="text" name="name" value="${param.name}">
<input type="submit" value="전송">
</form>
</body>
</html>
네이버 사전 발췌
xml의 interceptor
param -> 전송된 데이터를 낚아챔(name부분 홍길동등..)
workflow -> HelloWorld5의 validate()메소드를 동작시킴
param이 먼저 name을 낚아챈뒤 setName을 동작시켜서 전송된 데이터를
workflow가 동작되면서 validate()의 유효성 검사를 시킴
addFieldError가 동작되면 (validate()안에 있는 또다른 메소드)
workflow가 다시 동작되면서 result부분에 접근하게됨
'JSP > Struts2' 카테고리의 다른 글
| struts2 커스텀 인터셉터 (0) | 2012.02.21 |
|---|---|
| struts2 도메인 오브젝트 (0) | 2012.02.21 |
| Struts2 유효성 검사 (0) | 2012.02.21 |
| Struts2 include 사용 (0) | 2012.02.21 |
| Struts2 와일드카드 디폴트 (0) | 2012.02.21 |