Struts2的配置与简单案例:
1. 创建一个dynamic web project(创建时让它自动生成web.xml文件)
2.引入相关jar包
3.在web.xml中进行配置
(启动tomcat服务器之后第一个加载的文件就是web.xml)
在配置中添加过滤器:
1
2
3
4
5
6
7
8
|
<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>
|
4.创建struts的核心文件(struts.xml),将其创建在Java Resources-src文件目录下,内容为:
1
2
3
4
5
6
|
<? 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 >
</ struts >
|
5.同样在Java Resources-src文件目录下创建一个Action类,继承自ActionSupport,并覆盖父类中的execute方法:
1
2
3
4
5
6
7
|
public class HelloWorldAction extends ActionSupport {
@Override
public String execute() throws Exception {
System.out.println( "执行Action" );
return SUCCESS;
}
}
|
6.在struts.xml中的<struts>标签中添加:
1
2
3
4
5
6
7
|
< struts >
< package name = "default" namespace = "/" extends = "struts-default" >
< action name = "helloworld" class = "default package.HelloWorldAction" >
< result >/result.jsp</ result >
</ action >
</ package >
</ struts >
|
7.创建视图(在WebRoot目录下创建result.jsp):
1
2
3
|
< body >
This is result.jsp!
</ body >
|
8.调试运行
希望本篇文章对您有所帮助