将表单参数从JSP绑定到弹簧控制器

时间:2016-08-29 23:18:56

标签: spring spring-mvc data-binding

我知道这是一个之前被问过的问题。我确实看过那些问题,但我仍然无法解决我的问题,因此写入stackoverflow。我试图将表单参数从我的表单addArticles.jsp绑定到控制器。在我执行system.out.println的控制器上,我只获取categoryId而不获取categoryName。我不确定我在做什么是不正确的,非常有趣。

addArticles.jsp

package com.java.bricks.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
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.servlet.ModelAndView;

import com.java.bricks.model.Article;
import com.java.bricks.model.Category;
import com.java.bricks.service.ArticleService;
import com.java.bricks.service.CategoryService;

@Controller("articlesController")
@RequestMapping(value="/articles")
public class ArticlesController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private CategoryService categoryService;

    @RequestMapping(value="/list", method=RequestMethod.GET)
    public ModelAndView listArticles(@ModelAttribute("command") Article article, BindingResult bindingResult){

        Map<String,Object> model = new HashMap<String,Object>();
        model.put("articles", articleService.listArticles());
        return new ModelAndView("articlesList",model);
    }

    @RequestMapping(value="/add",method=RequestMethod.GET)
    public ModelAndView addArticle(@ModelAttribute("command") Article article, BindingResult bindingResult) {

        Map<String,Object> model = new HashMap<String,Object>();
        model.put("articles", articleService.listArticles());
        model.put("categories", categoryService.listCategories());
        return new ModelAndView("addArticle",model);
    }

    @RequestMapping(value="/save",method=RequestMethod.POST)
    public ModelAndView saveArticle(@ModelAttribute("command") Article article, BindingResult bindingResult) {

        Map<String,Object> model = new HashMap<String,Object>();
        System.out.println("----------------------");
        System.out.println(article.getCategory());
        System.out.println(article.getCategory().getCategoryName());
        System.out.println(article.getCategory().getCategoryId());
        articleService.addArticle(article);
        model.put("articles",articleService.listArticles());
        return new ModelAndView("addArticle",model);
    }

}

ArticlesController.java

package com.java.bricks.model;


public class Article {

    private Long id;
    private String title;
    private String description;
    private String keywords;
    private String content;
    private Category category;

    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    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 getKeywords() {
        return keywords;
    }
    public void setKeywords(String keywords) {
        this.keywords = keywords;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
    public Category getCategory() {
        return category;
    }
    public void setCategory(Category category) {
        this.category = category;
    }


}

当我点击保存时,SOP语句的前4行是

的System.out.println(article.getCategory());
输出:类别[categoryId = 29,categoryName = null]

的System.out.println(article.getCategory()getCategoryName()); 空

的System.out.println(article.getCategory()。getCategoryId()) 29

我不确定为什么在控制器中没有填充categoryName。

Article.java

{{1}}

1 个答案:

答案 0 :(得分:1)

您不会在表单内容中获取类别名称,因为类别的名称正在用作选项的标签。只有值被绑定。这是你的绑定path="category.categoryId"。你没有绑定任何path="category.categoryName",所以它将是空的。

因此,在您的控制器中,您必须通过其ID获取类别。如果您想进行一些自动自定义转换,这是一个单独的问题。

Here's a nice article on entity conversion.