Swing 和 Dojo 很新,我有一个非常简单的开/关切换表单,带有 2 个单选按钮,我无法将值传递给我的 Java Swing 控制器,然后将其保存在模型中。
我不会详细介绍模型,你应该从我的代码中得到 jist,它非常简单直接。
索引.jsp
<form:form name="ToggleForm" commandName="ToggleForm" method="POST" action="ToggleForm.do" dojoType="dijit.form.Form" id="ToggleForm">
<input type="radio" dojoType="dijit.form.RadioButton" name="Toggle" id="toggleOn" value="1"> On
<input type="radio" dojoType="dijit.form.RadioButton" name="Toggle" id="toggleOff" value="0"> Off
</form:form>
<script>
dojo.addOnLoad(function() {
// Required includes
dojo.require("dijit.form.RadioButton");
});
function updateToggleValue(){
dojo.connect(dojo.byId(this.ToggleForm), 'onchange' ,function(){
// Find new checked item
var emailToggleOpts = document.getElementsByName('Toggle');
for (var i = 0, length = ToggleOpts.length; i < length; i++) {
if (ToggleOpts[i].checked) {
var ToggleChecked = ToggleOpts[i].value;
break;
}
}
// Set updated value
dojo.byId(this.updateToggle).setAttribute('ToggleValue', ToggleChecked);
dojo.xhrPost({
form: "updateToggle"
, timeout: 3000
, load: function(response){
console.log(response);
}
, error: function(error){
console.log(error);
}
});
dojo.disconnect(dijit.byId(self.form));
});
}
dojo.ready(updateToggleValue);
</script>
ToggleFormController.java
@Resource
ToggleOptService toggleOptService;
@RequestMapping(value = "/Toggle.do", method = RequestMethod.POST)
public @ResponseBody String updateToggle(
ModelMap model
, @ModelAttribute ToggleForm toggleForm
, BindingResult result) {
// 1. Get value of radio button from form
// This is the bit I'm having trouble with
// 2. Update value in model with value from form
ToggleOpt toggleOpt = new ToggleOpt();
toggleOpt.setToggleName("anOption");
toggleOpt.setToggleSetting(****form value****)
toggleOptService.updateToggleOpt(toggleOpt);
return "";
}
ToggleForm.java
public class ToggleForm {
private long id;
private String toggleName;
private int toggleSetting;
public long getId(){ return id; }
public void setId(long Id) { this.Id = Id; }
public String getToggleName() { return toggleName; }
public void setToggleName(String toggleName) { this.toggleName = toggleName; }
public int getToggleSetting() { return toggleSetting; }
public void setToggleSetting(int setting) { this.setting = setting; }
}