Jquery+Json+struts交互demo,以及为什么要配置includeProperties,root

上周四面试问到了json,坦白说,我只是见过这种用法,甚至我知道json也仅仅 j  s  o  n这四个字母,隐隐朦朦记得好像是一种类似多个数组拼装的数据格式。

看了几个json的demo。              

首先 1 struts.xml的配置 

<package name="system" namespace="/system" extends="json-default">


<result name="findJctUnit" type="json">
<param name="includeProperties">\[\d+\]\.ddlCode,\[\d+\]\.ddlName</param>
</result>
<result name="checkUser" type="json">
<!-- 将对象中的某个属性message的值放置到栈顶 -->
<param name="root">message</param>
<!-- 标识对象中只有message的属性被json话,此时页面调用的时候,data.message -->
<param name="includeProperties">message</param>
</result>

     2 Action层   注意(ElecSystemDDL对象含有ddlCodeddlName等多個属性,为了节约资源,前台只需要ddlCodeddlName,所以就设置includeProperties的parm)

public String findJctUnit(){
String jctID=elecUser.getJctID();
List<ElecSystemDDL> list=iElecSystemDDLService.findSystemDDLListByKeyword(jctID);
ValueStackUtils.setValueStatck(list);
return "findJctUnit";
}

public String checkUser(){
String LogonName=elecUser.getLogonName();

String message=iElecUserService.findUserByLogonName(LogonName);

return "checkUser";
}


3   前台JQuery function    业务场景(一个option下拉选择A城市,,另一个option下拉出来A城市对应的子细节)

 function findJctUnit(o){
    //货物所属单位的文本内容
    var jct = $(o).find("option:selected").text();
    $.post("elecUserAction_findJctUnit.do",{"jctID":jct},function(data,textStatus){
     //先删除单位名称的下拉菜单,但是请选择要留下
      // alert(data);
     $("#jctUnitID option").remove();
       if(data!=null && data.length>0){
           for(var i=0;i<data.length;i++){
        var ddlCode = data[i].ddlCode;
        var ddlName = data[i].ddlName;
        //添加到单位名称的下拉菜单中
        var $option = $("<option></option>");
        $option.attr("value",ddlCode);
        $option.text(ddlName);
        $("#jctUnitID").append($option);
         }
       }
        });
   
    }
    
    /**校验登录名是否出现重复*/
    function checkUser(o){
    //alert(o.value);//dom的写法
    //alert($(o).val());//jquery的写法
    var logonName = $(o).val();
    //以登录名作为查询条件,查询该登录名是否在数据库表中存在记录
    //?????????为什么不直接用userName
    $.post("elecUserAction_checkUser.do",{"logonName":logonName},function(data){
    if(data==1){
$("#check").html("<font color='red'>登录名不能为空</font>");
$(o)[0].focus();
$("#BT_Submit").attr("disabled","none");
}
else if(data==2){
$("#check").html("<font color='red'>登录名已经存在</font>");
$(o)[0].focus();
$("#BT_Submit").attr("disabled","none");
}
else{
$("#check").html("<font color='green'>登录名可以使用</font>");
$("#BT_Submit").attr("disabled","");
}
    });
    }



百度找了下也没找到struts.xml配置includeProperties的緣由。下面是JSONResult的源码,可以看到了List<Pattern> includeProperties;这大概就类似一种正泽表达式过滤。并且在JSONUtil.serialize的方法有 includeProperties等参数。


public class JSONResult implements Result {


    private static final long serialVersionUID = 8624350183189931165L;


    private static final Logger LOG = LoggerFactory.getLogger(JSONResult.class);


    private String defaultEncoding = "ISO-8859-1";
    private List<Pattern> includeProperties;
    private List<Pattern> excludeProperties


