首页

源码搜藏网

首页 > 开发教程 > 软件工程 >

Struts2学习第一天——struts2基本流程与配置

创建时间:2016-06-14 17:43  

文档版本 开发工具 测试平台 工程名字 日期 作者 备注
V1.0 2016.06.12 lutianfei none

struts2框架

struts2快速入门

Struts2学习第一天——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>
* 2.创建一个struts.xml配置文件 ,这个是struts2框架配置文件。
    * 目的:是为了struts2框架流程可以执行。
    * 名称:struts.xml
    * 位置:src下(classes下)
public class HelloAction {
    public String say(){
        System.out.println("hello world");
        return "good"; // 结果页面命名
    }
}
    <package name="default" namespace="/" extends="struts-default">
        <action name="hello" class="cn.itcast.action.HelloAction"
            method="say">
            <result name="good">/hello.jsp</result>
        </action>
    </package>


Struts2 处理流程

Struts2学习第一天——struts2基本流程与配置

Struts2学习第一天——struts2基本流程与配置


模仿struts2流程完成入门程序

    <filter>
        <filter-name>struts</filter-name>
        <filter-class>cn.itcast.filter.StrutsFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
// 2.1 得到请求资源路径
String uri = request.getRequestURI();
String contextPath = request.getContextPath();
String path = uri.substring(contextPath.length() + 1);

// System.out.println(path); // hello

// 2.2 使用path去struts.xml文件中查找某一个<action name=path>这个标签
SAXReader reader = new SAXReader();
// 得到struts.xml文件的document对象。
Document document = reader.read(new File(this.getClass()
        .getResource("/struts.xml").getPath()));

Element actionElement = (Element) document
        .selectSingleNode("//action[@name='" + path + "']"); // 查找<action name='hello'>这样的标签

if (actionElement != null) {
    // 得到<action>标签上的class属性以及method属性
    String className = actionElement.attributeValue("class"); // 得到了action类的名称
    String methodName = actionElement.attributeValue("method");// 得到action类中的方法名称。

    // 2.3通过反射,得到Class对象,得到Method对象
    Class actionClass = Class.forName(className);
    Method method = actionClass.getDeclaredMethod(methodName);

    // 2.4 让method执行.
    String returnValue = (String) method.invoke(actionClass
            .newInstance()); // 是让action类中的方法执行,并获取方法的返回值。

    // 2.5
    // 使用returnValue去action下查找其子元素result的name属性值,与returnValue做对比。
    Element resultElement = actionElement.element("result");
    String nameValue = resultElement.attributeValue("name");

    if (returnValue.equals(nameValue)) {
        // 2.6得到了要跳转的路径。
        String skipPath = resultElement.getText();

        // System.out.println(skipPath);

        request.getRequestDispatcher(skipPath).forward(request,
                response);
        return;
    }
}


struts2的流程分析以及工具配置

Struts2学习第一天——struts2基本流程与配置

Struts2学习第一天——struts2基本流程与配置




struts2配置(重点)

1.struts2配置文件加载顺序

init_DefaultProperties(); // [1]   ----------  org/apache/struts2/default.properties 
init_TraditionalXmlConfigurations(); // [2]  --- struts-default.xml,struts-plugin.xml,struts.xml
init_LegacyStrutsProperties(); // [3] --- 自定义struts.properties 
init_CustomConfigurationProviders(); // [5]  ----- 自定义配置提供
init_FilterInitParameters() ; // [6] ----- web.xml 
init_AliasStandardObjects() ; // [7] ---- Bean加载 


2.关于Action的配置

    <package name="default" namespace="/" extends="struts-default">
        <action name="hello" class="cn.itcast.action.DefaultAction">
            <result>/hello.jsp</result>
        </action>
    </package>
    * 此时输入: http://localhost/struts2_day01_2/a/b/c/hello 也访问到了action。

    * 原因:struts2中的action被访问时,它会首先查找
        * 1.namespace="/a/b/c"  action的name=hello  没有.
        * 2.namespace="/a/b     action的name=hello  没有
        * 3.namespace="/a"      action的name=hello  没有
        * 4.namespace="/"       action的name=hello  查找到了.
    * 如果最后也查找不到,会报404错误.

* 3.默认的action。
    * 作用:处理其它action处理不了的路径。
    * `<default-action-ref name="action的名称" />`
    * 当访问的路径,其它的action处理不了时,就会执行name指定的名称的action。

* 4.action的默认处理类
    * 在action配置时,如果class不写。默认情况下是 com.opensymphony.xwork2.ActionSupport
    * `<default-class-ref class="cn.itcast.action.DefaultAction"/>`
    * 如上设置,在当前包下,默认处理action请求的处理类就为class指定的类。即:当`<action name="xxx" class="">`中class省略时,按照default-class-ref中的class设置认定对应的类。


