我有这样的java类:
public class Action{
private String title;
private String description;
private ActionState actionState;
public Action(){};
public Action(String title, String description, ActionState actionState)
this.title = title;
this.description = description;
this.actionState = actionState;
}
public String getTitle() {
return title;
}
public String getDescription() {
return description;
}
public ActionState getActionState() {
return actionState;
}
}
和控制器:
@RequestMapping(value = "/new/addAction", method = RequestMethod.POST)
public @ResponseBody void addAction(@RequestBody Action action){
actionService.add(action);
}
@RequestMapping(value = "/new/actionStates", method = RequestMethod.GET)
@ResponseBody
public List<String> returnActionsStates(){
return actionService.enumToStringList(ActionState.class);
}
首先负责将对象写入DB,然后将我的ENUMS名称发送给角度控制器
枚举类看起来像这样:
public enum ActionState {
CREATED,
OPENED,
CLOSED
}
这里是角度控制器
app.controller('actionController',function ($scope, $http) {
$scope.addAction = function(action){
$scope.action = {};
$http.post('actions/new/addAction', action).success(function(){
$scope.action.title='';
$scope.action.description='';
})
}
$scope.getActionStates = function() {
$http.get('actions/new/actionStates').success(function(actionTypesState){
$scope.states = actionTypesState;
})
}
最后是html表单
<b>Ttitle</b><input type="text" data-ng-model="action.title">
<br>
<b>Description</b><input type="text" data-ng-model="action.description">
<div data-ng-init="getActionStates()">
<b>Action State</b> <select id="actionState">
<option value="">-- choose--</option>
<option data-ng-repeat="state in states" value="{{state}}">{{state}}</option>
</select><br>
</div>
<br>
<button data-ng-click="addAction(action)">submit</button>
它的标题和描述。我的意思是Action
控制器中的addAction
对象的标题值等于我用html格式输入的标题值,其描述相同。
问题是:如何使其与我的enum
值一样有效?
我知道我可以发送带有枚举名称的String
,然后使用valueOf
方法在控制器中创建它。但我希望自动创建整个对象:)。
我会感激任何帮助。
答案 0 :(得分:0)
如果有人想知道,Spring会自动将JSON转换为ENUM。 这里的问题是html代码,它根本没有发送JSON。 使用以下代码
<div data-ng-init="getActionStates()">
<b>Action State</b> <select data-ng-model="action.actionState" data-ng-options="actionState for (id, actionState) in states">
<option value="">{{neutralActionState}}</option>
</select>
</div>