我有这样的事情:
[DisplayName("First Name")]
[Required(ErrorMessage="{0} is required.")]
[StringLength(50, MinimumLength = 10, ErrorMessage="{0}'s length should be between {2} and {1}.")]
public string Name { get; set; }
Run Code Online (Sandbox Code Playgroud)
我想要以下输出:
它在使用ASP.NET MVC2错误摘要时有效,但当我尝试手动验证它时,如下所示:
ValidationContext context = new ValidationContext(myModel, null, null);
List<ValidationResult> results = new List<ValidationResult>();
bool valid = Validator.TryValidateObject(myModel, context, results, true);
Run Code Online (Sandbox Code Playgroud)
结果是:
怎么了?谢谢.
查看ReadOnlyCollection类的规范,它确实实现了IList接口.
IList接口具有Add/Update/Read方法,我们称之为接口的前置条件.在我的应用程序的任何地方,如果我有一个IList我应该能够做所有这些操作.
但是,如果我在代码中的某处返回ReadOnlyCollection并尝试调用.Add(...)方法呢?它会抛出NotSupportedException.你认为这是一个糟糕设计的好例子吗?另外,这个类是否打破了Liskov替换原则?
为什么Microsoft以这种方式实现?是否更容易(和更好)使这个ReadOnlyCollection只实现IEnumerable接口(顺便说一下,已经只读)?
我有一个名为的简单POJO Check
.我有一个简单的休息库:
@RepositoryRestResource(collectionResourceRel = "check",path = "check")
public interface RestCheckRepo
extends JpaRepository<Check,Integer> {
public List<Check> findByShopName(@Param("shop") String shop);
public List<Check> findByDateTime(@Param("dt")Date dt);
public List<Check> findByShopNameAndDateTime(@Param("shop")String shop, @Param("dt")Date dt);
public List<Check> findByShopNameAndDateTimeBetween(@Param("shopName") String shop,
@Param("start")Date t1,
@Param("end") Date t2);
}
Run Code Online (Sandbox Code Playgroud)
一切正常!! 但我不知道如何使用java.util.Date
as 实现请求处理程序@RequestParam
.示例:http:// localhost:8080/check/search/findByDateTime?dt = {value}
UPDATE 请求 http:// localhost:8080/check/search/findByDateTime?dt = 2015-08-10T13:47:30 --->响应:
{
"cause": {
"cause":null,
"message":null
},
"message":"Failed to convert from type java.lang.String to type @org.springframework.data.repository.query.Param java.util.Date for value '2015-10-07T15:04:46Z'; nested exception is …
Run Code Online (Sandbox Code Playgroud) 我在SpringMvc应用程序上使用以下操作:
@RequestMapping(value = "/test", method = RequestMethod.GET)
public ModelAndView test(
@ModelAttribute List<Group> groups
) {
//return whatever
}
Run Code Online (Sandbox Code Playgroud)
我的Group类有一个'id'和'name'属性.默认的getter/setter.我应该如何调用此操作才能正确实现此列表?
我试过类似的东西:/
test?groups.id = 2&groups.name = stackrocks&groups.id = 3&groups.name = stackrules
不起作用.
还尝试了:/
test?groups [].id = 2&groupss [].name = stackrocks&groupss [] .id = 3&groupss [].name = stackrules
没有成功.
那么,在使用SpringMvc时如何绑定列表?
c# ×2
java ×2
spring ×2
asp.net-mvc ×1
binding ×1
collections ×1
frameworks ×1
liskov-substitution-principle ×1
rest ×1
spring-mvc ×1