一、找到开发Struts2应用需要使用到的jar文件:
- asm-x.x.jar:官方说要加,但我不加也行
- asm-commons-x.x.jar:官方说要加,但我不加也行
- asm-tree-x.x.jar:官方说要加,但我不加也行
- Struts2-core-2.x.x.jar:Struts2框架的核心类库;
- xwork-2.x.x.jar :xwork类库,Struts2是在其之上构建的;
- ognl-2.6.x.jar:对象图导航语言(Object Graph Navigation Language),Struts2框架通过其读写对象属性;
- freemarker-2.2.x.jar:Struts2的UI标签的模板使用freemarker编写;
- commons-fileupload-1.2.1.jar:文件上传组件, 2.1.6版本后必须加入文件;
- javassist-3.11.0.GA.jar:Javassist是一个开源的分析、编辑和创建Java字节码的类库;
- commons-io-2.0.1.jar:IO输入输出流组件,主要完成文件的读写功能;
- commons-lang3-3.1.jar:commons-lang是一个很有用的开源包,它弥补了Java API在提供的基本处理方法上的不足;
- commons-lang3-X.X.X.jar: as from version 2.3.3 Struts 2 bases on Commons Lang 3, but there are still external dependencies that base on Commons Lang.
- commons-logging-1.1.x.jar:ASF出品的日志包,Struts2框架使用这个日志包来支持Log4J和JDK1.4+的日志记录(如果不想显示日志的话,不需要导入)。
二、 编写Struts2的配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<!
DOCTYPE
struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<
struts
>
<
constant
name
=
"struts.devMode"
value
=
"true"
></
constant
>
<
constant
name
=
"struts.i18n.encoding"
value
=
"utf-8"
></
constant
>
<
package
name
=
"struts"
namespace
=
"/"
extends
=
"struts-default"
>
<
default-action-ref
name
=
"test"
></
default-action-ref
>
<
global-results
>
<
result
name
=
"success"
>/success.jsp</
result
>
<
result
name
=
"error"
>/error.jsp</
result
>
</
global-results
>
<
action
name
=
"test"
class
=
"com.test.action.TestAction"
>
<
result
name
=
"index"
>/index.jsp</
result
>
</
action
>
</
package
>
</struts
|
三、 在web.xml中加入Struts2 MVC框架启动配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
web-app
version
=
"3.0"
xmlns
=
"http://java.sun.com/xml/ns/javaee"
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<
display-name
></
display-name
>
<
welcome-file-list
>
<
welcome-file
>index.jsp</
welcome-file
>
</
welcome-file-list
>
<
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
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
web-app
version
=
"3.0"
xmlns
=
"http://java.sun.com/xml/ns/javaee"
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<
display-name
></
display-name
>
<
welcome-file-list
>
<
welcome-file
>index.jsp</
welcome-file
>
</
welcome-file-list
>
<
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
>
|
四、 编写TestAction类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package
com.test.action;
import
com.opensymphony.xwork2.ActionSupport;
public
class
TestAction
extends
ActionSupport {
private
static
final
long
serialVersionUID = 1L;
public
String success() {
return
SUCCESS;
}
public
String error() {
return
ERROR;
}
public
String index() {
return
"index"
;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
package
com.test.action;
import
com.opensymphony.xwork2.ActionSupport;
public
class
TestAction
extends
ActionSupport {
private
static
final
long
serialVersionUID = 1L;
public
String success() {
return
SUCCESS;
}
public
String error() {
return
ERROR;
}
public
String index() {
return
"index"
;
}
}
|