유효성 체크x

infestationwatchSoftware and s/w Development

Oct 28, 2013 (3 years and 7 months ago)

111 views

유효성

체크

방법

1.

validate
메소드를

사용하는

방법

-

액션

클래스가

ActionSupport


상속받으면

validate
라는

메소드를

사용할



있게된다
.

방법

2.

struts
-
tags


사용해야만

한다
.


액션클래스

+

-
validation.xml




이름을

갖는

xml
설정

파일을

이용하는

방법


방법

1
예제

<test.jsp>

<%@

page

language
=
"java"

contentType
=
"text/html; charset
=UTF
-
8"


pageEncoding
=
"UTF
-
8"
%>

<%@

taglib

prefix
=
"c"

uri
=
"http://java.sun.com/jsp/jstl/core"

%>

<!
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

method
=
"post"

action
=
"testAction.action"
>


이름

<
input

type
=
"text"

name
=
"name"
/>
${fieldErrors.errName }
<
br
/>


나이

<
input

type
=
"text"

name
=
"age"

value
=
"0"
/>
${fieldErrors.errAge

}
<
br
/>


주소

<
input

type
=
"text"

name
=
"addr"
/>
${fieldErrors.errAddr }
<
br
/>


<
input

type
=
"submit"

value
=
"
전송
"
/>

</
form
>






</
body
>

</
html
>

<TestAction.java>

package

hb.action;


import

com.opensymphony.xwork2.ActionSupport;


public

class

TestAction
extends

ActionSupport {


private

String
name
;


private

int

age
;


private

String
addr
;


@
Override


public

void

validate() {



//
에러가

하나라도

발생하면

액션을

수행하지

않고

result




//INPUT


수행한다
..



if
(
name
.equals(
""
)){




addFieldError(
"errName"
,
"
이름을

입력하세요
!"
);



}



if

(
age
<
=0 ||
age
>120){




addFieldError(
"errAge"
,
"
나이를

정확히

입력하세요
!"
);






}



if

(
addr
.equals(
""
)){




addFieldError(
"errAddr"
,
"
주소

입력하세요
!"
);






}




}




public

void

setName(String name) {



this
.
name

= name;


}


public

void

setAge(
int

age) {



this
.
age

= age;


}


public

void

setAddr(String addr) {



this
.
addr

= addr;


}



public

String getName() {



return

name
;


}



public

int

getAge() {



return

age
;


}



public

String getAddr() {



return

addr
;


}




@
Override


public

String execute()
throws

Exception {






return

SUCCESS
;


}






}


<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
=
"default"

extends
=
"struts
-
default"
>



<
action

name
=
"testAction"

class
=
"hb.action.TestAction"
>




<
result

name
=
"success"
>
/result.jsp
</
result
>




<
result

name
=
"input"
>
/test.jsp
</
result
>



</
action
>


</
package
>



</
struts
>


<result.jsp>

<%@

page

language
=
"java"

contentType
=
"text/html; charset=UTF
-
8"


pageEncoding
=
"UTF
-
8"
%>

<%@

taglib

prefix
=
"c"

uri
=
"http://java.sun.com/jsp/jstl/core"

%>

<!
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
>

사용자가

보내온

데이터
<
br
/>

이름

: ${name }
<
br
/>

나이

: ${age }
<
br
/>

주소

: ${addr }
<
br
/>


</
body
>

</
html
>


방법

2
예제

아파치에서

제공하는

sample
코드를

참고해서

만들어

보자

sample
코드의

위치

C:
\
java
\
FrameWork
\
Struts2
\
자료실
\
struts
-
2.1.8.1
-
all
\
struts
-
2.1.8.1
\
apps
\
struts2
-
blank
-
2.1.8.1
\
WEB
-
INF
\
src
\
java
\
example


위의

Login
-
validation.xml
파일을

작업중인

프로젝트의

액션

클래스가

있는

패키지의

같은

경로에

붙여

넣는다
.
파일의

이름은


액션클래스이름

+

-
validation.xml



