struts2基本流程

本文介绍了Struts2框架的基础概念及其在Java Web开发中的应用。Struts2融合了Struts1和WebWork的优点,简化了Web开发过程并提高了开发效率。文章详细阐述了Struts2的架构特点和核心功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

struts2框架

  • 什么是框架,框架有什么用?

    • 框架 是 实现部分功能的代码 (半成品),使用框架简化企业级软件开发 ,提高开发效率。
    • 学习框架 ,清楚的知道框架能做什么? 还有哪些工作需要自己编码实现 ?
  • 什么是struts2框架,它有什么用?

    • Struts 2是在 struts 1和WebWork的技术基础上进行了合并的全新的Struts 2框架。
    • 其全新的Struts 2的体系结构与Struts 1的体系结构差别巨大。Struts 2以WebWork为核心
    • struts2=struts1+webwork;
    • struts2框架是apache产品。
    • struts2是一个标准的mvc框架。
    • javaweb中的model2模式就是一个mvc模式。 model2=servlet+jsp+javaBean
    • struts2框架只能在javaweb开发中使用的。
    • 使用struts2框架,可以简化我们的web开发,并且降低程序的耦合度。
  • XWork—它是webwork核心,提供了很多核心功能:

    • 前端拦截机(interceptor)
    • 运行时表单属性验证
    • 类型转换
    • 强大的表达式语言(OGNL – the Object Graph Navigation Language)
    • IoC(Inversion of Control反转控制)容器等
  • 类似于struts2框架的产品 :

    • struts1 webwork jsf springmvc
    • ssh—struts2 spring hibernate
    • ssi—springmvc spring ibatis
  • Strust2 核心功能

    • 允许POJO(Plain Old Java Objects)对象 作为Action
    • Action的execute 方法不再与Servlet API耦合,更易测试
    • 支持更多视图技术(JSP、FreeMarker、Velocity)
    • 基于Spring AOP思想的拦截器机制,更易扩展
    • 更强大、更易用输入校验功能
    • 整合Ajax支持

模仿struts2流程完成入门程序
  • index.jsp
  • hello.jsp
  • HelloAction
  • struts.xml

  • 1.创建一个Filter—-StrutsFilter

  • 2.在web.xml文件中配置StrutsFilter
    <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>
  • 3.在StrutsFilter中完成拦截操作,并访问Action中的方法,跳转到hello.jsp页面操作.
// 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;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值