2007년 09월 06일
struts2 개발 메모 2
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="hello" extends="struts-default" namespace="/hello">
<action name="HelloWorld" class="hello.HelloWorld">
<result>/hello/HelloWorld.jsp</result>
</action>
<!-- Add your actions here -->
</package>
</struts>
URL 경로의맨마지막에.action을붙이지않을경우웹서버가찾지못하는거같다.
아래와 같이 struts.xml 파일을 설정하고
http://localhost:8080/Struts2/hello/HelloSupport.action 불렀을때
웹 서버는 기본적으로 HelloSupport.jsp 파일을 찾는거 같다.
이 경우 hellosupport.jsp 파일을 만들어 두어도 찾지 못한다, 반드시 대소문자
구별을 하는거 같다.
<action name="*" class="hello.HelloSupport">
<result>/hello/{1}.jsp</result>
</action>
프레임워크 전반에 관한 설정은
Struts2/WEB-INF/classes/ struts-default.properties 에서 해 줄수도 있고
Struts.xml 파일에서 아래와 같은 식으로도 가능 하다.
<struts>
생략
<constant name=”struts.devMode” value=”true” />
생략
</struts>
링크
<li><a href="<s:url action="Register"/>">Register</a></li>
Parameter가 있는 링크
<s:url id="url" action="Welcome">
<s:param name="request_locale">en</s:param>
</s:url><s:a href="%{url}">English</s:a>
Struts2에서는 기본적으로 ActionSupport 클래스를 상속한다.
ActionSupport 클래스는 Action 인터페이스를 상속 하는데 Action 인터페이스를 보면
public static final String SUCCESS = "success";
public static final String NONE = "none";
public static final String ERROR = "error";
public static final String INPUT = "input";
public static final String LOGIN = "login";
다섯가지return 유형이정해져있다.
어떤액션처리에서jsp 페이지로가지않고다른액션같은곳으로가고싶을때
기본적으로type을선언하지않으면dispatcher 가되는거같다.
type =” redirect-action” 을사용하면redirect 되는거같다.
명시적으로해줘도되는거같다.
Forward가필요한경우에는type=”chain” 을사용해서하는거같다.
<action name="Logon" class="hello.Logon">
<result type="redirect-action">Menu</result>
<result name=”cancel” type="chain">Menu2</result>
<result name="input">/hello/logon.jsp</result>
</action>
Validation check 부분이 이상하다.
위에 있는<action name="Logon" class="hello.Logon">
URL Logon에 대하여 체크를 하고 싶을 경우 hello 디렉토리에 /hello/Logon-validation.xml
파일을 만들어 주면 자동으로 체크를 한다.
이경우 도대체 몇개의 validation.xml 파일을 만들어야 하는지.. ㅠ.ㅠ
Struts1에서 처럼 struts.xml 파일에서 특정 장소의 validation.xml을 include
예를들어
hello-validation.xml 파일 하나로 모든 hello 디렉토리 밑의 validation check가
가능하게 하는 방법은 없는지 찾아 봐라. 없다면.. 좌절이다…. ㅠ.ㅠ
Logon 페이지에 왔을때 첫 페이지 인데 validation check를 해 버린다.
이 경우 링크를 걸어 주는 곳에서
<li><a href="<s:url action="Logon"/>">Sign On</a></li>
<li><a href="<s:url action="Logon_input"/>">Sign On</a></li>
설정 파일에서도
<action name="Logon" class="hello.Logon">
<action name="Logon_*" method="{1}" class="hello.Logon">
이렇게 바꿔 줌으로서 처음 전이에는 에러 메세지가 보이지 않고
Logon Action Class에서 input을 리턴 했을 경우만 보여 주게 된다.
현재 validation check 에러 메시지가 폼태크 바로 위에 출력된다.
에러 메시지 출력 위치를 제어 할수 없을까?
Validation 의 경우 Message bundle 파일을 이용할수 있다.
근데.. 이것도 좀 이상하다.
위의 내용이랑 계속 연결해서 Message bundle 의 경우 역시 프로젝트 전체는 안되고
특정 디렉토리 예를 들자면 /hello/package.properties 파일을 만들어야 한다.
이럴 경우 디렉토리가 많으면.. 전부 만들어야 한다는 건데.. 뭔가 설정이 있을 듯 하다.
설정 방법은 jsp 페이지
<s:textfield label="User Name" name="username"/>
-> <s:textfield label="%{getText('username')}" name="username"/>
Login-validation.xml 파일에서
<message>Username is required</message>
-> <message key="requiredstring"/>
이런 식으로 변경해 주고
Package.properties 파일은 다음과 같이 기술한다.
# by | 2007/09/06 14:02 | struts2 | 트랙백 | 덧글(0)





☞ 내 이글루에 이 글과 관련된 글 쓰기 (트랙백 보내기) [도움말]