一 创建一个Web project
名称为HelloWorld,创建界面如下:
![](http://dl2.iteye.com/upload/attachment/0127/4564/e315c444-79ec-3790-ab1d-bcb70e6a4ccc.png)
二 安装struts
截图如下:
![](http://dl2.iteye.com/upload/attachment/0127/4566/f57a12f3-24de-359b-ac35-60deacb59e68.png)
![](http://dl2.iteye.com/upload/attachment/0127/4568/f7c71686-dc4d-32b1-8c43-9d2db22693d3.png)
三 生成项目后,框架自动生成过滤器
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
web-app
version
=
"3.0"
<
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
>
*.action
</
url-pattern
>
</
filter-mapping
></
web-app
>
四 框架自动生成struts
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<!
DOCTYPE
struts
PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"
http://struts.apache.org/dtds/struts-2.1.dtd"
;
>
<
struts
>
</
struts
>
五 创建action
截图如下:
![](http://dl2.iteye.com/upload/attachment/0127/4570/bc9309ee-6732-36fd-8ec0-a17d4e50a4e3.png)
![](http://dl2.iteye.com/upload/attachment/0127/4572/8a846bd9-42ca-3001-a508-9ca90d2bf518.png)
package
com.cakin.action;
import
com.opensymphony.xwork2.ActionSupport;
public
class
HelloWorldAction
extends
ActionSupport {
@Override
public
String execute()
throws
Exception {
System.
out
.println(
"执行Action"
);
return
SUCCESS
;
}
}
六 配置struts.xml文件
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<!
DOCTYPE
struts
PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"
http://struts.apache.org/dtds/struts-2.1.dtd"
;
>
<
struts
>
<
package
name
=
"default"
namespace
=
"/"
extends
=
"struts-default"
>
<
action
name
=
"helloworld"
class
=
"com.cakin.action.HelloWorldAction"
>
<
result
>
/result.jsp
</
result
>
</
action
>
</
package
>
</
struts
>
七 创建result.jsp文件
<!
DOCTYPE
HTML
PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN">
<
html
>
<
head
>
<
base
href
=
"
<%=
basePath
%>
"
>
<
title
>
My JSP 'result.jsp' starting page
</
title
>
<
meta
http-equiv
=
"pragma"
content
=
"no-cache"
>
<
meta
http-equiv
=
"cache-control"
content
=
"no-cache"
>
<
meta
http-equiv
=
"expires"
content
=
"0"
>
<
meta
http-equiv
=
"keywords"
content
=
"keyword1,keyword2,keyword3"
>
<
meta
http-equiv
=
"description"
content
=
"This is my page"
>
<!--
<link
rel
="
stylesheet
" type="text/
css
"
href
="styles.css">
-->
</
head
>
<
body
>
This is my JSP page.
<
br
>
</
body
>
</
html
>
八 测试
![](http://dl2.iteye.com/upload/attachment/0127/4574/c7a591bc-5a6d-327c-b87d-50a04ec4ff90.png)