zoukankan      html  css  js  c++  java
  • Struts2之Json插件的使用

    时间:2017-1-15 02:27

    ——普通方式处理异步请求:

        ServletActionContext.getResponse().getWriter().print("aa");



    ——Struts2中怎样处理异步请求

    1、步骤:
        1)导入json插件包:struts2-json-plugin-2.3.15.1.jar
            这个jar包中有一个struts-plugin.xml,其中的package继承了struts-default.xml:
                <package name="json-default" extends="struts-default">


        2)继承json-default
            以后struts.xml继承json-default即可:
                <package name="default" namespace="/" extends="json-default">

        3)定义视图
            在json的jar包中定义了一个视图:
                <result-types>
                    <result-type name="json" class="org.apache.struts2.json.JSONResult"/>
                </result-types>

            在struts.xml中设置视图:
                <action name="AjaxDemo2" class="com.wyc.action.AjaxDemo2Action">
                    <result type="json"></result>
                </action>

            这样设置后,会将ValueStack栈顶数据变成Json对象。
            也就是会将Action对象转换成Json对象。 

    2、<result type="json">参数
        root属性
            <result type="json">标签下有一个参数:
                <result name="success" type="json">
                    <param name="root">ps</param>
                </result>
            如果没有设置param,可以理解为将整个Action都设置为json数据,也就是说只要在Action中提供了getXxx()方法,就是json中的一个属性。
            如果设置了root,那么只将指定的数据转换成json对象。


    3、怎样设置转换成json的对象中不包含特定的属性
        也就是说在获取的json字符串中,不包含id、name等指定属性。
        1)在getXxx()方法上设置注解:
            @JSON(serialize = false)

        2)还可以通过json插件的interceptor完成
            <action name="AjaxDemo2" class="com.wyc.action.AjaxDemo2Action">
                <result name="success" type="json">
                    <param name="root">ps</param>
                    <param name="includeProperties">ps[d+].name,ps[d+].price,ps[d+].count</param>
                    <param name="excludeProperties">ps[d+].id</param>
                </result>
            <result name="jsp">/index.jsp</result>

            root表示要转换的对象。
            includeProperties表示需要被转换的对象。
            excludeProperties表示不需要被转换的对象。
  • 相关阅读:
    Html禁止粘贴 复制 剪切
    表单标签
    自构BeanHandler(用BeansUtils)
    spring配置中引入properties
    How Subcontracting Cockpit ME2ON creates SD delivery?
    cascadia code一款很好看的微软字体
    How condition value calculated in sap
    Code in SAP query
    SO Pricing not updated for partial billing items
    Javascript learning
  • 原文地址:https://www.cnblogs.com/wwwwyc/p/6375443.html
Copyright © 2011-2022 走看看