一 创建一个Web project
名称为HelloWorld,创建界面如下:
![](https://i-blog.csdnimg.cn/blog_migrate/8284f41c845a3c65a685ea3dd010d98f.png)
二 安装struts
截图如下:
![](https://i-blog.csdnimg.cn/blog_migrate/5e9b23f0d41ade9118759b7abbceb832.png)
![](https://i-blog.csdnimg.cn/blog_migrate/c81d0603fa1fe0875a0be6a39170ffe1.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
截图如下:
![](https://i-blog.csdnimg.cn/blog_migrate/73f51338cf6a1a538a25b951d434b0ed.png)
![](https://i-blog.csdnimg.cn/blog_migrate/965d6401ac74fb557ec76121ae5480ba.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
>
八 测试
![](https://i-blog.csdnimg.cn/blog_migrate/8b31cd315d83cc6d69113d56824430f5.png)