1. 程式人生 > >向controller傳一個類,類中有一個list,400,415,500

向controller傳一個類,類中有一個list,400,415,500

報錯有可能是json格式不對,controller沒有@RequestBody,ajax沒有
contentType: 'application/json;charset=UTF-8',
data: JSON.stringify(datas),//data沒有JSON.stringify()

框架沒有json轉換配置

<!-- json轉換器 這是spring的配置-->

	<bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
		<property name="supportedMediaTypes">
			<list>
				<value>application/json;charset=UTF-8</value>
				<value>text/html;charset=UTF-8</value>
			</list>
		</property>
		<property name="features">
			<util:list>
				<value>QuoteFieldNames</value>
				<value>WriteDateUseDateFormat</value>
				<value>DisableCircularReferenceDetect </value>
				<value>WriteMapNullValue</value>
				<value>WriteNullListAsEmpty</value>
				<value>WriteNullStringAsEmpty</value>
				<value>WriteNullNumberAsZero</value>
				<value>WriteNullBooleanAsFalse</value>
			</util:list>
		</property>
	</bean>
	<bean
			class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="messageConverters">
			<list>
				<ref bean="fastJsonHttpMessageConverter" />    <!-- JSON轉換器 -->
			</list>
		</property>
	</bean>

js

function add() {
        var goodspes =[]
        var wwslength = $(".wws").length;

        $.each($(".wws"),function (i,w) {
//            alert($(w).children(".gg").find("input[name='goods_tp']").val()+$(w).children(".gg").find("input[name='goods_tp']").attr("name"));
//            alert($(w).children(".jg").find("input[name='goods_price']").val()+$(w).children(".jg").find("input[name='goods_price']").attr("name"));
            var goods_tp = $(w).children(".gg").find("input[name='goods_tp']").val();
            var goods_price = $(w).children(".jg").find("input[name='goods_price']").val();

            goodspes.push({"goods_tp":goods_tp,"goods_price":goods_price});//拼接集合

        })


        console.info(JSON.stringify(goodspes));

        var goods_name = $('#goods_name').val();
        var ctg_id = $('#ctg_id').val();
        var goods_spe = $('#goods_spe').val();
        var goods_img = $('#filepath').val();
        var goods_img_1 = $('#filepath1').val();
        var goods_img_2 = $('#filepath2').val();
        var goods_price = $('#goods_price').val();
        var cost_price = $('#goods_cost_price').val();
        var goods_detail = $('#goods_detail').val();
        var end_time = $('#end_time').val();
        var type = $('#type option:selected').val();
        var is_by = $('#is_by option:selected').val();

        var goods_old_price = $('#goods_old_price').val();
        var datas={"goods_name":goods_name,"ctg_id":ctg_id,"goods_img":goods_img,
        "goods_img_1":goods_img_1,"goods_img_1":goods_img_1,"goods_img_2":goods_img_2,"goods_spe":goods_spe,
            "goods_price":goods_price,"goods_old_price":goods_old_price,"cost_price":cost_price,"end_time":end_time,"type2":type,"is_by":is_by,"goods_detail":goods_detail,"goodspes":goodspes
        }
        console.info((datas));
        $.ajax({
            contentType: 'application/json;charset=UTF-8',
url: 'goodsInsert1.html', type: 'post', dataType: "json", data: JSON.stringify(datas), success: function (rs) { if (rs == 1) { alert("新增成功!"); window.location.href = document.referrer; } else { alert("新增失敗!"); } } }) }
controller
@ResponseBody
	@RequestMapping(value = "/main/1.html", method = RequestMethod.POST)
	public String insert(@RequestBody Goods goods) throws UnsupportedEncodingException {

entity


 
public class Goods extends Page{
   Integer goods_id;
   String goods_name;
   String goods_img;
   String goods_img_1;
   String goods_img_2;
   float goods_price;
   float goods_old_price;
   String goods_detail;
   String add_time;
   Integer ctg_id;
   Integer status;
   Integer is_recommend;
   String goods_spe;
   Integer type;
   Integer goods_num;
   Integer totals;
   String end_time;
   Integer type2;
   Integer is_by;
   List<Goodspe> goodspes;