首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring表单处理

Spring表单处理
EN

Stack Overflow用户
提问于 2014-09-16 19:08:52
回答 1查看 231关注 0票数 0

首先:我是Spring的初学者,这是我第一次尝试用Spring实现web应用程序。以下是我所做的事:

实体:

代码语言:javascript
复制
@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;
}

主计长:

代码语言:javascript
复制
@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:

代码语言:javascript
复制
<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保存输入时,我总是得到以下内容:

字段'Country‘上的对象’硬币‘中的字段错误:拒绝值1;代码typeMismatch.coin.country、typeMismatch.country、typeMismatch.Country、typeMismatch;参数typeMismatch.country代码coin.country,country;参数[];默认消息国家];默认消息[未能将'java.lang.String’类型的属性值转换为属性‘country’所需的类型'country';嵌套的例外是java.lang.IllegalStateException:无法将类型java.lang.String的值转换为属性“Country”所需的类型国家:没有找到匹配的编辑器或转换策略]

所以我的问题是:

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

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-09-16 19:46:16

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

代码语言:javascript
复制
@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,如下所示:

代码语言:javascript
复制
@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注释处理编辑器的注入。因此,如果您喜欢这样做,记得启用该类的包扫描!

希望能帮上忙!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25876601

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档