struts2_基础

本文详细介绍了Struts2框架中的结果配置方法,包括全局结果配置与局部结果配置的区别及应用,各种结果类型如dispatcher、chain、redirect等的功能与配置方式。同时,还讲解了Struts2如何通过ActionContext和ServletActionContext与Servlet进行交互,以及Struts2的数据封装机制,如属性驱动和模型驱动等。

Struts2

Result结果配置

全局结果

package标签中配置global-results标签

<package name="xxx" namespace="/" extends="struts-default">
    <global-results>
        <result name="xxx">xxx.jsp</result>
    </global-results>
</package>

作用是为package中配置的所有action提供全局的返回结果页面

局部结果

action标签中配置result标签

<action name="xxx" method="xxx"
    class="xxx">
    <result>xxx.jsp</result>
</action>

作用是为当前action配置结果页面

结果类型

结果类型是在父类配置struts-default中定义的,struts框架默认实现了绝大多数的。
常用的结果类型有:

  1. dispatcher:转发。为result标签的默认值。

    <result type="dispatcher">xxx.jsp</result>

    由action转发到jsp。

    result标签中配置的结果必须是jsp页面

  2. chain:转发。

    <result type="chain">
        <param name="actionName">xxx.action</param>
    </result>   

    由action转发到action。

    result标签中配置的结果必须是action

  3. redirect:重定向。

    <result type="redirect">
        <param name="location">xxx.jsp</param>
    </result>

    由action重定向到jsp。

    result标签中配置的结果必须是jsp页面

  4. redirectAction:重定向。

    <result type="redirectAction">
        <param name="actionName">xxx.action</param>
    </result>   

    由action重定向到action。

    result标签中配置的结果必须是action

  5. stream: 结果为文件流。文件下载

    <result name="success" type="stream">
        <param name="contentType">image/jpeg</param>
        <param name="inputName">imageStream</param>
        <param name="contentDisposition">attachment;filename="document.pdf"</param>
        <param name="bufferSize">1024</param>
    </result>

    contentType:下载文件类型

    contentDisposition:下载到客户端时,客户端文件名称

    bufferSize:读文件的缓存大小

    inputName:对应要输出到客户端流声明的名称

  6. json:转发。

    <package name="xxx" namespace="/xxx" extends="json-default">
        <action name="json" method="xxx"
            class="org.itheima.struts.action.JsonAction">
            <result name="success" type="json">
                <param name="encoding">utf-8</param>
                <param name="root">jsonObject</param>
            </result>
        </action>   
    </package>

    由action转发到json结果

    encoding:配置编码格式

    root:配置对象。action类中必须提供一个和root值相同的属性名称,且需要提供getter方法。

  7. jsonActionRedirect: 重定向。

    <action name="xxx" method="xxx"
            class="org.itheima.struts.action.JsonAction">
            <result name="success" type="jsonActionRedirect">
                xxx.action
            </result>
    </action>

    由action重定向到json结果。

json Plugin的加载

  1. 拷贝struts2-json-plugin-xxx.jar
  2. 让package继承json-default

Struts与Servlet交互对接

解决的问题

客户端与服务端交互时,通常会带参数过来,服务器也会回写数据给客户端。在此过程中,参与着请求,和响应,以及会话。servlet在此过程中提供了HttpServletRequest作为获取请求数据的方案,HttpServletResponse作为响应的方案,HttpSession负责了会话方案。Struts其实是基于servlet实现的web框架,他需要遵循规则提供请求,响应和会话的API供开发人员使用,因此Struts针对这一问题提供了自己的一套API封装,提供了多种方式的访问。

ActionContext

  1. ActionContext对象实例获取

    ActionContext context = ActionContext.getContext();

    ActionContext是绑定在当前线程中的。框架在请求线程开启时,将ActionContext实例对象就绑定上来了,因此我们可以通过静态方法直接获取

  2. 请求参数的获得

    Map<String, Object> parameters = context.getParamters();

    相当于Servlet中的request.getParamters()方法

  3. 数据回显到页面

    context.put(key, value);

    相当于Servlet中的request.setAttribute()方法

ServletActionContext

  1. Servlet继承子ActionContext,因此具备ActionContext的一切功能。
  2. ServletActionContext是对ActionContext功能的扩展,提供了多个静态方法。
  3. 请求获得

    HttpServletRequest request = ServletActionContext.getRequest();
  4. 响应获得

    HttpServletResponse response = ServletActionContext.getResponse();

接口实现类获得

  • ServletContextAware
  • ServletRequestAware
  • ServletResponseAware
  • ParameterAware
  • SessionAware
  • ApplicationAware
  • PrincipalAware

Struts数据的封装

