简单入门struts2
1 什么是struts2?
- Struts 2是在 struts 1和WebWork的技术基础上进行了合并的全新的Struts 2框架,struts2以webwork为核心,是一个标准的MVC框架。
个请求在Struts2框架中的处理大概分为以下几个步骤 :
1 客户端请求初始化一个Http请求指向Servlet容器;
2 这个请求经过一系列的过滤器(Fileter),ActionContextCleanUp等
3 请求到达FilterDispather,FilterDispather询问ActionMapper决定这个请求调用哪个action;
4 如果ActionMapper决定需要调用某个Action,FilterDispatcher把请求的处理交给ActionProxy ;
5 ActionProxy通过ConfigurationManager询问框架的配置文件,找到需要调用的Action类 ,这里,我们一般是从struts.xml配置中读取。
6 ActionProxy创建一个ActionInvocation的实例。
7 ActionInvocation实例使用命名模式来调用,在调用Action的过程前后,涉及到相关拦截器(Intercepter)的调用。
简化流程图片为:
搭建struts开发环境:
1 创建一个web工程,
2 在该工程中 的webRoot--Web-Info目录下的lib文件中导入相关的struts的核心包,
没有的可以在这里下载,链接:https://pan.baidu.com/s/1c4bFgKo 密码:6x6m
3 配置web.xml (web.xml文件中配置前端控制器(核心控制器)—–就是一个Filter )
<!-- 配置struts2核心拦截器dispatcherServlet -->
<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 在src文件下配置struts.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="helloWorld" namespace="/" extends="struts-default" >
<!--action标签中的name为要访问的地址,class为创建action对应的路径, method为action中要调用的方法-->
<action name="hello" class="com.zb.controller.HelloAction" method="login">
<result name="success">/index.jsp</result>
</action>
</package>
</struts>
5 创建相应的action:
6 将项目布置到服务器上,启动服务,浏览器上访问 : http://localhost:8080/StrutsDemo01/hello