되어야

하므로

리네임해서

넣는

.




<test1.jsp>

<%@

page

language
=
"java"

contentType
=
"text/html; charset=UTF
-
8"


pageEncoding
=
"UTF
-
8"
%>

<%@

taglib

prefix
=
"c"

uri
=
"http://java.sun.com/jsp/jstl/core"

%>

<%@

taglib

prefix
=
"s"

uri
=
"/struts
-
tags"

%>

<!
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
>

<!
--

스트럿츠

태그를

이용해야함

유효성

검사

메시지가

출력됨

--
>

<
s:form

method
=
"post"

action
=
"actTest"
>


<
s:textfield

label
=
"
이름
"

name
=
"name"
/><
br
/>


<
s:textfield

label
=
"
나이
"

name
=
"age"

value
=
"0"
/><
br
/>


<
s:textfield

label
=
"
전화번호
"

name
=
"tel"
/><
br
/>


<
s:textfield

label
=
"
이메일
"

name
=
"email"
/
><
br
/>


<
s:submit

label
=
"send"
/><
br
/>


</
s:form
>


</
body
>

</
html
>

<ActTest.java>

package

hb.action;


import

com.opensymphony.xwork2.ActionSupport;


public

class

ActTest
extends

ActionSupport{


private

String
name
;


private

int

age
;


private

String
tel
;


private

String
addr
;


private

String
email
;


public

String getName() {



return

name
;


}


public

void

setName(String name) {



this
.
name

= name;


}


public

int

getAge() {



return

age
;


}


public

void

setAge(
int

age) {



this
.
age

= age;


}


public

String
getTel() {



return

tel
;


}


public

void

setTel(String tel) {



this
.
tel

= tel;


}


public

String getAddr() {



return

addr
;


}


public

void

setAddr(String addr) {



this
.
addr

= addr;


}


public

String getEmail() {



return

email
;


}


public

void

setEmail
(String email) {



this
.
email

= email;


}





@Override


public

String execute()
throws

Exception {






return

SUCCESS
;


}

}


<ActTest
-
validation.xml>

<!
DOCTYPE

validators

PUBLIC


"
-
//OpenSymphony Group//XWork Validator 1.0.2//EN"


"http://www.opensymphony.com/xwork/xwork
-
validator
-
1.0.2.dtd"
>


<
validators
>


<
field

name
=
"name"
>



<
field
-
validator

type
=
"requiredstring"
>




<
message
>
이름을

입력하세요
</
message
>



</
field
-
validator
>


</
field
>


<
field

name
=
"age"
>



<
field
-
validator

type
=
"int"
>




<
param

name
=
"min"
>
1
</
param
>




<
param

name
=
"max"
>
120
</
param
>




<
message
>
나이를

정확히

입력하세요
</
message
>



</
field
-
validator
>


</
field
>


<
field

name
=
"email"
>



<
field
-
validator

type
=
"requiredstring"
>







<
message
>
이메일을

입력하세요
</
message
>



</
field
-
validator
>



<
field
-
validator

type
=
"email"
>




<
message
>
이메일

형식으로

입력하세요
</
message
>



</
field
-
validator
>


</
field
>


<
field

name
=
"tel"
>



<
field
-
validator

type
=
"stringlength"
>




<
param

name
=
"maxLength"
>
12
</
param
>







<
message
>
12


이상은

입력이

불가합니다
.
</
message
>



</
field
-
validator
>




</
field
>



</
validators
>



<result1.jsp>

<%@

page

language
=
"java"

contentType
=
"text/html; charset=UTF
-
8"


pageEncoding
=
"UTF
-
8"
%>

<%@

taglib

prefix
=
"c"

uri
=
"http://java.sun.com/jsp/jstl/core"

%>

<!
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
>

사용자로부터

넘어온

데이터
<
br
/>

이름

: ${name }
<
br
/>

나이

: ${age }
<
br
/>

전화

:

${tel }
<
br
/>

이멜

: ${email }
<
br
/>


</
body
>

</
html
>