碎片化 | 第四阶段-29-Struts2入门示例1-视频

时间:2022-05-06
本文章向大家介绍碎片化 | 第四阶段-29-Struts2入门示例1-视频,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
视频内容

如清晰度低,可转PC网页观看高清版本:

http://v.qq.com/x/page/u0565acptbm.html

入门示例

1.hello Struts2

流程:/hello.action->StrutsPrePareAndExecuteFilter(web.xml)->HelloAction->Result(dipatcher)(sturts.xml)->hello.jsp

1:编写hello.jsp

2:编写HelloAction.java

    controller:
        public @ResponseBody String XXXX(String name){...}

    action:
        private String name;

3:配置web.xml中Struts2的启动项StrutsPrePareAndExecuteFilter

    <!-- struts2的启动拦截 -->
    <filter>
        <filter-name>strutsMVC</filter-name>
        <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>strutsMVC</filter-name>
        <url-pattern>*.action</url-pattern>
    </filter-mapping>

4:添加struts.xml
    <!-- namespace默认是/ -->
    <package name="demo" extends="struts-default" namespace="/demo">
        <!--name代表的请求路径 ,class对应的就是请求处理类,method默认的就是execute -->
        <action name="hello" class="com.xdl.action.HelloAction" method="execute">
            <!-- result代表的就是方法返回处理结果,type:dispather, -->
            <result name="success" type="dispatcher">/WEB-INF/jsp/hello.jsp</result>
        </action>
    </package>

请求名:

/hello.action:接口请求名:hello 请求后缀名:.action,命名空间路径/

/demo1/dddd/hello.action:接口请求名:hello,请求后缀名:.action,命名空间路径:/demo1/dddd/