Struts中文手册
Struts中文手册
struts
struts
:
[email protected] QQ:36983608
2002 08 26
2002-9-12
mailto:[email protected]
1
1. Struts ................................................................................................................................1
1.1. Struts .....................................................................................................1
1.2. Struts ........................................................................................................1
1.2.1. ...................................................................................................................2
1.2.2. ...................................................................................................................2
1.2.3. ...............................................................................................................2
1.3. Struts .................................................................................................3
1.3.1. Struts .................................................................................................3
1.4. ActionServlet ........................................................................................................6
1.4.1. ActionServlet ............................................................................................6
1.4.2. ActionServlet ............................................................................................7
1.5. ActionMapping ....................................................................................................8
1.6. Action ...................................................................................................................8
1.6.1. Action ...............................................................................................9
1.7. ActionForm ..........................................................................................................9
1.8. ActionForward ...................................................................................................10
1.9. .................................................................................................................11
1.9.1. ActionError ................................................................................................11
1.9.2. ActionError ................................................................................................11
2. Struts ..........................................................................................................................13
2.1. Bean ...............................................................................................................13
2.1.1. Bean ................................................................................................13
2.1.2. .....................................................................................14
2.1.3. Bean ...............................................................................................15
2.1.4. .........................................................................................15
2.2. .................................................................................................................16
2.2.1. .........................................................................................................17
2.2.2. .........................................................................................................18
2.2.3. .........................................................................................19
2.3. HTML ............................................................................................................20
2.3.1. .............................................................................20
a) .........................................................................................................21
b) .............................................................................................22
c) .............................................................................................22
d) .........................................................................................22
e) .....................................................................................23
f) .........................................................................................................23
g) .....................................................................................................23
h) .........................................................................................................24
i) .........................................................................................................24
j) .........................................................................................................24
k) ( <html:select> ) ..............................24
2.3.2. .....................................................................................25
1
2.3.3. HTML ...........................................................................................26
2.4. .................................................................................................................26
2.4.1. .........................................................................................................26
2.4.2. .........................................................................................................26
2.4.3. .........................................................................................................27
2.4.4. .................................................................................................27
2
1. Struts
struts MVC web
struts struts
struts
1.
2.
3. HTML
1.1 Struts
*.tld struts
WEB-INF lib
webapps
Web
Struts-blank.war web
Struts-documentation.war struts struts
Struts-example.war Struts
Struts-exercisetaglib.war
struts
Struts-template.war struts
Struts-upload.war struts
1.2 Struts
MVC struts
1
1.2.1
struts
ActinForm JavaBean
Bean
Action struts
Action
1.2.2
JSP struts
1.2.3
2
1.3 Struts
( ROSE ActionErrors
ActionMapping JSP Tag Library )
ActionServlet
ActionClass
ActionForm
ActionMapping
ActionForward
ActionError
Struts
1.3.1 Struts
struts struts-config.xml
WEB-INF struts-config.xml
ActionMapping
ActionForm bean
JDBC
JSP
actionMappingInstace.findForward( logicalName );
(
)
<global-forwards>
3
<forward name="bookCreated" path="/BookView.jsp"/>
</global-forwards>
Name
Path URL
ActionMapping
ActionMapping URI Action
, Action ActionForm bean ActionServlet
Action Action perform()
ActionForward
<action-mappings>
<action path="/createBook"
type="BookAction"
name="bookForm"
scope="request"
input="/CreateBook.jsp">
</action>
<forward name= failure path= /CreateBook.jsp />
<forward name= cancel path= /index.jsp />
</action-mappings>
Path Action
Name Action bean
Type Action
Scope ActionForm bean
Prefix bean
Suffix bean
attribute
className ActionMapping
org.apache.struts.action.ActionMapping
input bean
unknown true ActionMapping
URI
validate true Action perform()
ActionServlet ActionForm bean validate()
<forward> Action
Id ID
ClassName ActionForward
org.apache.struts.action.ActionForward
Name ActionForward
Path
4
redirect true ActionServlet
sendRedirect()
ActionForm Bean
ActionServlet ActionForm bean HTTP
ActionForm bean
Action
<form-beans>
<form-bean name="bookForm" type="BookForm"/>
</form-beans>
Id ID
className ActionForm bean
org.apache.struts.action.ActionFormBean
Name bean
bean ActionMapping
Type
JDBC
<data-sources>
Id ID
Key Action
Type JDBC
<set-property> 1.1
<data-source>
<data-sources>
<data-source id= DS1
key= conPool
type= org.apache.struts.util.GenericDataSource
<set-property id= SP1
autoCommit="true"
description="Example Data Source Configuration"
driverClass="org.test.mm.mysql.Driver"
maxCount="4"
minCount="2"
url="jdbc:mysql://localhost/test"
user="struts"
password="wrox" />
<data-source/>
</data-sources>
desciption
autoCommit
5
driverClass JDBC
loginTimeout
maxCount
minCount
password
readOnly
User
url JDBC URL
Action
javax.sql.DataSource ds = servlet.findDataSource( conPool );
javax.sql.Connection con = ds.getConnection();
1.4 ActionServlet
org.apache.struts.action.ActionServlet
javax.servlet.http.HttpServlet
Struts controller
1. Http
2. Action
3. ActionForm bean
bean.
4. Action perform ActioForm bean,Action Mapping ,request
response
public ActionForword perform(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
5.perform ActionForword jsp .
1.4.1 ActionServlet
web.xml ActionServlet
application null
bufferSize 4096
6
config /WEB-INF/struts-config.xml
content Text/html
debug 0
detail 0
factory null
maxFileSize 250M
multipartClass org.apache.struts.action.ActionForward
validate True
vallidating True
servlet
URI ActionServlet Action
ActionServlet Action servlet
Action
ActionServlet Action
1.4.2 ActionServlet
ActinServlet Action
Struts API struts-documentation.war
7
public void removeMapping(ActionMapping actionMapping)
destroy() ActionServlet
reload() struts
1.5 ActionMapping
1.6 Action
Action
ActionServlet
Action
Action
Action perform()
ActionServlet Action (
) ActionServlet ActionMapping Action Action
ActionMapping ActionMapping servlet
perform() ActionServlet ServletRequest
HttpServletRequest
Action org.apache.struts.action.Action
perform() perform()
HTTP
8
public ActionForward perform(ActionMapping action,
AcionForm form,
ServletRequest request,
ServletResponse response)
throws IOException,ServletException
HTTP
public ActionForward perform(ActionMapping action,
AcionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException,ServletException
Action
Action
JavaBean
1.6.1 Action
perform()
1.7 ActionForm
ActionForm bean
struts-config.xml bean Action perform()
bean
bean bean
bean Action
9
Action perform() ActionForm bean
ActionForm org.apache.struts.action.ActionForm
bean ActionServlet
ActionForm
Public ActionErrors validate(ActionMappin mapping,
ServletRequest request)
Public ActionErrors validate(ActionMappin mapping,
HttpServletRequest request)
bean validate() <action>
validate true ActionServlet Action validate()
ActionErrors null ActinForm ActionErrors
null 0
ActionServlet <action> input
Action
ActionForm
reset() bean
public void reset(ActionMapping mapping,HttpServletRequest request)
public void reset(ActionMapping mapping,ServletRequest request)
ActionFrom bean getXXX ,
Action bean
1.8 ActionForward
Action
Action ActionForward ActionServlet
findForward():
ActionServlet
ActionMappin perform()
public ActionForward()
public ActionForward(String path)
public ActionForward(String path,Boolean redirect)
10
1.9
1.9.1 ActionError
ActionError
1.9.2 ActionError
ActionError ActionErrors
ActionErrors ActionError
ActionErrors.GLOBAL_ERROR.
Action perform()
MyForm form = (MyForm) form;
if (number == -1) {
ActionErrors errors = new ActionErrors();
ActionError error = new ActionError( error.Invalid ,new Double(-1));
errors.add(ActionErrors.GLOBAL_ERROR,error);
saveErrors(req,errors);
String input = mapping.getInput();
Return new ActionForward(input);
}
ActionErrors
11
clear()
empty() ActionErrors true
get() Iterator
properties() Iterator
size()
12
2 Struts
Struts
JSP struts
2. Bean JSP bean
3. JSP
4. HTML HTML ID URL
5.
2.1 Bean
bean
bean
2.1.1 Bean
bean bean
bean bean
<bean:define>
Id bean
Type
Value id
Name bean value
property Name bean bean
13
Scope bean
toScope bean
bean:
<bean:define id= test value= this is a test />
bean bean:
<bean:define id= targetBean name= sourceBean
scope= page toScope= request />
2.1.2
cookie, HTTP
Id
Name cookie/ /
multiple cookie
Cookie[]( ) bean cookie
Cookie
Value cookie
Id
Page
forward ActionForward
Href URL
14
Id
Name
Input
2.1.3 Bean
Name bean
property
java.beans.PropertyEditor,getAsText() toString
Scope Bean
Filter true, HTML
Ignore false null
2.1.4
strtus web
strtus
-
error.validation.location = The entered location is invalid
ActionServlet Java
WEB-INF classes
15
ApplicationResources.properties ApplicationResources
WEB-INF classes com test com.test.
ApplicationResources.
-
ApplicationResources.properties
ApplicationResources_xx.properties(xx ISO en)
java.text.MessageFormat
Key
Locale
Action.LOCALE_KEY
Bundle
Action.MESSAGE_KEY
arg0
arg1
arg2
arg3
2.2
scriptlet Struts
JSP
struts-logic.tld
16
2.2.1
struts
cookie
bean bean
<equal> true
<notEqual> true
<greaterEqual> true
<lessEqual> true
<lessThan> true
<greaterThan> true
Value
Cookie HTTP cookie
Header HTTP
parameter HTTP
Name bean bean bean
property bean
Scope Bean
<logic:present>
<logic:notPresent>
17
Cookie cookie
Header
parameter
Name property bean
bean bean
property name bean
Scope bean bean
Role
User
<logic:match>
<logic:notMatch>
JSP
property bean
value
2.2.2
<logic:iterate>
java.util.Iterator,java.util.Collection
18
,java.util.Map
bean name
name bean property bean
bean
collection name
Id bean
indexed JSP bean
Length
Name bean bean property
Offset
property Bean
Scope bean bean
Type bean
java.lang.Integer myList 1 2 3 4
1 2
2.2.3
<logic:forward> ActionForward
ActionForward PageContext sendRedirect
name ActionForward
<logic:redirect> HTTP
URL
19
Forward ActionForward
Href URL
Page
Name Map
URL property -
Map bean property
ParamID
ParamName bean (
paramProperty ) bean ( paramProperty
)
paramProperty bean
ParamScope ParamName bean
forward,href page
2.3 HTML
Struts HTML
HTML
2.3.1
Onblur
Onchange
Onclick
Ondblclick
Onfocus
Onkeydown
20
onkeypress
Onkeyup
onmousedown
onmousemove
onmouseout
onmouseover
Onmouseup
<form>
Accesskey
Style
styleClass
Tabindex tab
a)
Action
ActionForm bean
Enctype HTTP
Focus
Method HTTP
Name ActionForm bean bean
Onreset JavaScript
Onsubmit JavaScript
Scope ActionForm bean
Style
styleClass
Type ActionForm bean
21
<form-beans>
<form-bean name= empForm type= com.example.EmployeeForm />
</form-beans>
<action-mappings>
<action path= /validateEmployee
type= com.example.ValidateExampleAction
name= empForm
scope= request
input= /employeeInput.jsp >
<forward name= success path= /employeeOutput.jsp >
</action>
</action-mapping>
URI *.do ActionServlet
ActionForm bean
empForm,com.example.EmployeeForm request. <html:form>
<html:form>
b)
<html:button> <html:cancel>
Property
Value
c)
d)
Property
bean
Name bean
ActionForm
<html:text>
Maxlength
22
Size
<html:textarea>
Rows
Cols
e)
Name Bean
ActionFrom bean
Property bean
Value
f)
<html:file> HTML
Name Bean
ActionForm bean
property
bean
Accept
Value
g)
<html:radio> HTML
Name Bean
ActionFrom bean
property
bean
23
Value
h)
<html:hidden> HTML
Name Bean
ActionFrom bean
property
bean
Value
i)
<html:password> HTML
maxlength
Name Bean
ActionFrom bean
property
bean
redisplay bean
Size
j)
<html:select> HTML
multiple
Name Bean
ActionFrom bean
property
bean
Size
Value
k) ( <html:select> )
<html:option> HTML
24
collection Bean
Property bean
labelProperty bean
labelName bean bean
<html:option> ( )
labelProperty collection bean
bean <html:option>
value
Name bean
bean <html:option> value
property collection
bean name collection
name bean ( name )
ActionForm bean bean
2.3.2.
<html:errors> ActionErrors
errors.header
ActionErrors ( Action.ERROR_KEY )
ActionError
errors.footer
property ActionErrors
ActionError
Bundle
Acion.MESSAGE_KEY
Locale
Action.ERROR_KEY
25
Name ActionErrors
Action.ERROR_KEY
property ActionErrors ActionError
<html:errors/>
2.3.3. HTML
2.4.
WEB Struts
2.4.1.
<template:insert> JSP
template JSP <template:put>
<template:insert>
2.4.2.
<template:put> <template:insert>
Name
Role
26
2.4.3.
Name <template:put>
Role
2.4.4.
JSP web
<html>
<%@ taglib uri= /template prefix= template %>
<head>
<title></title>
</head>
<body>
<table width= 100% height= 100% >
<tr height= 10% >
<td>
<template:get name= header />
</td>
</tr>
<tr height= 80% >
<td>
<template:get name= content />
</td>
</tr>
<tr height= 10% >
<td>
<template:get name= footer />
</td>
</tr>
</table>
</body>
</html>
template.jsp <template:get>
JSP <template:put> HTML
JSP
<%@ taglib uri= /template prefix= /template %>
<template:insert template= template.jsp >
<template:put name= header content= header.html />
27
<template:put name= content content= employeeList.jsp />
<template:put name= footer content= footer.html />
</template:insert>
JSP <template:insert
<template:put> JSP
template.jsp
28