Json 根据在表a上单击的行填充表B
我有一个由dojo制作的表,我需要填充另一个表 表B再次使用dojo。在这里,我需要单击表A的行 根据这一行,我需要调用我的spring控制器 同时,我将把行id或行的某些值发送给控制器 控制器应该返回我需要的模型的json数据 返回dojo,使其成为表B 下面我向你展示我所拥有的 按钮填充我在谷歌搜索中得到的表Json 根据在表a上单击的行填充表B,json,spring-mvc,dojo,spring-roo,dojox.grid.datagrid,Json,Spring Mvc,Dojo,Spring Roo,Dojox.grid.datagrid,我有一个由dojo制作的表,我需要填充另一个表 表B再次使用dojo。在这里,我需要单击表A的行 根据这一行,我需要调用我的spring控制器 同时,我将把行id或行的某些值发送给控制器 控制器应该返回我需要的模型的json数据 返回dojo,使其成为表B 下面我向你展示我所拥有的 按钮填充我在谷歌搜索中得到的表 <button data-dojo-type="dijit/form/Button" type="button" id="goBtn" disabled="disabled"
<button data-dojo-type="dijit/form/Button" type="button" id="goBtn" disabled="disabled" >GO
<script type="dojo/method" data-dojo-event="onClick" data-dojo-args="evt">
populateTable();
</script>
我尝试了这个内部函数populateTableB,而不是for循环,它在脚本中,我们可以注意到它
下面我给你一个弹簧控制器
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.model.TableBBean;
@Controller
@RequestMapping("/populateFromSpring")
public class PopulateFromSpringCtrl {
@RequestMapping(method = RequestMethod.POST)
public @ResponseBody List<TableBBean> tableBmth(@RequestParam String name) {
// ModelAndView mv = new ModelAndView("loginsuccess");
List<TableBBean> Beans = new ArrayList<TableBBean>();
TableBBean B1 = new TableBBean();
B1.setDay(1);
B1.setDescription("Desc 1");
B1.setEvent("GS");
B1.setCheckBox("0");
TableBBean B2 = new TableBBean();
B2.setDay(2);
B2.setDescription("Desc 2");
B2.setEvent("GS");
B2.setCheckBox("1");
TableBBean B3 = new TableBBean();
B3.setDay(3);
B3.setDescription("Desc 3");
B3.setEvent("GS");
B3.setCheckBox("1");
Beans.add(B1);
Beans.add(B2);
Beans.add(B3);
//mv.addObject("Beans",Beans);
return Beans;
}
}
import java.util.ArrayList;
导入java.util.List;
导入org.springframework.stereotype.Controller;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RequestMethod;
导入org.springframework.web.bind.annotation.RequestParam;
导入org.springframework.web.bind.annotation.ResponseBody;
导入com.model.TableBBean;
@控制器
@请求映射(“/populateFromSpring”)
公共类PopulateFromSpringCtrl{
@RequestMapping(method=RequestMethod.POST)
public@ResponseBody List tableBmth(@RequestParam String name){
//ModelAndView mv=新的ModelAndView(“登录成功”);
listbeans=newarraylist();
TableBBean B1=新的TableBBean();
B1.设定日(1);
B1.设置说明(“说明1”);
B1.setEvent(“GS”);
B1.设置复选框(“0”);
TableBBean B2=新的TableBBean();
B2.设定日(2);
B2.设置说明(“说明2”);
B2.setEvent(“GS”);
B2.设置复选框(“1”);
TableBBean B3=新TableBBean();
B3.设定日(3);
B3.设置说明(“说明3”);
B3.setEvent(“GS”);
B3.设置复选框(“1”);
添加(B1);
添加(B2);
添加(B3);
//mv.addObject(“bean”,bean);
返豆;
}
}
我知道这个控制器还没有完全完成,我需要帮助完成这个控制器并将bean转换为JSON
所以我需要的是
dojo应该调用这个控制器并发送一些数据,比如id
将表A的行添加到它,然后它应该以JSON和
填充表B
要在Spring MVC中返回JSON,请确保您拥有Jackson库,并执行以下操作:
@RequestMapping("myHandler")
public @ResponseBody MyBean myHandlerToGetBean() {
// Do some stuff...
return myBean; // return instance of MyBean
}
Spring将看到对象被返回,MyBean
,并使用Jackson自动神奇地转换它。如果不需要太多花哨的东西,您可以使用Jackson注释来帮助控制MyBean
的序列化/封送方式。如果您需要提供更多控制的内容,可以放弃直接返回MyBean
,而是返回字符串并使用Jackson自定义序列化程序。您可能还需要设置内容类型
,以表示您正在返回JSON
在客户端,您可以使用诸如jQuery之类的东西对服务进行AJAX调用,并直接在JavaScript中使用returns对象(浏览器应将JSON转换为JavaScript对象,再次“自动神奇地”)。然后,您如何使用它取决于您的实现,但似乎您希望修改表的DOM或其他内容。您可能还需要使用某种表插件,如果是这样,您需要阅读文档,以了解JSON的确切格式以及如何进行更新。我不喜欢使用dojoe jQuery,但在我看来,获取
Beans
的代码行应该是获取一个数据对象,它将是一个基于控制器代码的JSON响应构建的JavaScript对象,并使用它来填充表B。我也喜欢jQuery,但现在我需要使用DOJO,我面临的问题是我无法使用它调用控制器。我看到的一个问题是,您显然在客户端使用GET,但您在控制器端的方法显然需要POST。@CodeChimp抱歉,我将在客户端将此更改为POST。在Spring MVC中,返回JSON就像包含Jackson库并返回对象而不是字符串一样简单。Spring将使用Jackson将对象序列化为JSON。如果您希望对生成的JSON有更多的控制,我相信Jackson有注释,您可以在返回的对象上使用注释来控制返回的字段,或者您可以让处理程序返回字符串,用@ResponseBody
对其进行注释,这些注释都是返回自定义JSON。如果我需要发送一个日期表单javascript,请告诉我如何发送。在xhrPost中,如何指定日期,以便控制器可以读取。您可以在控制器中使用init活页夹来指示它如何从文本转换为对象。谷歌搜索“Spring MVC InitBinder”,您应该可以找到大量示例。
<script>
require(["dojo/parser", "dijit/form/FilteringSelect", "dijit/form/Button", "dojox/data/HtmlTableStore", "dojox/grid/DataGrid"]);
require(["dojo/store/Memory", "dojo/data/ObjectStore", "dojox/grid/DataGrid", "dojo/_base/lang", "dojox/grid/DataGrid",
"dojo/data/ItemFileWriteStore", "dojox/grid/cells/dijit", "dojox/grid/cells/CheckBox", "dojo/dom", "dojo/domReady!"], function () {
layoutGridForTableA = [[
{ field: "nm", name: "Name", width: 'auto' },
{ field: "Cod", name: "Code", width: 'auto' },
{ field: "startDt", name: "Start Date", width: 'auto' },
{ field: "endDt", name: "End Date", width: 'auto' }
]];
layoutGridForTableB = [[
{ field: "day", name: "Day", width: 'auto' },
{ field: "description", name: "Description", width: 'auto' },
{ field: "event", name: "Event", width: 'auto' },
{ field: "checkBoxTest", name: "Check Box Test", width: 'auto', editable: true, type: dojox.grid.cells.Bool, formatter:formatCell, styles: 'text-align: center;' },
{ field: "", name: "", width: 'auto', formatter:editButton}
]];
storeDataForTableA = [];
storeDataForTableB = [];
var formatCell = function(){
var checked = val? 'checked="checked";' : '<input type="checbox"' +checked+'disabled="disabled"/>';
return checked;
};
function editButton(){
return "<button onclick=\"\" class=\"editbuttonicon\"></button>";
}
});
function populateTableA(){
var addItemToTableA = { name:'Steve', Cod:'007', startDt:'any date', endDt:'any date'};
for (var i=0;i<4;i++)
{
tableADateStoreForGrid.newItem(addItemToTableA );
}
}
function populateTableB(){
var addItemToTableA = { name:'Steve', Cod:'007', startDt:'any date', endDt:'any date'};
for (var i=0;i<4;i++)
{
tableBDateStoreForGrid.newItem(addItemToTableA );
}
}
</script>
dojo.ready(function(){
dojo.xhrGet({
url : "populateFromSpring",
handleAs: "json",
load: function(Beans) {
//I need to get the Beans object here and populate the Table B
alert("hi");
},
error: function(err) {
alert("err: "+err);
}
});
});
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.model.TableBBean;
@Controller
@RequestMapping("/populateFromSpring")
public class PopulateFromSpringCtrl {
@RequestMapping(method = RequestMethod.POST)
public @ResponseBody List<TableBBean> tableBmth(@RequestParam String name) {
// ModelAndView mv = new ModelAndView("loginsuccess");
List<TableBBean> Beans = new ArrayList<TableBBean>();
TableBBean B1 = new TableBBean();
B1.setDay(1);
B1.setDescription("Desc 1");
B1.setEvent("GS");
B1.setCheckBox("0");
TableBBean B2 = new TableBBean();
B2.setDay(2);
B2.setDescription("Desc 2");
B2.setEvent("GS");
B2.setCheckBox("1");
TableBBean B3 = new TableBBean();
B3.setDay(3);
B3.setDescription("Desc 3");
B3.setEvent("GS");
B3.setCheckBox("1");
Beans.add(B1);
Beans.add(B2);
Beans.add(B3);
//mv.addObject("Beans",Beans);
return Beans;
}
}
@RequestMapping("myHandler")
public @ResponseBody MyBean myHandlerToGetBean() {
// Do some stuff...
return myBean; // return instance of MyBean
}