属性驱动:基本数据类型封装(掌握)

  1. 页面表单

    <input type="text" name="name">
    <input type="text" name="password">
  2. action类

    private String name;
    private String password;
    public void setName(String name) {
    this.name = name;
    }
    public void setPassword(String password) {
    this.password = password;
    }
    1. 要求页面中提供的key和action类中属性名称必须一致
    2. action类中必须提供属性的setter方法

页面表达式:对象封装

  1. 页面表单

    <input type="text" name="user.name">
    <input type="text" name="user.password">
  2. action类

    private User user;
    public void setUser(User user) {
    this.user = user;
    }
    public User getUser() {
    return user;
    }
    1. 页面中的key相当于对象.属性
    2. action中的对象名称需要要和页面中key的对象名称相同
    3. action中的对象中的属性名称要和页面中key的对象的属性相同
    4. action中的对象必须提供无参构造函数
    5. action中的对象必须提供getter和setter

模型驱动:对象封装(掌握)

  1. 页面表单

    <input type="text" name="name">
    <input type="text" name="password">
  2. Aciton类需要实现ModelDriven接口,并且实现接口方法

    public class Action03 extends ActionSupport implements ModelDriven<User> {
    private User user;
    @Override
    public User getModel() {
        if (user == null) {
            user = new User();
        }
        return user;
    }
    }

    页面中的key的名称需要和模型对象的名称一致

List数据(了解)

  1. 页面表单

    <input type="text" name="list[0].name">
    <input type="text" name="list[0].age">
    <input type="text" name="list[1].name">
    <input type="text" name="list[1].age">
  2. action类

    public class DataAction05 extends ActionSupport {
        private List<User> list;
        public List<User> getList() {
            return list;
        }
        public void setList(List<User> list) {
            this.list = list;
        }
        public String test() {
            System.out.println(list);
            return SUCCESS;
        }
    }

Map数据(了解)

  1. 页面表单

    <input type="text" name="map['a'].name">
    <input type="text" name="map['a'].age">
    <input type="text" name="map['b'].name">
    <input type="text" name="map['b'].age">
  2. action类

    public class DataAction05 extends ActionSupport {
        private Map<String, User> map;
        public Map<String, User> getMap() {
            return map;
        }
        public void setMap(Map<String, User> map) {
            this.map = map;
        }
    }

类型转换器

解决的问题

客户端传输过程中传输特定类型的字符串时,到action类中需要转换为对应的对象时,中间的转换的问题。
主要解决的是对象类型的转换

配置方案

  1. 新建一个类继承StrutsTypeConverter,实现内部方法

    public class DateConversion extends StrutsTypeConverter {
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
    @Override
    public Object convertFromString(Map context, String[] values, Class toClass) {
        if (values != null && values.length > 0) {
            String dateString = values[0];
            try {
                return sdf.parse(dateString);
            } catch (ParseException e) {
                e.printStackTrace();
                return null;
            }
        }
        return null;
    }
    @Override
    public String convertToString(Map context, Object o) {
        if (o != null && o instanceof Date) {
            return sdf.format(o);
        }
        return null;
    }
    }
  2. 在项目的src下新建xwork-conversion.properties,在其中配置要转换的类型

    java.util.Date=org.itheima.struts.action.DateConversion
基于可靠性评估序贯蒙特卡洛模拟法的配电网可靠性评估研究(Matlab代码实现)内容概要:本文围绕“基于可靠性评估序贯蒙特卡洛模拟法的配电网可靠性评估研究”,介绍了利用Matlab代码实现配电网可靠性的仿真分析方法。重点采用序贯蒙特卡洛模拟法对配电网进行长时间段的状态抽样与统计,通过模拟系统元件的故障与修复过程,评估配电网的关键可靠性指标,如系统停电频率、停电持续时间、负荷点可靠性等。该方法能够有效处理复杂网络结构与设备时序特性,提升评估精度,适用于含分布式电源、电动汽车等新型负荷接入的现代配电网。文中提供了完整的Matlab实现代码与案例分析,便于复现和扩展应用。; 适合人群:具备电力系统基础知识和Matlab编程能力的高校研究生、科研人员及电力行业技术人员,尤其适合从事配电网规划、运行与可靠性分析相关工作的人员; 使用场景及目标:①掌握序贯蒙特卡洛模拟法在电力系统可靠性评估中的基本原理与实现流程;②学习如何通过Matlab构建配电网仿真模型并进行状态转移模拟;③应用于含新能源接入的复杂配电网可靠性定量评估与优化设计; 阅读建议:建议结合文中提供的Matlab代码逐段调试运行,理解状态抽样、故障判断、修复逻辑及指标统计的具体实现方式,同时可扩展至不同网络结构或加入更多不确定性因素进行深化研究。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值