Spring MVC - 使用POST请求方法时,Hibernate映射对象部分填充

时间:2014-02-20 18:39:07

标签: spring hibernate jsp spring-mvc hibernate-mapping

由于我决定使用相同的JSP来添加和编辑帖子,我只传递一个属性“saveUrl”,它定义了JSP中我的表单的操作。添加新帖子工作正常,但在编辑帖子时,返回控制器的对象缺少除描述之外的所有属性。

我在这里错过了什么或做错了什么?

感谢您的帮助!

我的控制器:

@Controller 
public class BlogController {

    private static final Logger logger = LoggerFactory.getLogger(BlogController.class);

    @Autowired
    private BlogPostManager bpManager;

    @Autowired
    private UserManager usrManager;

    .....

    @RequestMapping(value = "addPost", method = RequestMethod.GET)
    public String addPost(Locale locale, Model model, Principal principal) {
        model.addAttribute("post", new BlogPostEntity());
        /** some more code here **/
        return "addEditPost";
    }

    @RequestMapping(value = "addPostProcess", method = RequestMethod.POST)
    public String addPostProcess(Locale locale, Model model, Principal principal,  @ModelAttribute("post") BlogPostEntity blogPost) {
        blogPost.setDate(new Date());
        blogPost.setAuthor(usrManager.getUser(principal.getName()));
        bpManager.addBlogPost(blogPost);
        return "redirect:/latest";
    }

    @RequestMapping(value = "editPost/{id}", method = RequestMethod.GET)
    public String editPost(Locale locale, Model model, Principal principal, @PathVariable Integer id) {
        model.addAttribute("post", bpManager.getBlogPost(id));
        model.addAttribute("username", getUsername(principal));
        model.addAttribute("saveUrl", "");
        return "addEditPost";
    }

    @RequestMapping(value = "editPost/{id}", method = RequestMethod.POST)
    public String editPostProcess(Locale locale, Model model, Principal principal, @ModelAttribute("post") BlogPostEntity blogPost) {
        bpManager.updateBlogPost(blogPost);
        return "redirect:/latest";
    }

        /** some private methods **/

    }

addEditPost.jsp

  

注意:此jsp充当Apache tile的主体。

<h2>Create new post:</h2>
<form:form modelAttribute="post" action="${saveUrl}" method='POST'>

    <table>
        <tr>
            <td><form:label path="title">Title:</form:label></td>
            <td><form:input path="title"></form:input></td>
        </tr>
        <tr>
            <td><form:label path="description">Description:</form:label></td>
            <td><form:input path="description"></form:input></td>
        </tr>
        <tr>
            <td><form:label path="text">Text:</form:label></td>
            <td><form:input path="text"></form:input></td>
        </tr>
        <tr>
            <td><input value="Save" type="submit"></td>
            <td></td>
        </tr>
    </table>

</form:form> 

映射的BlogPost类:

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name = "BLOGPOST")
public class BlogPostEntity {

    @Id
    @GeneratedValue
    @Column(name = "ID")
    private int id;

    @Column(name = "TITLE", nullable = false, length = 100)
    private String title;

    @Column(name = "DESCRIPTION", length = 500)
    private String description;

    @Column(name = "TEXT", length = 5000)
    private String text;

    @Column(name = "DATE")
    private Date date;

    @ManyToOne(targetEntity = UserEntity.class)
    @JoinColumn(name = "authorid", referencedColumnName = "id")
    private UserEntity author;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public int getId() {
        return id;
    }

    public void setAuthor(UserEntity author) {
        this.author = author;
    }

    public UserEntity getAuthor() {
        return author;
    }

}

DAO for blogpost:

import java.util.ArrayList;
import java.util.List;

import org.danizmax.simpleblog.entity.BlogPostEntity;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository("blogpostdao")
public class BlogPostDaoImpl implements BlogPostDao {

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public void addBlogPost(BlogPostEntity blogPost) {
        getSession().persist(blogPost);
    }

    @Override
    public void removeBlogPost(int id) {
        BlogPostEntity entity = (BlogPostEntity) sessionFactory.getCurrentSession().load(BlogPostEntity.class, id);
        if (entity != null) {
            getSession().delete(entity);
        }
    }

    @Override
    @SuppressWarnings("unchecked")
    public List<BlogPostEntity> latest() {
        List<BlogPostEntity> result = new ArrayList<BlogPostEntity>();
        try {
            result = getSession().createQuery("FROM BlogPostEntity ORDER BY 'id' desc LIMIT 5;").list();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    @SuppressWarnings("unchecked")
    public List<BlogPostEntity> listPosts(int userId) {
        List<BlogPostEntity> result = new ArrayList<BlogPostEntity>();
        try {
            result = getSession().createQuery("FROM UserEntity").list();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public void updateBlogPost(BlogPostEntity blogPost) {
        blogPost = getBlogPost(blogPost.getId());
        getSession().update(blogPost);
    }

    @Override
    public BlogPostEntity getBlogPost(int id) {
        return (BlogPostEntity) getSession().get(BlogPostEntity.class, id);

    }

    private Session getSession() {
        return sessionFactory.getCurrentSession();
    }

}

更新:我一直在尝试从HERE尝试一下方法,但是返回到控制器的对象仍然是空的。

然后我将JSP中的saveURL更改为(我读到它可能很重要HERE):

<c:url var="addUrl" value="/secure/postProcess"/>
    <form:form modelAttribute="post" action="${addUrl}" method='POST'>

现在对象已填充,只有id仍为空。所以JSP可能存在问题。

0 个答案:

没有答案