编辑页面数据初始化时,一条数据中的某个字段,java后台怎么返回给前端数组
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了编辑页面数据初始化时,一条数据中的某个字段,java后台怎么返回给前端数组相关的知识,希望对你有一定的参考价值。
/** * 获取数据 */ @Override public StuInfo getStuById(Integer id) StuInfo info = stuInfoDao.getStuById(id); return info; info中有个字段A的值是这样的 A=1,2,3,4我想让A的值返回给前端是这样的 "A":["1","2","3","4"]跟下图字段sites的值一样,求大佬帮忙~~~~~~~~~
spring方式
import lombok.extern.log4j.Log4j2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.*;
@SpringBootApplication
@Log4j2
@RestController
public class LearnConcurrencyApplication
public static void main(String[] args)
SpringApplication.run(LearnConcurrencyApplication.class, args);
@GetMapping("/")
public String test()
log.warn("Receive request.");
return "test";
@GetMapping("/a")
public Map<String,Object> getA()
StudentInfo info = new StudentInfo();
//方式1,假设字段a是字符串
// info.setA("1,2,3,4,5,6");
// Map<String,Object> result = new HashMap<>();
// result.put("A",Arrays.asList(info.getA().split(",")));
// return result;
//假设A是list
List<String> list = Arrays.asList("1,2,3,4,5,6".split(","));
info.setA1(list);
Map<String,Object> result = new HashMap<>();
result.put("A",info.getA1());
return result;
@Bean
public ServletRegistrationBean servletRegistration()
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
servletRegistrationBean.addUrlMappings("/sa");
servletRegistrationBean.setServlet(new MyServlet());
return servletRegistrationBean;
servlet方式,注意依赖jackson,jar包
package com.learn;import com.fasterxml.jackson.databind.ObjectMapper;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author lixiaosuo
* @since 2019-04-12 10:05
*/
public class MyServlet extends HttpServlet
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
StudentInfo info = new StudentInfo();
List<String> list = Arrays.asList("1,2,3,4,5,6".split(","));
info.setA1(list);
Map<String,Object> result = new HashMap<>();
result.put("A",info.getA1());
//依赖jackson
ObjectMapper mapper = new ObjectMapper();
String json =mapper.writeValueAsString(result);
resp.setContentType("application/json;charset=utf-8");
resp.setCharacterEncoding("utf-8");
resp.getWriter().write(json);
resp.getWriter().flush();
resp.getWriter().close();
辅助类
package com.learn;import java.util.List;
/**
* @author lixiaosuo
* @since 2019-04-12 09:46
*/
public class StudentInfo
public String a;
public List<String> a1;
public String getA()
return a;
public void setA(String a)
this.a = a;
public List<String> getA1()
return a1;
public void setA1(List<String> a1)
this.a1 = a1;
spring实现,输出结果:
servlet,实现输出结果
参考技术A按你的描述,A是用逗号隔开的字符串,应该先用split方法转成字符串数组,然后放在JSON里,直接toString返回前端即可
追问那怎么把obj.get("a")的值放到StuInfo的b字段里呢,返回是个StuInfo
追答b字段的格式应该是数组吧
info.setB(obj.get("a"))
StuInfo类中有个属性A,类型是List<String>
private List<String> A;以上是关于编辑页面数据初始化时,一条数据中的某个字段,java后台怎么返回给前端数组的主要内容,如果未能解决你的问题,请参考以下文章
django modelss.py里定义数据时,autofield定义时如何指定初始值,比如让某个字段从9995开始?