下圖是Struts的工作流程
ActionServlet是一個FrontController
ActionMapping是ActionConfig的子類
RequestProcessor根據提交過來的url
ActionFoward是對mapping中一個foward的包裝
ActionForm使用了ViewHelper模式
ActionErrors是對錯誤信息的包裝
Action是用於執行業務邏輯的RequsestHandler
Struts提供了多種Action供選擇使用
我們可以先回到剛才的例子
下面我們看Struts自帶的example實例
說明
代碼中大量應用了struts taglib
但是代碼樹立了一個不好的榜樣
部分代碼如下
登錄
<%@ page con_tentType=
// 聲明Taglib
<%@ taglib uri=
<%@ taglib uri=
<html:html locale=
<head>
// bean是用來從ApplicationResource中讀取i
<title><bean:message key=
<html:base/>
</head>
<body bgcolor=
// 錯誤信息部分
<html:errors/>
// 登錄form
<html:form action=
on_submit=
<table border=
<tr>
<th align=
<bean:message key=
</th>
<td align=
<html:text property=
</td>
</tr>
<tr>
<th align=
<bean:message key=
</th>
<td align=
<html:password property=
</td>
</tr>
<tr>
<td align=
<html:submit value=
</td>
<td align=
<html:reset/>
</td>
</tr>
</table>
</html:form>
// Validator插件
<html:javascript formName=
<script language=
</body>
</html:html>
struts
<form
<!
<form
<form
<form
</form
<!
<form
</form
<action
<!
<action path=
type=
attribute=
scope=
validate=
<forward name=
<forward name=
</action>
subscriptionForm 是一個標准的ActionForm
public final class SubscriptionForm extends ActionForm {
// The maintenance action we are performing (Create or Edit)
private String action =
// Should we auto
private boolean autoConnect = false;
// The host name
private String host = null;
private String password = null;
private String type = null;
private String username = null;
public String getAction() { return (this
public void setAction(String action) { this
public boolean getAutoConnect() { return (this
public void setAutoConnect(boolean autoConnect) { this
public String getHost() { return (this
public void setHost(String host) { this
public String getPassword() { return (this
public void setPassword(String password) { this
public String getType() { return (this
public void setType(String type) { this
public String getUsername() { return (this
public void setUsername(String username) { this
/**
* Reset all properties to their default values
*
* @param mapping The mapping used to select this instance
* @param request The servlet request we are processing
*/
public void reset(ActionMapping mapping
this
this
this
this
this
this
}
/**
* Validate the properties that have been set from this HTTP request
* and return an <code>ActionErrors</code> object that encapsulates any
* validation errors that have been found
* <code>null</code> or an <code>ActionErrors</code> object with no
* recorded error messages
*
* @param mapping The mapping used to select this instance
* @param request The servlet request we are processing
*/
public ActionErrors validate(ActionMapping mapping
HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if ((host == null) || (host
errors
new ActionError(
if ((username == null) || (username
errors
new
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28405.html