基于String值创建新对象实例

时间:2015-06-29 01:17:27

标签: java spring

我试图弄清楚是否有办法根据java中的用户输入动态实例化一个新对象。

E.g。

我有3个实体/对象

public class Item {}

public class Room {}

public class Location {}

在表单中,用户会填写一个名为" id =' objecttype'"它会被发送到一个控制器。 控制器将根据String值动态返回一个对象。

e.g。

如果用户在Item中键入,它将生成一个Item对象并将其返回。 如果用户键入Room,它将实例化一个Room对象并返回新的Room()对象,最后同样用于Location,其中Location和java中的用户类型返回一个新的Location()对象。

E.G。控制器

@RequestMapping(value = "/retVal", method = /*POST Type*/) 
public Object retVal(@RequestParameter("objecttype") String type) {
// here the logic would determine which object would get returned
// if type is Item
//    return new Item();
// if type is Room
//    return new Room();
// if type is Location
//    return new Location();

   return null;
}

0 个答案:

没有答案