首页 新闻 会员 周边 捐助

JSON传递的问题

0
悬赏园豆:20 [已关闭问题] 关闭于 2018-01-31 16:11

java代码

@Controller
@RequestMapping("/plan/")
public class PlanController {

    //private ProjectTypeService projectTypeService;
//    @Resource
//    private WorkProgram workProgram;
    //进入工作计划表主页面
    @RequestMapping("listUI")
    public String listUI() {
        System.out.println("进入plan.listUI");
        return "table2";
    }
    //处理点击提交之后的请求
    
    
    @ResponseBody
    @RequestMapping("disposeSubmit")
    public JsonResult dispose(WorkProgram params) {
        WorkProgram w = new WorkProgram();
        PhasePlan p = w.phasePlan();
        System.out.println(params);
        System.out.println("进入/plan/disposeSubmit");
        String phasePlan = params.getPhasePlan();
        String[] phasePlans = phasePlan.split("##");
        for(int i=0;i<phasePlans.length;i++) {
            String str[] = phasePlans[i].split("&&");
            p.setSequence(i+1);
            p.setProgramContent(str[1]);
            p.setFinishTime(str[2]);
            p.setMember(str[3]);
            System.out.println("p="+p);
        }
        return new JsonResult("请求完成");
    }
}

javascript代码

    function doPlan(){
        var url = "plan/disposeSubmit.oo";
        var params=getEditFormData();
        if(params == "exception") return;
        console.log("JSON为:    "+JSON.stringify(params));
        $.post(url,params,function(result){
            console.log("回调函数执行了");
            console.log(result);
            //console.log("params="+params);
            //console.log(result);
            alert(result);
        })
    }
问题补充:

返回类型JsonResult代码

public class JsonResult implements Serializable{
   /**
     * 
     */
    private static final long serialVersionUID = -1780875855049989475L;
   public static final int SUCCESS=1;
   public static final int ERROR=0;
   /**状态:(SUCCESS,ERROR)*/
   private int state;
   /**状态信息*/
   private String message;
   /**具体数据*/
   private Object data;
   public JsonResult(){
       state=SUCCESS;
   }
   public JsonResult(String message){
       this();
       this.message=message;
   }
   public JsonResult(Object data){
       this();
       this.data=data;
   }
   public JsonResult(Throwable exp){
       state=ERROR;
       this.message=exp.getMessage();
   }
    public int getState() {
        return state;
    }
    public String getMessage() {
        return message;
    }
    public Object getData() {
        return data;
    }
}

 

spring-xml文件

<?xml version="1.0" encoding="UTF-8"?> <beans default-lazy-init="true" xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd" >
<!-- spring 包的扫描
1)dao 2)service (@Service)
3)controller(@Controller)
4)..... -->
<context:component-scan base-package="com.yhmp" />
<!-- spring mvc 注解及类型转换 -->
<mvc:annotation-driven conversion-service="conversionService" />
  <bean id="conversionService"                            class="org.springframework.format.support.FormattingConversionServiceFactoryBean"></bean>
<!-- spring mvc 视图解析器 -->
<!-- 定义跳转的文件的前后缀 ,视图模式配置 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">   <property name="prefix" value="/WEB-INF/pages/" />
  <property name="suffix" value=".jsp"></property>
</bean>

启动tomcat报错信息

删除spring-xml文件的这个内容后

<!-- spring mvc 注解及类型转换 -->
<mvc:annotation-driven conversion-service="conversionService" />
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> </bean>

tomcat启动不会报错,但是点击网页上按钮提交后提示状态码406

勤劳的杯子的主页 勤劳的杯子 | 初学一级 | 园豆:178
提问于:2018-01-30 13:18
< >
分享
所有回答(3)
0

@RequestMapping("disposeSubmit")

@ResponseBody 

这样试一下

ckx0709 | 园豆:414 (菜鸟二级) | 2018-01-30 17:03
0

你的拦截后缀是.oo?

ajax请求的时候,指定为application/json试试

王小帅 | 园豆:233 (菜鸟二级) | 2018-01-30 17:07
0

以解决,加入json三个配置文件,并在spring.xml内加入配置

勤劳的杯子 | 园豆:178 (初学一级) | 2018-01-31 16:10
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册