鍍金池/ 問答/Java  HTML/ 在maven項(xiàng)目的servlet使用fastjson無法傳送數(shù)據(jù)給前端

在maven項(xiàng)目的servlet使用fastjson無法傳送數(shù)據(jù)給前端

package com.mvc.servlet;

import com.mvc.bean.Goods;
import com.mvc.bean.GoodsItem;
import com.mvc.dao.GoodsDao;
import com.mvc.impl.GoodsDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.JSONLibDataFormatSerializer;
import com.alibaba.fastjson.serializer.JSONSerializerMap;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.io.PrintWriter;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;

import com.mvc.bean.Student;

public class GetGoodsServlet  extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        //得到編號(hào)
        String goodsName = request.getParameter("goodsName");
        System.out.println(goodsName);

        String output = "后臺(tái)返回的結(jié)果加上前臺(tái)的結(jié)果";

        Student student = new Student(0, "Aaron", 24);
        System.out.println(JSON.toJSONString(student));
        response.getWriter().write(JSON.toJSONString(student));

    }
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    function ajax(){
        var ajaxData = {
            type:arguments[0].type || "GET",
            url:arguments[0].url || "",
            async:arguments[0].async || "true",
            data:arguments[0].data || null,
            dataType:arguments[0].dataType || "text",
            contentType:arguments[0].contentType || "application/x-www-form-urlencoded",
            beforeSend:arguments[0].beforeSend || function(){},
            success:arguments[0].success || function(){},
            error:arguments[0].error || function(){}
        }
        ajaxData.beforeSend()
        var xhr = createxmlHttpRequest();
        xhr.responseType=ajaxData.dataType;
        xhr.open(ajaxData.type,ajaxData.url,ajaxData.async);
        xhr.setRequestHeader("Content-Type",ajaxData.contentType);
        //xhr.send(convertData(ajaxData.data));
        xhr.send("goodsName="+"youda");
        xhr.onreadystatechange = function() {
            console.log(xhr.readyState)
            if (xhr.readyState == 4) {
                console.log(xhr.status)
                if(xhr.status == 200){
                    console.log(XMLHttpRequest);
                    ajaxData.success(xhr.response)
                }else{
                    ajaxData.error()
                }
            }
        }
    }

    function createxmlHttpRequest() {
        if (window.ActiveXObject) {
            return new ActiveXObject("Microsoft.XMLHTTP");
        } else if (window.XMLHttpRequest) {
            return new XMLHttpRequest();
        }
    }

    function convertData(data){
        if( typeof data === 'object' ){
            var convertResult = "" ;
            for(var c in data){
                convertResult+= c + "=" + data[c] + "&";
            }
            convertResult=convertResult.substring(0,convertResult.length-1)
            return convertResult;
        }else{
            return data;
        }
    }

    ajax({
        type:"POST",
        url:"getGoods",
        dataType:"json",
        data:{"val1":"abc","val2":123,"val3":"456"},
        beforeSend:function(){
            //some js code
        },
        success:function(msg){
            console.log(msg)
        },
        error:function(){
            console.log("error")
        }
    })
</script>
<div><button id="getGoods" value="顯示全部商品" onclick="getInfoByName()">點(diǎn)擊商品全部</button></div>
</body>
</html>
package com.mvc.bean;


public class Student {

    private int id;
    private String name;
    private int age;

    /**
     * 默認(rèn)的構(gòu)造方法必須不能省,不然不能解析
     */

    public Student(){

    }
    public Student(int id,String name,int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
    }

}

圖片描述

圖片描述

回答
編輯回答
醉淸風(fēng)

你這個(gè)問題和fastjson沒太大關(guān)系
1.你需要現(xiàn)保證后臺(tái)的數(shù)據(jù)能傳到前臺(tái),比如console.info(xxx)能打印出來
2.第一步搞定后,再把你的對(duì)象搞成你的業(yè)務(wù)數(shù)據(jù),就是那個(gè)json形式的字符串,剩下的就是前段的json數(shù)據(jù)處理了

加油,你行滴!

2017年9月7日 14:48
編輯回答
忘了我

異常信息顯示找不到FastJSON相關(guān)的類,檢查一下相關(guān)jar包是否已經(jīng)引入...

2017年10月11日 17:48
編輯回答
練命

根據(jù)發(fā)出來異常

1.首先要解決的是,你沒有成功引入fastJson包。rootCase提示NoClassDefFoundError, 可能是部署的時(shí)候沒打包成功。試下更新下你的依賴?
2018年9月9日 15:18