我是一名使用Spring的完全noob。
我正在尝试提交表单,但我收到以下错误:
ERROR: org.springframework.web.servlet.tags.form.InputTag - Neither BindingResult nor plain target object for bean name 'hitoForm' available as request attribute
以下是表格:
<form:form role="form" action="modifyHito" method="post" commandName="hitoForm">
<div class="form-group">
<label>Hito number</label>
<form:input class="form-control" path="hitoNumber" disabled="" type="text"/>
</div>
<div class="form-group">
<label>Title</label>
<form:input class="form-control" path="title" type="text" maxlength="50"/>
</div>
<div class="form-group">
<label>Subtitle</label>
<form:input class="form-control" path="subtitle" type="text" maxlength="200"/>
</div>
<div class="form-group">
<label>Date</label>
<form:input class="form-control" path="date" type="text" maxlength="50"/>
</div>
<div class="form-group">
<label>Latitude</label>
<form:input class="form-control" path="latitude" type="text" maxlength="15"/>
</div>
<div class="form-group">
<label>Longitude</label>
<form:input class="form-control" path="longitude" type="text" maxlength="15"/>
</div>
</form:form>
控制器:
@Controller
@RequestMapping(value = "/modifyHito")
public class ModifyHitoController {
@Autowired
private HitoManager hitoManager;
@RequestMapping(method = RequestMethod.GET)
public String viewModificationForm(Model model) {
Hito hitoForm = new Hito();
model.put("hitoForm", hitoForm);
return "main";
}
@RequestMapping(method = RequestMethod.POST)
public String processModification(@ModelAttribute("hitoForm") Hito hito, Model model) {
hitoManager.modifyHito(hito);
return "main";
}
}
我的代码有什么问题?我尝试了几件事,但我总是有同样的错误。 提前谢谢。
修改
这是我的实体Hito:
@Entity
@Table(name="hito")
public class Hito implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private int hitoNumber;
private String title;
private String subtitle;
private String date;
private double latitude;
private double longitude;
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getSubtitle() {
return subtitle;
}
public void setSubtitle(String subtitle) {
this.subtitle = subtitle;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
public Integer getHitoNumber() {
return hitoNumber;
}
public void setHitoNumber(Integer hitoNumber) {
this.hitoNumber = hitoNumber;
}
}
和web.xml:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<display-name>PintiaServer</display-name>
<servlet>
<servlet-name>pintiaserver</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/app-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>pintiaserver</servlet-name>
<url-pattern>*.htm</url-pattern>
<url-pattern>/pintiaserver/*</url-pattern>
</servlet-mapping>
</web-app>
答案 0 :(得分:1)
我认为您正在使用其他Model.class
。使用时应使用org.springframework.ui.Model
并使用model.addAttribute
代替model.put
。
您遇到上述错误,因为Spring无法找到与表单数据绑定的ModelAttribute。
试试这个:
@RequestMapping(method = RequestMethod.GET)
public String viewModificationForm(Model model) {
Hito hitoForm = new Hito();
model.addAttribute("hitoForm", hitoForm);
return "main";
}
答案 1 :(得分:1)
在$modelAttribute="hitoForm"
页面的form
标记中添加html
。它会解决你的问题。
<form:form role="form" action="modifyHito" method="post"
modelAttribute="hitoForm">
它可以在viewModificationForm()
中建模
model.addAttribute("hitoForm", hitoForm);