0% found this document useful (0 votes)
11 views

Struts中文手册

Uploaded by

foo-hoat Lim
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Struts中文手册

Uploaded by

foo-hoat Lim
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

strtus

struts HTML struts

struts
struts

strus1.1 Nested Tag

jag banq steelg,holen,

:
[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

jakarta-struts-1.0.2 lib webapps lib struts

jdbc2_0-stdext.jar JDBC2.0 Optional Package API struts


WEB-INF lib
Struts.jar struts java WEB-INF lib

*.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

bean bean checkOut()

Action struts

Action

1.2.2

JSP struts

1.2.3

struts ActionServlet servelt


servlet ActionMapping

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

formBean org.apache.struts.action.ActionFormBean ActionForm bean

forward org.apache.struts.action.ActionForward ActionForward

locale true true,

mapping org.apache.struts.action.ActionForward ActionMapping

maxFileSize 250M
multipartClass org.apache.struts.action.ActionForward

noCache False HTTP

Null True true


null
tempDir servlet

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

public void addFormBean(ActionFormBean formBean)


public void removeFormBean(ActionFormBean formBean)
public void addForward(ActionForward actionForward)
public void removeForward(ActionForward actionForward)
public void addMapping(ActionMapping actionMapping)

7
public void removeMapping(ActionMapping actionMapping)

public ActionFormBean findFormBean(String name)


public ActionForward findForward(String name)
public ActionMapping findMapping(String name)

public void addDataSource(String key , DataSource ds)


public DataSource findDataSource(String key)

destroy() ActionServlet
reload() struts

1.5 ActionMapping

Action ActionMapping ActionServelt


ActionMapping Action perform() Action ActionMapping
findForward() ActionForward Action
ActionForward null.
public ActionForward findForward(String name)
ActionForward:
public void addForward(ActionForward forward)
bean:
public String getName()

public String getScope()

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()

public Locale getLocale(HttpServletRequest request)


public void setLocale(HttpServletRequest request,Locale locale)

public MessageResources getResources()


true:
public Boolean isCancelled(HttpServletRequest request)
Action
public void saveErrors(HttpServletRequest request,ActionErrors errors)
ActionError
ActionError struts JSP

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

struts ActionErrors ActionError


org.apache.struts.action ActionErrors ActionError
ActionError
ActionServlet

1.9.1 ActionError

ActionError

ActionError error = new ActionError( error.Invalid );


error
error.invalid=<b>Invalid Number</b>
JSP <html:error> Invalid Number
java.text.MessageFormat
error.invalid=<b>Invalid Number{0}</b>

ActionError error = new ActionError( error.invalid ,new Double(-1));


JSP Invalild Number 1

public String getKey()

public String[] getValues()

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 Struts


JSP JavaBean
struts-bean.tld Bean

bean

bean

2.1.1 Bean

bean bean bean


<bean:define>

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

<bean:cookie id= myCookie name= userName />


myCookie cookie userName
<bean:header id= myHeader name= Accept-Language />
myHeader, Accept-Language.
<bean:parameter id= myParameter name= myParameter >
myPatameter, myParameter.
<bean:include>
ActionForward URL
<jsp:include> bean

Id
Page
forward ActionForward
Href URL

<bean:include id= myInclude page= MyJsp?x=1 />


myInclude MyJsp?x=1

<bean:resource> web InputStream

14
Id
Name
Input

<bean:resource id= myResource name= /WEB-INF/images/myResource.xml />


myResource myResource.xml

2.1.3 Bean

<bean:write> bean JSP


<jsp:getProperty>

Name bean
property
java.beans.PropertyEditor,getAsText() toString
Scope Bean
Filter true, HTML
Ignore false null

<bean:write name= myBean property= myProperty scope= request


filter= true />
myBean myProperty HTML

2.1.4

strtus web

struts <bean:message> java.util


Locale ResourceBundle Java2
Java.text.MessageFormat

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)

ActionServlet true ActionServlet


Action.LOCALE_KEY
web

java.text.MessageFormat

error.invalid.number = The number {0} is valid


{0} <bean:message>

