我有时会得到错误的错误:
Error setting expression X with value [Ljava.lang.String;@......]
有时会发生,有时不会发生。这很安静很烦人。错误有时来自这个类:
package com.coreRestaurant.menuItem;
import com.google.gson.Gson;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class MenuItemAction extends ActionSupport implements ModelDriven<MenuItem>{
private MenuItem menuItem = new MenuItem();
private final String DATABASE_PROBLEMS = "Error code : Database problems";
private String json;
public String execute(){
MenuItemService itemService = new MenuItemService();
if(itemService.testDatabaseConnectionU()){
setJson(new Gson().toJson(itemService.getMenuItemsByMenuId(menuItem.getMenuId()) ));
}else{
setErrorToBeSentBackToClientSide(DATABASE_PROBLEMS);
}
return SUCCESS;
}
@Override
public MenuItem getModel() {
return menuItem;
}
public String getJson() {
return json;
}
public void setJson(String json) {
this.json = json;
}
private void setErrorToBeSentBackToClientSide(String error){
String errorCode = error;
setJson(new Gson().toJson(errorCode));
}
}
然后MenuItem
上课:
package com.coreRestaurant.menuItem;
public class MenuItem {
private String name;
private double price;
private int menuId;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getMenuId() {
return menuId;
}
public void setMenuId(int menuId) {
this.menuId = menuId;
}
public void setMenuId(MenuItem fromJson) {
setName(fromJson.getName());
setPrice(fromJson.getPrice());
}
}
我从PHP文件中调用这些东西(两行都是这样做的,但第二行是担心的):
$specificMenuJson = file_get_contents('http://localhost:8080/Core/read?id=' . $menuId);
$specificMenuItemsJson = file_get_contents('http://localhost:8080/Core/readMenuItemById?menuId=' . $menuId);
设置menuId
的值时出现问题,但我包含了getter和setter。我不知道为什么有时候不这样做。有什么建议吗?
答案 0 :(得分:2)
尝试更改方法setMenuId
public void fromJsonMenuId(MenuItem fromJson) {
setName(fromJson.getName());
setPrice(fromJson.getPrice());
}
答案 1 :(得分:0)
问题是:
当modeldriven Interceptor调用setMenuId(int menuId)方法时,它将String值传递给int menuId参数。由于Interceptor从String到int
没有隐式转换