将表单绑定到SPRING 3.0中的对象

时间:2013-01-24 03:27:58

标签: spring spring-mvc

您好我正在关注Passing parameters from JSP to Controller in Spring MVC显然我错过了一些因为我正在

  

javax.el.PropertyNotFoundException:在com.outbottle.hellospring.entities.Movie类型中找不到属性'Title'

关于我缺少什么的任何想法?

我的控制器:

package com.outbottle.hellospring.controllers;

import com.outbottle.hellospring.entities.Movie;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;



@Controller
@SessionAttributes("Movie")

public class FormPractice{

//display the form
@RequestMapping(value="/form", method= RequestMethod.GET)
public String form(ModelMap map) {

    map.addAttribute("Movie", new Movie());
    //return new ModelAndView("form", "movie", new Movie());  
    return "form";

}


// Process the form

@RequestMapping(value="/showMovies", method = RequestMethod.POST)
public String processMovieRegistration(@ModelAttribute("Movie") Movie moviedata,
                                       BindingResult result,                                           
                                       HttpServletRequest request,
                                       Map<String, Object> map){


    //System.out.print(moviedata.Title);
    map.put("moviedata", moviedata);
    //map.addAttribute("movie", moviedata);

    map.put("date", request.getParameter("date"));

    return "showMovies";
}



   public Movie formBackingObject() {
     return new Movie();
   }

}

我的对象

package com.outbottle.hellospring.entities;

import java.util.Date;




public class Movie {

    private String Title;
    private Date  ReleaseDate;



    /**
     * @return the ReleaseDate
     */
    public Date getReleaseDate() {
        return ReleaseDate;
    }

    /**
     * @param ReleaseDate the ReleaseDate to set
     */
    public void setReleaseDate(Date ReleaseDate) {
        this.ReleaseDate = ReleaseDate;
    }

    /**
     * @return the Title
     */
    public String getTitle() {
        return Title;
    }

    /**
     * @param Title the Title to set
     */
    public void setTitle(String Title) {
        this.Title = Title;
    }


}

表格

<form:form method="post" action="showMovies" modelAttribute="Movie" commandName="Movie">

    <table>
        <tr>
            <td>Title</td>
            <td><form:input path="Title" /></td>
        </tr>
        <tr>
            <td>Date</td>
            <!--Notice, this is normal html tag, will not be bound to an object -->
            <td><input name="date" type="text"/></td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="submit" value="send"/>
            </td>
        </tr>
    </table>

</form:form>

观点:

<body>
    <h3>Here are the list</h3>

     <c:choose> 
        <c:when test="${not empty moviedata.Title}">
            <b>${moviedata.Title}</b>
        </c:when>
        <c:otherwise>
            <b>No Movie yet.</b>
        </c:otherwise>
    </c:choose>
<br />
<br />
    <c:choose> 
        <c:when test="${not empty date}">
            <b>${date}</b>
        </c:when>
        <c:otherwise>
            <b>still nothing.</b>
        </c:otherwise>
    </c:choose>
</body>

1 个答案:

答案 0 :(得分:1)

不知道为什么会删除Title中的大写字母“T”。

<body>
<h3>Here are the list</h3>

 <c:choose> 
    <c:when test="${not empty moviedata.title}">
        <b>${moviedata.title}</b>
    </c:when>
    <c:otherwise>
        <b>No Movie yet.</b>
    </c:otherwise>
</c:choose>

如果有人能解释为什么这会有效,那将有助于我更快地了解SPRING 3.0 ..但是现在我很高兴我可以继续前进..