2

我正在开发一个 Spring MVC3 应用程序,如果链接被禁用,我需要启用/禁用页面中的链接并在链接上提供工具提示(某种帮助)。在许多情况下可以禁用链接,我想知道应该受到影响的各个层。

例如,考虑一个管理员用户尝试续订订阅的测试用例。如果我应该能够续订

  1. 我是管理员用户
  2. 我的订阅将在未来 30 天内结束
  3. 我不会续订自己的订阅。

就我而言,我有控制器,调用一个服务来确定用户是否有资格续订。如果不符合续订条件,请致电其他提供帮助文本的服务。然后视图 (JSP) 启用/禁用链接并显示基于 modelAttribute 的帮助文本。

我的问题是..

  1. 如何避免两次调用服务(这似乎再次调用相同的方法)。我可以向 User 模型添加一个属性来保存帮助文本,但我不确定帮助文本是否应该在模型中

  2. 如何从属性文件中提取更新帮助文本,而不是在服务中进行硬编码。我在验证器中使用messages.properties,但我不确定如何在@service 中使用它。

  3. 我是否应该在视图 (JSP) 中硬编码更新帮助而不是过度设计它?

我在下面有一些代码片段以进一步澄清查询..

public class UserController {
   @RequestMapping(value="/viewUserDetails", method=RequestMethod.GET)
      public String userDetails() {
          if(userService.isEligibleToRenew()) {
                user.setEligibleToRenew(true);
                model.addAttribute("renewalHelp", userService.getRenewalHelp())
          }
       }
   }

 @Service
 public class UserService {
    public boolean isEligibleToRenew (User user) {
        if (isAdminUser() && 
            isSubscriptionEndingin30days() && 
            !isRenewingOwnSubscription()) {
        return true;
    }
        return false;
}

public String renewalHelp(User user) {
    if (!isAdminUser ()) {
        return "You must be an admin to renew your subscription";
    }   else if (!isSubscriptionEndingin30days()) {
        return "Your subscription is not expiring in the next 30 days. You cannot renew now"; 
    } else if (isRenewingOwnSubscription()) {
        return "You cannot renew your own subscription";
    }
}
}
4

1 回答 1

2
  1. Make your service return an object that contains all the information that it requires, in this way you'll only call the service (with all its logic) only once.

  2. Something I've done in the past (in the object that I mentioned above), is to have an enum that describes the message, and then use Spring or any MVC framework to pick a message from a resource bundle with the localized message.

  3. Maybe, if it keeps things simple and this feature to display different messages is not that important.

About 1, I used that in a big ecommerce app in which we had to display a price with a text. The text could have hints related to discounts (e.g. now £1.99 or from £3.99), so I created an object that described a price with an enum + value. The enum later was localized to a message in the correct locale.

于 2012-11-06T21:35:06.533 回答