【问题标题】:Submitting form with Spring使用 Spring 提交表单
【发布时间】:2016-06-29 20:14:33
【问题描述】:

我完全是一个使用 Spring 的菜鸟。

我正在尝试提交表单,但出现以下错误:

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>

【问题讨论】:

    标签: java xml spring forms spring-mvc


    【解决方案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"; 
        } 
    

    【讨论】:

      【解决方案2】:

      html 页面的form 标记中添加$modelAttribute="hitoForm"。它会解决你的问题。

          <form:form role="form" action="modifyHito" method="post" 
          modelAttribute="hitoForm">
      

      并在viewModificationForm() 中建模

          model.addAttribute("hitoForm", hitoForm); 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-04
        • 2023-03-04
        • 1970-01-01
        • 2013-05-24
        • 2016-02-17
        相关资源
        最近更新 更多