Key
Locale
Action.LOCALE_KEY
Bundle
Action.MESSAGE_KEY
arg0
arg1
arg2
arg3

info.myKey = The numbers entered are {0},{1},{2},{3}

<bean:message key= info.myKey arg0= 5 arg1= 6 arg2= 7 arg3= 8 />


JSP The numbers entered are 5,6,7,8

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:equal parameter= name value= SomeName >


The entered name is SomeName
</logic:equal>
name SomeName
<logic:greaterThan name= bean property= prop scope= page
value= 7 >
The value of bean.Prop is greater than 7
</logic:greaterThan>
bean bean prop
7

<logic:present>
<logic:notPresent>

17
Cookie cookie
Header
parameter
Name property bean
bean bean
property name bean
Scope bean bean

Role
User

<logic:notPresent name= bean property= prop scope= page >


The bean property bean.prop is present
</logic:notPresent>
bean bean bean prop

<logic:match>
<logic:notMatch>
JSP

Cookie HTTP cookie


Header HTTP
parameter HTTP
Name bean bean bean
location ( )
scope bean bean

property bean
value

<logic:match parameter= name value= xyz location= 1 >


The parameter name is a sub-string of the string xyz from index 1
</logic:match>
name xyz
xyz 1 y yz

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

<logic:iterate id= currentInt


collection= <% =myList %>
type= java.lang.Integer
offset= 1
length= 2 >
<% =currentint %>
</logic:iterate>

java.lang.Integer myList 1 2 3 4
1 2

2.2.3

<logic:forward> ActionForward
ActionForward PageContext sendRedirect
name ActionForward

<logic:forward name= myGlobalForward />

<logic:redirect> HTTP
URL

19
Forward ActionForward
Href URL
Page
Name Map
URL property -
Map bean property

Property Map bean Bean name


Scope bean bean

ParamID
ParamName bean (
paramProperty ) bean ( paramProperty
)
paramProperty bean
ParamScope ParamName bean
forward,href page

2.3 HTML

Struts HTML

HTML

2.3.1

struts HTML ActionForm bean


ActionForm bean
ActionForm bean
ActionForm bean
<form> HTML
JavaScript

Onblur
Onchange
Onclick
Ondblclick
Onfocus
Onkeydown

20
onkeypress
Onkeyup
onmousedown
onmousemove
onmouseout
onmouseover
Onmouseup
<form>

Accesskey
Style
styleClass
Tabindex tab

a)

<html:form> HTML AcitonForm bean


ActionMapping
JSP bean ActionMapping
bean bean
<form> HTML
<html:form>

Action
ActionForm bean
Enctype HTTP
Focus
Method HTTP
Name ActionForm bean bean

Onreset JavaScript
Onsubmit JavaScript
Scope ActionForm bean
Style
styleClass
Type ActionForm bean

<html:form action= validateEmploee.do method= post >


</html:form>
validateEmployee, POST
ActionForm bean bean
ActionMapping

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)

<html:reset> <html:submit> HTML

d)

<html:text> <html:textarea> HTML

Property
bean
Name bean
ActionForm
<html:text>

Maxlength

22
Size
<html:textarea>

Rows
Cols

e)

<html:checkbox> <html:multibox> HTML


getParameterValues()

Name Bean
ActionFrom bean
Property bean

Value

<html:checkbox property= married value= Y />


married Y .

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

<html:option collection= optionCollection property= optionValue


labelProperty= optionLabel /
>
optionCollection
optionValue bean bean
optionLabel
<html:option name= optionValues labelName= optionLabels />
optionValues bean
optionLabels 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/>

<html:errors property= missing.name />


missing.name

2.3.3. HTML

struts HTML HTML


<html:html> : HTML
<html:img> :
<html:link> : HTML
<html:rewrite> : URI
struts

2.4.

WEB Struts

2.4.1.

<template:insert> JSP
template JSP <template:put>
<template:insert>

2.4.2.

<template:put> <template:insert>

content JSP HTML


direct true content JSP

Name
Role

26
2.4.3.

JSP <template:get> <template:put> JSP

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

You might also like