我的 spring 应用程序在将“日期”保存到“数据库”时遇到问题。错误在哪里?
错误
Failed to convert property value of type [java.lang.String] to required type [java.sql.Date] for property bornDate; nested exception is java.lang.IllegalArgumentException: Could not parse date: Unparseable date: "2016-11-02"
数据库
use lifecalc;
create table Man (
manId int not null auto_increment primary key,
name varchar(30) not null,
bornDate date,
lastDate date
);
insert into man value
(null, "Pawel Cichon", "1920-11-30", "2000-02-20");
实体
@Entity
@Table(name="Man")
public class Man {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="manId")
private int manId;
@Column
private String name;
@DateTimeFormat(pattern = "yyyy-MM-dd")
@Column
private java.sql.Date bornDate;
@DateTimeFormat(pattern = "yyyy-MM-dd")
@Column
private java.sql.Date lastDate;
//getter end setter
Controller
@Controller
@RequestMapping("/")
public class AppController {
@Autowired
private ManService manService;
@ModelAttribute("man")
public Man modelToAddMan(){
return new Man();
}
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/mm/dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor( dateFormat, true));
}
@RequestMapping(value="/addMan.html", method = RequestMethod.POST)
public String addManFinish(@Valid @ModelAttribute("man") Man man, BindingResult result) {
if(result.hasErrors()){
return "addMan";
} else{
manService.addMan(man);
return "redirect:/index.html";
}
}
addMan.html
<form:form method="POST" modelAttribute="man">
<div class="form-group">
<label for="name" class="col-sm-2 control-label">name</label>
<form:input path="name" />
<form:errors path="name" cssClass="error"/>
</div>
<div class="form-group">
<label for="bornDate" class="col-sm-2 control-label">bornDate</label>
<form:input path="bornDate" />
<form:errors path="bornDate" cssClass="error"/>
</div>
<div class="form-group">
<label for="lastDate" class="col-sm-2 control-label">lastDate</label>
<form:input path="lastDate" />
<form:errors path="lastDate" cssClass="error"/>
</div>
<div class="form-group">
<input type="submit" class="btn btn-success" value="save" /> <a
class="btn btn-danger" role="button"
href="<spring:url value="/index.html" />">cancel </a>
</div>
</form:form>
最佳答案
代替:
@DateTimeFormat(pattern = "yyyy-MM-dd")
@Column
private java.sql.Date bornDate;
使用 java.util.date:
@DateTimeFormat(pattern = "yyyy-MM-dd")
@Column
private java.util.date bornDate;
关于java - 无法转换 [java.lang.String] 类型的属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40494718/
我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h
我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]
这道题是thisquestion的逆题.给定一个散列,每个键都有一个数组,例如{[:a,:b,:c]=>1,[:a,:b,:d]=>2,[:a,:e]=>3,[:f]=>4,}将其转换为嵌套哈希的最佳方法是什么{:a=>{:b=>{:c=>1,:d=>2},:e=>3,},:f=>4,} 最佳答案 这是一个迭代的解决方案,递归的解决方案留给读者作为练习:defconvert(h={})ret={}h.eachdo|k,v|node=retk[0..-2].each{|x|node[x]||={};node=node[x]}node[
我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val
我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在
我有一个包含模块的模型。我想在模块中覆盖模型的访问器方法。例如:classBlah这显然行不通。有什么想法可以实现吗? 最佳答案 您的代码看起来是正确的。我们正在毫无困难地使用这个确切的模式。如果我没记错的话,Rails使用#method_missing作为属性setter,因此您的模块将优先,阻止ActiveRecord的setter。如果您正在使用ActiveSupport::Concern(参见thisblogpost),那么您的实例方法需要进入一个特殊的模块:classBlah
我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r
我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e