小编典典

Spring MVC表单处理

jsp

首先:我是Spring的初学者,这是我第一次尝试使用Spring MVC实现Web应用程序。这是我现在所做的:

实体:

@Entity
@Table(name = "coins")
public class Coin
{
    @Id
    @GeneratedValue
    private Integer id;

    @OneToOne
    private Country country;

    private double value;

    private int year;
}

@Entity
@Table(name = "countries")
public class Country
{
    @Id
    @GeneratedValue
    private Integer id;

    private String name;
}

控制器:

@Controller
public class CoinViewController {

    @Autowired
    private CoinService service;

    @Autowired
    private CountryService countryService;

    @ModelAttribute("countries")
    public List<Country> frequencies() {
        return countryService.get();
    }

    @RequestMapping(value = "/coins/add", method = RequestMethod.GET)
    public String addCoin(Model model) {
        model.addAttribute("coin", new Coin());

        return "coins/add";
    }

    @RequestMapping(value = "/coins/add", method = RequestMethod.POST)
    public String addCoinResult(@ModelAttribute("coin") Coin coin, BindingResult result) {
        // TODO: POST HANDLING

        return "/coins/add";
    }
}

JSP:

<form:form action="add" method="POST" modelAttribute="coin">
    <div class="form-group">
        <label for="country">Country:</label>
        <form:select path="country" class="form-control" >
            <form:option value="" label="-- Choose one--" />
            <form:options items="${countries}" itemValue="id" itemLabel="name" />
        </form:select>
    </div>
    <div class="form-group">
        <label for="value">Value:</label>
        <form:input path="value" class="form-control" />
    </div>
    <div class="form-group">
        <label for="year">Year:</label>
        <form:input path="year" class="form-control" />
    </div>
    <button type="submit" value="submit" class="btn btn-default">Erstellen</button>
</form:form>

但是,当我尝试保存JSP的输入时,总会得到以下信息:

字段“国家”上的对象“硬币”中的字段错误:拒绝的值[1];代码[typeMismatch.coin.country,typeMismatch.country,typeMismatch.Country,typeMismatch];参数[org.springframework.context.support.DefaultMessageSourceResolvable:代码[coin.country,country];
参数[];
默认消息[国家/地区]];默认消息[未能将类型’java.lang.String’的属性值转换为属性’country’的必需类型’Country’;嵌套的异常是java.lang.IllegalStateException:无法将属性“国家/地区”的[java.lang.String]类型的值转换为所需的[Country]类型:找不到匹配的编辑器或转换策略]

所以我的问题是:

  1. 我应该使用什么编辑器/转换器?
  2. 如何在我的控制器中注册其中之一?

阅读 327

收藏
2020-06-10

共1个答案

小编典典

您可以将自定义编辑器注册到控制器类的initBinder中:

@Controller
public class CoinViewController {

    @Autowired
    private CountryEditor countryEditor;

    @InitBinder
    protected void initBinder(final WebDataBinder binder, final Locale locale) {
        binder.registerCustomEditor(Country.class, countryEditor);
    }

    ......
}

locale在这种情况下不需要参数,但是如果您需要使用语言环境进行转换,例如在使用日期时,该参数将很有用)

您可以定义CountryEditor以下内容:

@Component
public class CountryEditor extends PropertyEditorSupport {

    @Autowired
    private CountryService countryService;

    @Override
    public void setAsText(final String text) throws IllegalArgumentException {
        try{ 
            final Country country = countryService.findById(Long.parseLong(text));
            setValue(cliente);
        }catch(Exception e){
            setValue(country);
            // or handle your exception
        }
    }
}

我让spring用@Component注解处理编辑器的注入。因此,如果您喜欢这种方式,请记住为该类启用程序包扫描!

希望有帮助!

2020-06-10