关于常量配置

        <init-param>
            <param-name>struts.action.extension</param-name>
            <param-value>do,,</param-value>
        </init-param>




Action

关于Action类的创建方式介绍


关于action的访问方式

    <a href="${pageContext.request.contextPath}/Book_add">book add</a><br>
    <a href="${pageContext.request.contextPath}/Book_update">book update</a><br>
    <a href="${pageContext.request.contextPath}/Book_delete">book delete</a><br>
    <a href="${pageContext.request.contextPath}/Book_search">book search</a><br>
* 当访问book add时,这时的路径是  Book_add,那么对于struts.xml文件中.
* `*_*`代表匹配两个字符串
    * {1} 匹配UserAction 用于执行class
    * {2} 匹配login用于指定method执行方法 和结果页面
    * 第一个星就是   Book
    * 第二个星就是   add
    * 对于{1}Action---->BookAction
    * 对于method={2}--->method=add

* 使用通配符来配置注意事项:
    * 1.必须定义一个统一的命名规范。
    * 2.不建议使用过多的通配符,阅读不方便。

Struts2学习第一天——struts2基本流程与配置

Struts2学习第一天——struts2基本流程与配置

<action name="book" class="cn.itcast.action.BookAction"></action>
* 访问时路径: http://localhost/struts2_day01_2/book!add
    * 就访问到了BookAction类中的add方法。
* 对于`book!add` 这就是动态方法调用。
* 注意:struts2框架支持动态方法调用,是因为在`default.properties`配置文件中设置了动态方法调用为**true**.
    * 第108行 `struts.enable.DynamicMethodInvocation = true`




在struts2框架中获取servlet API

    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }
    <interceptor name="servletConfig" class="org.apache.struts2.interceptor.ServletConfigInterceptor"/>

     if (action instanceof ServletRequestAware) { //判断action是否实现了ServletRequestAware接口
        HttpServletRequest request = (HttpServletRequest) context.get(HTTP_REQUEST); //得到request对象.
        ((ServletRequestAware) action).setServletRequest(request);//将request对象通过action中重写的方法注入。
    }


Result结果类型

    <result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>

    <result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>

    <result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>

    <result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>

    <result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>

    <result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>

    <result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>

    <result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>

    <result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>

    <result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" />


Struts2学习第一天——struts2基本流程与配置


    <action name="result" class="cn.itcast.struts2.demo6.ResultAction">
                <!-- 局部结果  当前Action使用 -->
                <result name="success">/demo6/result.jsp</result> 
    </action>

    <global-results>
                <!-- 全局结果 当前包中 所有Action都可以用-->
                <result name="success">/demo6/result.jsp</result>
    </global-results>


<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>

    <package name="default" namespace="/" extends="struts-default">
        <action name="login" class="cn.itcast.action.LoginAction">
            <result name="failer">/login.jsp</result>
            <result type="redirect">/success.jsp</result>
        </action>
    </package>
</struts>


<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>

    <package name="default" namespace="/" extends="struts-default">
        <action name="login" class="cn.itcast.action.LoginAction">
            <result name="failer">/login.jsp</result>
            <result type="redirect">/success.jsp</result>
        </action>

        <action name="login1" class="cn.itcast.action.Login1Action">
            <result name="failer">/login1.jsp</result>
            <result type="redirect">/success.jsp</result>
        </action>

        <action name="login2" class="cn.itcast.action.Login2Action">
            <result name="failer">/login2.jsp</result>
            <result type="redirect">/success.jsp</result>
        </action>
        <action name="login3" class="cn.itcast.action.Login3Action">
            <result name="failer">/login3.jsp</result>
            <result type="redirect">/success.jsp</result>
        </action>

        <action name="list" class="cn.itcast.action.ListAction">
        </action>

        <action name="map" class="cn.itcast.action.MapAction">
        </action>


    </package>
</struts>


//login.jsp
<html>
  <head>
  </head>

  <body>
    ${requestScope["login.message"] }<br>
    <form action="${pageContext.request.contextPath}/login" method="post">
        username:<input type="text" name="username"><br>
        password:<input type="password" name="password"><br>
        <input type="submit" value="登录">
    </form>
  </body>
</html>



//success.jsp
<html>
  <head>
  </head>

  <body>
    ${username}
  </body>
</html>
1
0
   
上一篇:《Linux系统编程》第二章笔记(一)
下一篇:[LeetCode][13]Roman to Integer解析 罗马字符转int类型关于栈的

相关内容

热门推荐