Tạo trang đăng ký với Struts 1 (Eclipse)
Huy Châu - September 25, 2010 -
Struts 1
công cụ: Eclipse IDE
thư viện: Struts 1x (link here)
server: Tomcat Apache 6
Giới thiệu Struts : (link here)
Cấu trúc file ứng dụng Struts:
Mô tả :
VD trang đăng ký căn bản
B1: chỉnh sửa web.xml
B2: Thư viện Struts
Chép toàn bộ file trong thư mục [struts1.x/lib] vào [WEB-INF/lib]
B3: tạo struts-config.xml
B4: Tạo trang web
Tạo 2 file jsp :
B5: Tạo Action Form
Tạo trang SignupForm (mở rộng từ ActionForm)
B6: Tạo Action
Chạy trang index.jsp ta được như sau :

Sau khi nhập đầy đủ các field trong index.jsp, trang success.jsp xuất hiện

Nếu thiếu 1 trong các field , trang index.jsp sẽ được gọi lại cùng với thông báo lỗi
thư viện: Struts 1x (link here)
server: Tomcat Apache 6
Giới thiệu Struts : (link here)
Cấu trúc file ứng dụng Struts:
- file struts-config.xml (WEB-INF)
- Thư viện Struts (WEB-INF/lib)
- Lớp ActionForm (thuộc tính của file jsp)
- Lớp Action (hành động )
Mô tả :
VD trang đăng ký căn bản
B1: chỉnh sửa web.xml
B2: Thư viện Struts
Chép toàn bộ file trong thư mục [struts1.x/lib] vào [WEB-INF/lib]
B3: tạo struts-config.xml
xml version="1.0" encoding="ISO-8859-1" ?>
DOCTYPE struts-config PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 1.0//EN""http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd">
<struts-config>
<form-beans><form-bean name="signup" type="test.struts.SignupForm">form-bean>
form-beans>
<global-forwards>global-forwards>
<action-mappings><action path="/actions/signup" type="test.struts.SignupAction"name="signup" scope="request"><forward name="success" path="/WEB-INF/result/success.jsp">forward><forward name="missing-value" path="/index.jsp">forward>action>action-mappings>
struts-config>
B4: Tạo trang web
Tạo 2 file jsp :
- (index.jsp) (trang đăng ký) trong WebContent
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sign Uptitle>
head>
<body>
<html:form action="/actions/signup">
<div align="center">
<table width="400" border="0">
<tr>
<td colspan="3">
<div align="center"><strong>Sign Upstrong>div>
td>
tr>
<tr>
<td colspan="3">
<div align="center"><strong> <bean:write name="signup"
property="warning" filter="false" /> strong>div>
td>
tr>
<tr>
<td> td>
<td> td>
<td> td>
tr>
<tr>
<td>First Name :td>
<td><label> <html:text property="firstName">html:text>
label>td>
<td> td>
tr>
<tr>
<td>Last Name:td>
<td><html:text property="lastName">html:text>td>
<td> td>
tr>
<tr>
<td>Email:td>
<td><html:text property="email">html:text>td>
<td> td>
tr>
<tr>
<td>Fax:td>
<td><html:text property="fax">html:text>td>
<td> td>
tr>
<tr>
<td colspan="2"><html:submit value="Sign Me Up!">html:submit>
td>
tr>
table>
div>
html:form>
body>
html>
- (success.jsp) (trang thông báo thành công) trong WEB-INF/resuslt
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sucesstitle>
head>
<body>
<div align="center">
<table width="400" border="0">
<tr>
<td colspan="3"><div align="center"><strong>Sign Upstrong>div>td>
tr>
<tr>
<td> td>
<td> td>
<td> td>
tr>
<tr>
<td>First Name :td>
<td><label>
<bean:write name="signup" property="firstName"/>
label>td>
<td> td>
tr>
<tr>
<td>Last Name:td>
<td>
<bean:write name="signup" property="lastName"/>
td>
<td> td>
tr>
<tr>
<td>Email: td>
<td>
<bean:write name="signup" property="email"/>
td>
<td> td>
tr>
<tr>
<td>Fax: td>
<td>
<bean:write name="signup" property="fax"/>
td>
<td> td>
tr>
<tr>
<td colspan="2">td>
tr>
table>
div>
body>
html>
B5: Tạo Action Form
Tạo trang SignupForm (mở rộng từ ActionForm)
- gồm các thuộc tính của trang đăng ký (vd: firstName, lastName,email,fax)
- phương thức Getter và Setter
- lớp kiểm tra field (isMissing())
B6: Tạo Action
Chạy trang index.jsp ta được như sau :

Sau khi nhập đầy đủ các field trong index.jsp, trang success.jsp xuất hiện

Nếu thiếu 1 trong các field , trang index.jsp sẽ được gọi lại cùng với thông báo lỗi
