Struts2 入门的学习笔记。
概念知识不写于此处了,直接进入项目实现部分。
步骤:
1. 在MyEclipse中创建Web Project工程。
2. 下载struts2的jar包,我下载的版本struts-2.1.6,解压后有4个文件夹有lib(jar包),apps(案例程序),docs(文档),src(源文件)。
     选择以下jar包导入到工程当中:commons-fileupload-1.2.1,commons-io-1.3.2,commons-logging-1.1,freemarker-2.3.13,ognl-2.6.11,struts2-core-2.1.6,xwork-2.1.2。
3. 创建struts.xml文件在src根目录下,代码如下:
<?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>
 <constant name="struts.devMode" value="true" />    设置struts2为开发模式,当修改struts.xml文件不用重新部署项目
 <package name="default" namespace="/" extends="struts-default">         namespace查询action的路径设置
        <action name="hello">       action的名称
            <result>
                /Hello.jsp                   result对应action的结果
            </result>
        </action>
    </package>
</struts>
4. 配置web.xml文件,代码如下:
    固定写法,把struts2配置成一个过滤器。
    <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>
5. 在WebRoot下创建一个Hello.jsp文件。
6. 部署项目启动服务器,在浏览器地址栏输入http://localhost:8083/struts2/hellos或者http://localhost:8083/struts2/hello.action。
    可以有action也可以没有。
注意:struts2.1.6这个版本有一个bug是当你开启了struts2的开发模式时,必须服务器路径不能有空格出现,否则服务器无法正确启动,这点需要各位调整。
Struts2的执行过程:客户端请求通过Url,找到对应项目中的web.xml,又根据过滤器做doFilter操作,找到struts.xml文件做namespace,action,result操作,按result进行forward到对应的视图,最后把该视图响应给客户端。
执行的时序图如下:
