這個例子講解的是Struts2的Hello 例子。html
本文的整合的環境是:java
1.Maven 3web
2.Eclipse 3.7apache
3.Struts 2.3.1.2app
本文的最終結構圖以下所示,防止你跟不上後面的步驟eclipse
使用Maven須要添加Struts2依賴,在pom.xml文件裏須要添加「struts2-core」jsp
pom.xml文件內容以下所示:maven
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
<
modelVersion
>4.0.0</
modelVersion
>
<
groupId
>com.mkyong.common</
groupId
>
<
artifactId
>Struts2Example</
artifactId
>
<
packaging
>war</
packaging
>
<
version
>com.mkyong.common</
version
>
<
name
>Struts2Example Maven Webapp</
name
>
<
dependencies
>
<
dependency
>
<
groupId
>junit</
groupId
>
<
artifactId
>junit</
artifactId
>
<
version
>3.8.1</
version
>
<
scope
>test</
scope
>
</
dependency
>
<
dependency
>
<
groupId
>org.apache.struts</
groupId
>
<
artifactId
>struts2-core</
artifactId
>
<
version
>2.3.1.2</
version
>
</
dependency
>
</
dependencies
>
<
build
>
<
finalName
>Struts2Example</
finalName
>
<
plugins
>
<
plugin
>
<
artifactId
>maven-compiler-plugin</
artifactId
>
<
version
>2.3.2</
version
>
<
configuration
>
<
source
>1.6</
source
>
<
target
>1.6</
target
>
</
configuration
>
</
plugin
>
</
plugins
>
</
build
>
</
project
>
|
編譯並轉換到Eclipse Web項目須要執行以下命令提示符:ui
1
|
mvn eclipse:eclipse -Dwtpversion=2.0
|
複查Eclipse的classpath文件,下面的Struts2的依賴關係被下載:this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<
classpath
>
<
classpathentry
kind
=
"src"
path
=
"src/main/java"
including
=
"**/*.java"
/>
<
classpathentry
kind
=
"src"
path
=
"src/main/resources"
excluding
=
"**/*.java"
/>
<
classpathentry
kind
=
"output"
path
=
"target/classes"
/>
<
classpathentry
kind
=
"var"
path
=
"M2_REPO/asm/asm/3.3/asm-3.3.jar"
/>
<
classpathentry
kind
=
"var"
path
=
"M2_REPO/asm/asm-commons/3.3/asm-commons-3.3.jar"
/>
<
classpathentry
kind
=
"var"
path
=
"M2_REPO/asm/asm-tree/3.3/asm-tree-3.3.jar"
/>
<
classpathentry
kind
=
"var"
path
=
"M2_REPO/commons-fileupload/commons-fileupload/1.2.2/commons-fileupload-1.2.2.jar"
/>
<
classpathentry
kind
=
"var"
path
=
"M2_REPO/commons-io/commons-io/2.0.1/commons-io-2.0.1.jar"
/>
<
classpathentry
kind
=
"var"
path
=
"M2_REPO/commons-lang/commons-lang/2.5/commons-lang-2.5.jar"
/>
<
classpathentry
kind
=
"var"
path
=
"M2_REPO/org/freemarker/freemarker/2.3.18/freemarker-2.3.18.jar"
/>
<
classpathentry
kind
=
"var"
path
=
"M2_REPO/javassist/javassist/3.11.0.GA/javassist-3.11.0.GA.jar"
/>
<
classpathentry
kind
=
"var"
path
=
"M2_REPO/junit/junit/3.8.1/junit-3.8.1.jar"
/>
<
classpathentry
kind
=
"var"
path
=
"M2_REPO/ognl/ognl/3.0.4/ognl-3.0.4.jar"
/>
<
classpathentry
kind
=
"var"
path
=
"M2_REPO/org/apache/struts/struts2-core/2.3.1.2/struts2-core-2.3.1.2.jar"
/>
<
classpathentry
kind
=
"lib"
path
=
"C:/Program Files/Java/jdk1.6.0_13/lib/tools.jar"
/>
<
classpathentry
kind
=
"var"
path
=
"M2_REPO/org/apache/struts/xwork/xwork-core/2.3.1.2/xwork-core-2.3.1.2.jar"
/>
<
classpathentry
kind
=
"con"
path
=
"org.eclipse.jdt.launching.JRE_CONTAINER"
/>
</
classpath
>
|
下面的登錄頁面用的是Struts2標籤,裏面包含一個用戶名,密碼和一個提交按鈕
login.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<
html
>
<
head
></
head
>
<
body
>
<
h1
>Struts 2 Hello World Example</
h1
>
<
s:form
action
=
"Welcome"
>
<
s:textfield
name
=
"username"
label
=
"Username"
/>
<
s:password
name
=
"password"
label
=
"Password"
/>
<
s:submit
/>
</
s:form
>
</
body
>
</
html
>
|
welcome_user.jsp ,這個頁面是給用戶展現歡迎信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<
html
>
<
head
></
head
>
<
body
>
<
h1
>Struts 2 Hello World Example</
h1
>
<
h4
>
Hello
<
s:property
value
=
"username"
/>
</
h4
>
</
body
>
</
html
>
|
Struts1和Struts2的標籤語法很是類似,僅僅是標籤的命名上有點小區別,例如:
Struts1以下所示:
1
2
3
4
|
<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<
html:form
action
=
"Welcome"
>
<
html:text
property
=
"username"
/>
</
html:form
>
|
Struts2以下所示:
1
2
3
|
<%@ taglib prefix="s" uri="/struts-tags" %>
<
s:form
action
=
"Welcome"
>
<
s:textfield
name
=
"username"
label
=
"Username"
/>
|
這是一個簡單的Struts2 Action類,這裏用來處理全部的業務邏輯。
WelcomeUserAction.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
package
com.mkyong.user.action;
public
class
WelcomeUserAction{
private
String username;
public
String getUsername() {
return
username;
}
public
void
setUsername(String username) {
this
.username = username;
}
// all struts logic here
public
String execute() {
return
"SUCCESS"
;
}
}
|
在Struts2中,須要實現任何接口或者任何父類,可是它須要建立一個execute()方法用來處理全部的業務邏輯並返回一個String類型的值用來告訴用戶跳轉到哪兒去。
注意1:你也許看到有些開發者實現了com.opensymphony.xwork2.Action類,可是它不是必須須要實現的類,由於這個類只是提供了幾個常量而已
注意2:Struts1是必須實現com.opensymphony.xwork2.Action類的,可是Struts2是可選的,你也能夠實現com.opensymphony.xwork2.Action類去使用一些常量或者你能夠繼承com.opensymphony.xwork2.ActionSupport去使用某些默認的通用的值
Struts2的配置文件用來配置把全部的東西組裝在一塊兒,這個xml文件名必須爲「struts.xml」
Struts.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
<
struts
>
<
package
name
=
"user"
namespace
=
"/User"
extends
=
"struts-default"
>
<
action
name
=
"Login"
>
<
result
>pages/login.jsp</
result
>
</
action
>
<
action
name
=
"Welcome"
class
=
"com.mkyong.user.action.WelcomeUserAction"
>
<
result
name
=
"SUCCESS"
>pages/welcome_user.jsp</
result
>
</
action
>
</
package
>
</
struts
>
|
這裏面配置了包名和Action類名,Action類名你也許很熟悉,但你也許會對下面的新標籤感興趣:
1.包名name=」user」
只是一個包名而已,不用太在乎
2.命名空間namespace=」/User」
它用來匹配「/User」路徑
3.繼承包名 extends=」struts-default」
它的意思是集成struts-default的組件和攔截器,它聲明在struts-default.xml文件裏,這個文件存放在struts2-core.jar 文件裏
在你的項目的web.xml文件裏配置Struts2
web.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
<
web-app
>
<
display-name
>Struts 2 Web Application</
display-name
>
<
filter
>
<
filter-name
>struts2</
filter-name
>
<
filter-class
>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</
filter-class
>
</
filter
>
<
filter-mapping
>
<
filter-name
>struts2</
filter-name
>
<
url-pattern
>/*</
url-pattern
>
</
filter-mapping
>
</
web-app
>
|
在struts2裏,訪問這個Action類你能夠添加「.action」擴展名
ttp://localhost:8080/Struts2Example/User/Login.action
http://localhost:8080/Struts2Example/User/Welcome.action
原創文章,轉載請註明出處:http://www.it161.com/article/javaDetail?articleid=140112224129
更多原創內容,請訪問:http://www.it161.com/