【发布时间】:2011-05-05 08:31:18
【问题描述】:
我将尽可能完整、简短地解释我的问题...
一个基于 Spring MVC 2.5 + Hibernate + Java 6 的 Web 应用程序(不使用注解!)。
我有一个扩展 SimpleFormController 的控制器和一个 jsp 页面,它是它的 formView 和 successView。
这个控制器应该帮助我将实体 PracticeT 插入到数据库中,该实体已连接(多对一)查找实体 PracticeConfT(将其视为“类型”)。我需要通过下拉菜单选择“类型”。在我的 web 应用程序中,我需要能够保存插入的数据,并在需要时提交批准请求。
该页面有一些文本字段和下拉菜单。被称为默认“命令”的 bean 是 NewPracticeBean,它具有对对象 PracticeT 的引用。
问题是:我填写表格,从下拉菜单中选择一种类型,我提交表格并将数据保存在数据库中,但是当我回到视图时,除了下拉菜单之外,所有属性都在那里它不是:它具有所有允许的选项,但没有选择一个。一些检查显示实体 PracticeConfT 为空(但它已正确记录在 db 上,并且在模型中调试它仍然存在,直到方法 onSubmit 结束!!!)。
我希望有人可以帮助我。先感谢您! 再见, 多尔菲兹
这里有一些有用的代码: (我不认为休眠配置可能是问题,但如果你需要它,我也可以发布)
newPractice.jsp
<form:form id="newPracticeForm" commandName="command">
<input type="hidden" name="action"/>
<spring:nestedPath path="practiceT">
<table class="table-data-form">
<tr>
<td class="left"><spring:message code="" text="Practice type" /></td>
<td>
<form:select path="practiceConfT" multiple="false">
<form:option value="" label="- seleziona -"/>
<form:options items="${practiceTypeList}" itemValue="idPracticeConf" itemLabel="practiceName"/>
</form:select>
</td>
</tr>
<tr>
<td class="left">
<spring:message code="" text="Opzione divisa" />
<br/><form:errors cssClass="errors" path="opzioneDivisa" />
</td>
<td><form:input path="opzioneDivisa" /></td>
</tr>
<tr>
<td colspan="1">
<input type="submit" name="submit" id="submit" value="Save" class="buttonEMS" style="width:100px;" />
</td>
</tr>
</table>
</spring:nestedPath>
</form:form>
NewPracticeBean.java
public class NewPracticeBean implements Serializable{
private PracticeT practiceT;
private String action;
private boolean typeSelected;
public NewPracticeBean(){
super();
this.practiceT = new PracticeT();
}
// getters & setters...
}
PracticeT.java
public class PracticeT implements java.io.Serializable {
private long idPractice;
private PracticeConfT practiceConfT;
private String opzioneDivisa;
// getters & setters...
}
PracticeConfT.java
public class PracticeConfT implements java.io.Serializable {
public static final String PRACTICE_NAME = "practiceName";
private long idPracticeConf;
private String practiceName;
// getters & setters...
}
NewPracticeController.java 公共类 NewPracticeController 扩展 SimpleFormController{
protected SmartLogger log = SmartLogger.getLogger(this.getClass());
private PracticeSu practiceSu;
private ConfigurationSu configurationSu;
private HibernateEntityDataBinder practiceConfTBinder;
private HibernateEntityDataBinder practiceTBinder;
public NewPracticeController() {
setCommandClass(NewPracticeBean.class);
setCommandName("command");
}
@Override
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
log.trace("NewPracticeController -- initBinder");
super.initBinder(request, binder);
binder.registerCustomEditor(PracticeT.class, "practiceT", practiceTBinder);
binder.registerCustomEditor(PracticeConfT.class, "practiceT.practiceConfT", practiceConfTBinder);
}
@Override
protected Map referenceData(HttpServletRequest request) throws Exception {
log.trace("NewPracticeController -- referenceData");
Map model = new HashMap();
RetrieveAllEntitiesReq req = new RetrieveAllEntitiesReq();
req.setEntity(PracticeConfT.class);
req.setOrderProperty(PracticeConfT.PRACTICE_NAME);
RetrieveAllEntitiesResp resp = configurationSu.retrieveAllEntities(req);
List entitiesList = resp.getEntitiesList();
model.put("practiceTypeList", entitiesList);
return model;
}
@Override
protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception {
NewPracticeBean practiceBean = (NewPracticeBean)command;
Map model = errors.getModel();
CreateNewPracticeReq req = new CreateNewPracticeReq();
req.setPracticeT(practiceBean.getPracticeT());
CreateNewPracticeResp resp = practiceSu.createNewPractice(req);
practiceBean.setPracticeT(resp.getPracticeT());
model.putAll(referenceData(null));
model.put(getCommandName(), practiceBean);
return new ModelAndView(getSuccessView(), model);
}
// setters and getters...
}
【问题讨论】:
-
我想强调一点: - 在 jsp 页面中 PracticeConfT 为空 - 在返回语句之前的 onSubmit 方法中相同的 PracticeConfT 不为空
-
尝试在PracticeConfT中添加一个Equals Method,因为我猜是equals但不一样!
-
我应该使用 fromBackingObject 方法来检索 PracticeConfT 并在表单呈现之前将其设置在 PracticeT 中吗?看起来很奇怪......我猜绑定是自动的......我确定我错过了一些重要的东西......>:(
-
我不明白为什么平等会影响这一点。你是什么意思?问题是服务器上的 PracticeConfT 不为空,但在 jsp 页面上为空 >:S
-
@Dolfiz 我的想法是
“根据绑定值适当地设置 'selected' 属性。”基于equals方法。 (但现在重新考虑之后,我不再那么确定了。我稍后会检查这个,但现在我将解释 a 的含义)。如果实体:PracticeT.practiceConfT 不是 SAME 对象,如 `model.put("practiceTypeList",entitiesList)` 中的一次,并且没有实现正确的 equals 方法,则不会选择正确的对象。
标签: java model-view-controller spring binding controller