본문 바로가기
카테고리 없음

Struts 간단 정리

by jjerryhan 2008. 5. 14.
반응형
[Action Mapping - struts config]
<!-- 단순 jsp Forward 액션 -->
<action
path="/Welcome"
forward="/Welcome.jsp"/>
<!-- 로그인 폼으로 이동하는 Forward 액션 -->
<action
path="/login1/logInForm"
forward="/login1/logInForm.jsp"/>
<!-- 로그인을 실행하는 Action -->
<action
path="/login1/logIn"
type="strutsguide.actions.Login1Action"
validate="false"
>
<!-- 로그인을 수행한 뒤에 성공/실패 표시를 위해 이동할 View 페이지 -->
<forward name="success" path="/login1/logInSuccess.jsp" redirect="true" />
<forward name="fail" path="/login1/logInFail.jsp" />
</action>

[Use Bean - jsp]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%-- Action에서 Session 객체에 저장한 userInfo 자바 빈 객체를 사용한다. --%>
<jsp:useBean class="strutsguide.beans.UserInfoBean" id="userInfo" scope="session"/>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=euc-kr">
<title>로그인 성공</title>
</head>
<body>
<h1>로그인 성공</h1>
로그인 사용자명 : <jsp:getProperty name="userInfo" property="userName"/><br />
전화번호 : <jsp:getProperty name="userInfo" property="phone"/><br />
이메일 : <jsp:getProperty name="userInfo" property="email"/><br />
</body>
</html>

[Form Bean - struts config]
<form-beans>
<form-bean
name="폼의이름"
type="myproject.form.FormClass"/>
</form-beans>

[Action Message - jsp]
<html:messages id="msg" message="true">
<%--
ActionMessages.GLOBAL_MESSAGE 키로 저장된 ActionMessage 객체가 없다면
이 부분은 실행되지 않는다.
--%>
<b><bean:write name="msg"/></b> <br />
</html:messages>
Username : <html:text property="username"/>
<html:messages id="msg" property="invalidUsernameError">
<%--
invalidUsernameError 라는 키로 저장된 ActionMessage 객체가 없다면
이 부분은 실행되지 않는다.
--%>
<b><bean:write name="msg"/></b>
</html:messages>

[Action Message - model]
ActionMessages messages = new ActionMessages();
// 글로벌 메시지를 추가한다.
// 이것은 <html:messages id="msg" message="true">에 의해 JSP에서 출력된다.
messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
"error.invalidLogin"));
// request 객체에 메시지 추가
saveMessages(request, messages);

[Action Errors + Validator - model]
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
// 사용자명을 입력하지 않았거나 공백을 포함하고 있을 경우
if (username == null || username.length() == 0) {
errors.add("invalidUsernameError",
new ActionMessage("error.invalidUsername", "사용자명을 입력해주세요."));
} else if (username.indexOf(" ") >= 0 || username.indexOf("\t") >= 0 ||
username.indexOf("\n") >= 0) {
errors.add("invalidUsernameError",
new ActionMessage("error.invalidUsername", "사용자명은 공백을 포함할 수 없습니다."));
}
// 비밀번호를 입력하지 않았을 경우
if (password == null || password.length() == 0) {
errors.add("invalidPasswordError",
new ActionMessage("error.invalidPassword"));
}
/*
* ActionErrors errors 객체가 null이거나 아무런 ActionMessage 객체도 포함하고 있지 않으면
* 오류가 발생하지 않았다고 가정한다.
*/
return errors;
}