        protected String createJSONString(HttpServletRequest request, Object rootObject) throws JSONException {
        String json = JSONUtil.serialize(rootObject, excludeProperties, includeProperties, ignoreHierarchy, enumAsBean, excludeNullProperties);
        json = addCallbackIfApplicable(request, json);
        return json;
    }

-----

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
搭建项目自动化框架需要遵循以下步骤: 1. 安装 Docker 和 Jenkins:Docker是一个流行的容器化平台,Jenkins是一个流行的持续集成/持续交付工具。安装这两个工具可以帮助我们快速部署和管理应用程序。 2. 创建 Docker 镜像:创建一个 Dockerfile,定义我们的应用程序的环境和依赖项。然后使用 Docker 命令构建我们的镜像。 3. 设置 Git 仓库:将代码托管到 Git 仓库中,以便我们可以在 Jenkins 中自动拉取代码并构建我们的应用程序。 4. 配置 Jenkins:在 Jenkins 中创建一个新的任务,以便我们可以拉取代码、构建镜像、运行测试和生成测试报告。我们可以使用 Jenkinsfile 来定义构建任务的管道。 5. 集成 Pytest:Pytest是一个流行的 Python 测试框架,我们可以在 Jenkins 中使用 Pytest 运行我们的测试用例。 6. 集成 Allure:Allure是一个流行的测试报告框架,它可以帮助我们生成漂亮的测试报告。我们可以在 Jenkins 中使用 Allure 插件来生成测试报告。 以下是更详细的步骤: 1. 安装 Docker 和 Jenkins 首先,我们需要安装 Docker 和 Jenkins。可以在官方网站上找到安装指南。 2. 创建 Docker 镜像 在项目根目录下创建一个 Dockerfile 文件,用于定义我们的应用程序环境和依赖项。例如,如果我们的应用程序需要 Python 3.8,我们可以在 Dockerfile 中添加以下内容: ``` FROM python:3.8 WORKDIR /app COPY requirements.txt . RUN pip install -r requirements.txt COPY . . CMD [ "python", "app.py" ] ``` 然后使用以下命令构建 Docker 镜像: ``` docker build -t myapp . ``` 这将构建一个名为“myapp”的 Docker 镜像。 3. 设置 Git 仓库 将代码托管到 Git 仓库中,以便我们可以在 Jenkins 中自动拉取代码并构建我们的应用程序。可以使用 Git 命令行或 Git 客户端将代码推送到 Git 仓库。 4. 配置 Jenkins 在 Jenkins 中创建一个新的任务,以便我们可以拉取代码、构建镜像、运行测试和生成测试报告。我们可以使用 Jenkinsfile 来定义构建任务的管道。 以下是一个简单的 Jenkinsfile 示例: ``` pipeline { agent any stages { stage('Build') { steps { sh 'docker build -t myapp .' } } stage('Test') { steps { sh 'pytest tests --alluredir=./allure-results' } } stage('Report') { steps { allure includeProperties: false, jdk: '', properties: [], reportBuildPolicy: 'ALWAYS', results: [[path: 'allure-results']] } } } } ``` 这个管道定义了三个阶段:构建、测试和报告。在构建阶段中,我们使用 Docker 构建我们的镜像。在测试阶段中,我们使用 Pytest 运行我们的测试用例,并将测试结果保存在 allure-results 目录中。最后,在报告阶段中,我们使用 Allure 插件生成测试报告。 5. 集成 Pytest 在项目中添加测试用例,使用 Pytest 运行测试用例。可以使用以下命令运行测试: ``` pytest tests ``` 6. 集成 Allure 使用 Allure 生成漂亮的测试报告。可以使用以下命令生成测试报告: ``` allure serve allure-results ``` 可以在 Jenkins 中使用 Allure 插件来生成测试报告。在 Jenkins 中安装 Allure 插件后,只需在 Jenkinsfile 中添加以下代码即可: ``` allure includeProperties: false, jdk: '', properties: [], reportBuildPolicy: 'ALWAYS', results: [[path: 'allure-results']] ``` 这将在 Jenkins 构建完成后自动生成测试报告。 以上就是从0到1使用 Docker + Jenkins + Git + Pytest + Allure 搭建项目自动化框架的步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值