zoukankan      html  css  js  c++  java
  • Struts2

    Struts2 MVC框架,
    Struts2-Core Struts2核心包
    Xwork-core Xwork 核心,构建基础
    JavaSist-GA 底层字节码生成
    File-upload 文件上传
    --------------------
    commons-io IO操作
    commons-lang 数据类型的处理工具类
    freemarker 模板引擎
    ognl 表达式

    1.依赖引入
    <!--获取ServletAPI-->
    <dependency>
    <groupId>javaee</groupId>
    <artifactId>javaee-api</artifactId>
    <version>5</version>
    </dependency>

    <dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-core</artifactId>
    <version>2.3.4.1</version>
    </dependency>

    <dependency>
    <groupId>org.apache.struts.xwork</groupId>
    <artifactId>xwork-core</artifactId>
    <version>2.3.4.1</version>
    </dependency>

    2.配置web.xml
    SpringMVC 普通的Servlet
    配置的是什么?
    解析:过滤器 核心过滤器 StrutsPrepareAndExcuteFilter /*拦截的是所有Action 其实就是一个特定功能的类

    3.view视图
    <%@ page pageEncoding="UTF-8" language="java" %>
    <html>
    <body>
    <h2>Hello,Strut2 ,Must See Me~~~~~~~~~~~呵</h2>
    </body>
    </html>

    4.定制Action
    public class UserAction implements Action{
    //SUCCESS NONE INPUT LOGIN ERROR
    //SpringMvc Controller
    public String execute() throws Exception {
    return SUCCESS;
    }
    }
    5.struts.xml 在resources文件夹下定义一个名称为struts.xml的文件
    根节点Struts节点
    <package name="default" namespace="/" extends="struts-default">
    <action name="userAction" class="cn.happy.action.UserAction">
    <result name="success">/success.jsp</result>到底投影到哪个视图做显示
    </action>
    </package>

    6.部署运行
    2.入门案例
    3.自动装配
    Struts2 自动装配 AutoAWired(PageUI 中的表单元素的name属性值 ----->Action的属性)
    拦截器
    零散参数的自动装配


    JavaBean类型的自动装配 PageUI 紧耦合 xxx yyy Action属性名 info

    ModelDriven

    4.Struts2访问Servlet API
    01.什么是Servlet API?
    解析:实现接口重写方法 继承类,重写方法。
    02.session的类型 类型名 HttpSession

    小Tip:之前我们学习的session.其实底层的数据结构就是一个Map集合


    ServletAPI 去获取常见对象Session ,request
    与Servlet API解耦的访问方式
    方案一: 对Servlet API进行封装 ,借助ActionContext
    --->01.使用ActionContext类获取ServletAPI对象对应的Map对象
    获取session对象
    Map<String,Object> map=ActionContext.getContext().getSession();

    获取application对象
    Map<String,Object> map=ActionContext.getContext().getApplication();

    获取request对象
    ActionContext context=ActionContext.getContext();


    方案二:向Action中注入ServletAPI对象对应的Map对象
    --->02.Struts2向Action注入ServletAPI对象对应的Map对象

    LoginBeanAction implements Action,SessionAwire{
    private Map<String,Object> map;
    public void setSession(Map<String,Object> map){
    this.map=map;
    }
    }

    与Servlet API耦合的访问方式
    方式一:通过ActionContext的子类ServletActionContext实现


    方式二:向Action实例注入Servlet API对象


    类似于 ServletRequest 和HttpServletRequest 仅对http有效
    SerlvetResponse和HttpServletResponse 仅对http有效

    使用Struts2操作ServletAPI
    解耦:
    方式一:Map<String,Object> map=ActionContext.getContext().getSession();

    Struts2 核心控制器: StrutsPrepareAndExecuteFilter 过滤器
    SpringMVC 核心控制器:Servlet DispatcherServlet

    2.Namespace空间的使用
    作用:隔离同名的Action
    分模块开发,每个模块必须有不同的name,namespace按模块名称命名。
    所以说就衍生除了 namespace="/"
    namespace"/oa"
    namespace="/market"
    namespace="job"
    <include file="struts-day03.xml"></include>


    方式二:注入SessionAware
    getSession(Map<String,Object> map){
    }

    耦合:
    方式一:HttpSession session=ServletActionContext.getRequest().getSession();

    方式二:实现接口ServletRequestAware

    public void setServletRequest(HttpServletRequest httpServletRequest) {
    this.httpServletRequest=httpServletRequest;
    }

    2.Struts2 自动装配
    零散参数 Page UI 表单元素的名字--------->Action的成员变量的名称一致

    对象类型 Page UI info.userName ---------->info

    ModelDriven<UserInfo> 手动的new成员变量 Page UI userName ------------>info

    3.Struts2标签
    <s:form action="" method="">

    <s:textfield>

    <s:password>

    <s:submit>

  • 相关阅读:
    【leetcode】106. Construct Binary Tree from Inorder and Postorder Traversal
    【leetcode】105. Construct Binary Tree from Preorder and Inorder Traversal
    【leetcode】236. Lowest Common Ancestor of a Binary Tree
    【leetcode】235. Lowest Common Ancestor of a Binary Search Tree
    【leetcode】352. Data Stream as Disjoint Intervals
    【leetcode】897. Increasing Order Search Tree
    【leetcode】900. RLE Iterator
    BEC listen and translation exercise 26
    BEC listen and translation exercise 25
    BEC listen and translation exercise 24
  • 原文地址:https://www.cnblogs.com/wyl123/p/8472534.html
Copyright © 2011-2022 走看看