Java 在spring mvc中烘焙表单以进行编辑
完全是初学者到SpringMVC。 我需要在springmvc框架中预填充编辑表单 联系_edit.jspJava 在spring mvc中烘焙表单以进行编辑,java,spring,jakarta-ee,spring-mvc,Java,Spring,Jakarta Ee,Spring Mvc,完全是初学者到SpringMVC。 我需要在springmvc框架中预填充编辑表单 联系_edit.jsp <form:form method="post" action="edit/1" commandName="contact"> <% Contact contact = (Contact)request.getAttribute("contact"); %> <table> <tr> <
<form:form method="post" action="edit/1" commandName="contact">
<% Contact contact = (Contact)request.getAttribute("contact"); %>
<table>
<tr>
<td><form:label path="firstname">
<spring:message code="label.firstname" />
</form:label></td>
<td><form:input path="firstname" value="<%=contact.getFirstname() %>"/></td>
</tr>
<tr>
<td><form:label path="lastname">
<spring:message code="label.lastname" />
</form:label></td>
<td><form:input path="lastname" value="<%=contact.getLastname() %>"/></td>
</tr>
<tr>
<td><form:label path="email">
<spring:message code="label.email" />
</form:label></td>
<td><form:input path="email" value="<%=contact.getEmail()%>" /></td>
</tr>
<tr>
<td><form:label path="telephone">
<spring:message code="label.telephone" />
</form:label></td>
<td><form:input path="telephone" value="<%=contact.getTelephone()%>"/></td>
</tr>
<tr>
<td colspan="2"><input type="submit"
value="<spring:message code="label.addcontact"/>" /></td>
</tr>
</table>
</form:form>
我的联系人类包含所有getter和setter方法,甚至我的create表单也可以正常工作。不知道是什么在哪里造成了问题。我已经在请求以及ModelAndView类中插入了联系人对象 我认为问题在于你的模型课。您的模型中是否有用于
firstname
的getter/setter方法?如果是,请为我们添加代码以了解更多信息您不需要显式调用getter。由于您使用的是spring表单标记,spring将自动填充表单
编辑:
请试着换这条线
Contact contact = contactService.getContact(contactId);
与
只需找出问题的根源代码中需要纠正的几件事: 1) 当您将域对象添加到模型中时,SpringMVC使它们可用于JSP。您不需要在请求中再次设置域对象。 因此,从控制器中删除以下内容
request.setAttribute(“contact”,contact)代码>
同样,也不需要在jsp中引用请求对象。所以移除
Contact Contact=(Contact)request.getAttribute(“Contact”)代码>
2) 当您使用spring表单标记的“commandName”属性设置表单支持域对象时,spring使用域对象的属性来设置/获取表单元素的值
例如,在下面的代码中,spring将在填充/提交表单时对contact的firstname属性使用getter/setter
<form:form method="post" action="edit/1" commandName="contact">
<form:input path="firstname"/>
</form>
只需确保为域/表单烘焙对象中需要使用的所有属性实现了getter/setter。我通过在applicationContext.xml而不是spring-servlet.xml中初始化sessionFactory解决了这个问题。请确保将其从spring servlet.xml中删除。您可以发布您的联系人模型类吗?我已经添加了联系人类。如果还需要什么,请告诉我。我重复一遍,getter和setter是完美的,因为create正在工作,finegetter已经在那里了…我已经编辑了我的帖子。。你能不能请检查者已经在那里了…我已经编辑了我的帖子。。请您检查一下,正如我在回复中提到的,从controller和JSP中删除不需要的代码(设置和读取请求中的对象),然后重试。如果这不起作用,请发布您的创建联系人控制器MethodGetter已经存在…我已经编辑了我的帖子。。你能查一下吗
@Entity
@Table(name="CONTACTS")
public class Contact {
@Id
@Column(name="ID")
@GeneratedValue
private Integer id;
@Column(name="FIRSTNAME")
private String firstname;
@Column(name="LASTNAME")
private String lastname;
@Column(name="EMAIL")
private String email;
@Column(name="TELEPHONE")
private String telephone;
public String getEmail() {
return email;
}
public String getTelephone() {
return telephone;
}
public void setEmail(String email) {
this.email = email;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getFirstname() {
return firstname;
}
public String getLastname() {
return lastname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
@RequestMapping(value = "/edit/{contactId}", method = RequestMethod.GET)
public ModelAndView edit(@PathVariable("contactId") Integer contactId) {
System.out.println("I was here");
Contact contact = contactService.getContact(contactId);
return new ModelAndView("contact_edit","contact", contact);
}
Contact contact = contactService.getContact(contactId);
Contact contact = new Contact();
<form:form method="post" action="edit/1" commandName="contact">
<form:input path="firstname"/>
</form>