小编goe*_*ing的帖子

如何将DisplayName放在ErrorMessage格式上

我有这样的事情:

    [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)

我想要以下输出:

  • 名字是必需的.
  • 名字的长度应在10到50之间.

它在使用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)

结果是:

  • 名称是必需的.
  • 姓名的长度应在10到50之间.

怎么了?谢谢.

c# asp.net-mvc data-annotations

24
推荐指数
2
解决办法
2万
查看次数

ReadOnlyCollection类是Bad Design的一个很好的例子吗?

查看ReadOnlyCollection类的规范,它确实实现了IList接口.

IList接口具有Add/Update/Read方法,我们称之为接口的前置条件.在我的应用程序的任何地方,如果我有一个IList我应该能够做所有这些操作.

但是,如果我在代码中的某处返回ReadOnlyCollection并尝试调用.Add(...)方法呢?它会抛出NotSupportedException.你认为这是一个糟糕设计的好例子吗?另外,这个类是否打破了Liskov替换原则

为什么Microsoft以这种方式实现?是否更容易(和更好)使这个ReadOnlyCollection只实现IEnumerable接口(顺便说一下,已经只读)?

c# collections frameworks liskov-substitution-principle

20
推荐指数
4
解决办法
2810
查看次数

Spring数据休息,url param中的java.util.Date?

我有一个名为的简单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.Dateas 实现请求处理程序@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)

java rest spring

5
推荐指数
1
解决办法
3778
查看次数

如何将对象列表绑定到SpringMvc Controller?

我在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时如何绑定列表?

java binding spring spring-mvc

2
推荐指数
1
解决办法
8054
查看次数