美文网首页S2SH在线答题
Json初体验(第七天)

Json初体验(第七天)

作者: setone | 来源:发表于2017-07-10 17:53 被阅读0次

第七天;

构思:
1.通过ajax把课程id(每个课程都有自己的唯一id)传到后台
2.后台拿到id进行查询并组装成list
3.把list装换成json数据
4.通过PrintWriter out = ServletActionContext.getResponse().getWriter();把数据传到前台,前台进行解析并判断出是否为多选题

文件结构图发生了变化

文件结构图

预览

今天要做出的效果

onepage

<script type="text/javascript">
      function showcontent(classid){
            $.ajax({
            type:"post",//请求方式
            url:"answer/subject_list.action",//请求路径
            data:{//传参
                classid:classid,
            },
            dataType:"json",//数据格式
            success : function(datajson) {
                   //清空显示层中的数据
                    $("#message").html("");
                    var arr = datajson;  
                    var radio = "radio";
                    $.each(arr, function(index, content)
                    { 
                      //单选题与多选题判断 
                      if (content.resulter.length>1){
                             radio = "checkbox";
                              }
                            else{
                             radio = "radio";
                              }; 
                    var obj = "<fieldset><ul><li><h3><xmp>"+(index+1)+"、"+content.subjecttext+"</xmp></h3></li><hr/><li><input name='"+content.subjectid+"' type='"+radio+"' value='A'/>A、"+content.subjecta+"</li><li><input name='"+content.subjectid+"' type='"+radio+"' value='B'/>B、"+content.subjectb+" </li><li><input name='"+content.subjectid+"' type='"+radio+"' value='C'/>C、"+content.subjectc+" </li><li><input name='"+content.subjectid+"' type='"+radio+"' value='D'/>D、"+content.subjectd+" </li><li><input  type='button' value='提交' onclick='checkResult("+content.subjectid+")'/><span id='"+content.subjectid+"'></span></li><ul></fieldset><br/>";                     
                     $("#message").append(obj);
                    });
                },
            error:function(datajson){ 
                      alert("error"); 
            },
           });  
        }; 
</script>

struts.xml

加上下面这个action

<action name="subject_list" class="subjectAction" method="list" />

applicationContext.xml

加上下面这个bean

<!-- utils -->
    <bean id="utils" class="com.jianshu.utils.Utils" scope="prototype" />

今天我们多出来的一个包,里面只有一个类

Utils

package com.jianshu.utils;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.struts2.ServletActionContext;

public class Utils {
      /**
     * 封装json共用,自动识别list,针对subject
     * @param object
     * @throws IOException
     */
    public void subjectJson(Object object) throws IOException {
        HttpServletResponse response = ServletActionContext.getResponse();
        /* 设置格式为text/json */
        response.setContentType("text/json");
        /* 设置字符集为'UTF-8' */
        response.setCharacterEncoding("UTF-8");

        PrintWriter out = ServletActionContext.getResponse().getWriter();

        JsonConfig config = new JsonConfig();
        config.setExcludes(new String[] { "allclass", "errorsubjects" });// 除去allclass,errorsubjects属性,否则不能组装成json
        String json;
        if(object instanceof List){
            json = JSONArray.fromObject(object, config).toString();//JSONArray对list的装换
        }else{
            json = JSONObject.fromObject(object, config).toString();// JSONObject单个对象的装换
        }
        // 强制刷新
        out.write(json);
        out.flush();
        out.close();
    }
}

同时根据struts.xml的变化我们的SubjectAction.java也发生了变化

SubjectAction.java

package com.jianshu.action;

import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;


import com.jianshu.service.SubjectService;
import com.jianshu.utils.Utils;

public class SubjectAction {
    @Autowired
    SubjectService subjectService;
    @Autowired
    Utils utils;
    List allSubject;
    Long classid;
    public Long getClassid() {
        return classid;
    }
    public void setClassid(Long classid) {
        this.classid = classid;
    }
    
    public List getAllSubject() {
        return allSubject;
    }
    public void setAllSubject(List allSubject) {
        this.allSubject = allSubject;
    }
    /**
     * 取出题目并填装到allSubject(List) 同时取出并组装成json
     * 
     * @return
     * @throws IOException
     */
    public void list() throws IOException {
        allSubject = subjectService.list(classid);
        utils.subjectJson(allSubject);
    }}

SubjectService.java

package com.jianshu.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.jianshu.dao.SubjectDAO;

public class SubjectService {
    @Autowired
    SubjectDAO subjectDAO;
    public List list(Long classid) {
        // TODO Auto-generated method stub
        return subjectDAO.findByclassid(classid);
    }

}

SubjectDAO.java

    /**
     * 根据classid查询该科目
     * @param classid
     * @return
     */
    public static final String CLASSID = "allclass.classid";
    public List findByclassid(Object classid) {
        return findByProperty(CLASSID, classid);
    }

相关文章

网友评论

    本文标题:Json初体验(第七天)

    本文链接:https://www.haomeiwen.com/subject/ztruhxtx.html