BaseController.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package com.goafanti.common.controller;
  2. import java.text.DateFormat;
  3. import java.text.MessageFormat;
  4. import java.text.SimpleDateFormat;
  5. import java.util.Date;
  6. import javax.annotation.Resource;
  7. import javax.servlet.http.HttpServletRequest;
  8. import org.apache.commons.lang3.StringUtils;
  9. import org.apache.shiro.SecurityUtils;
  10. import org.springframework.beans.factory.annotation.Value;
  11. import org.springframework.beans.propertyeditors.CustomDateEditor;
  12. import org.springframework.core.io.support.ResourcePropertySource;
  13. import org.springframework.stereotype.Controller;
  14. import org.springframework.ui.Model;
  15. import org.springframework.web.bind.WebDataBinder;
  16. import org.springframework.web.bind.annotation.InitBinder;
  17. import org.springframework.web.bind.annotation.ModelAttribute;
  18. import com.goafanti.common.bo.Error;
  19. import com.goafanti.common.model.User;
  20. import com.goafanti.core.shiro.token.TokenManager;
  21. @Controller
  22. public class BaseController {
  23. @Resource(name = "errorResource")
  24. private ResourcePropertySource errorResource;
  25. @Value(value = "${static.host}")
  26. private String staticHost = null;
  27. @Value(value = "${upload.path}")
  28. private String uploadPath = null;
  29. @Value(value = "${upload.private.path}")
  30. private String uploadPrivatePath = null;
  31. @Value(value = "${accessKey}")
  32. private String accessKey = null;
  33. @Value(value = "${accessSecret}")
  34. private String accessSecret = null;
  35. protected Error buildError(String key) {
  36. return buildError(key, null);
  37. }
  38. protected Error buildError(String key, String message) {
  39. String msg = (String) errorResource.getProperty(key);
  40. if (StringUtils.isEmpty(msg)) {
  41. msg = message;
  42. }
  43. return new Error(key, msg);
  44. }
  45. /**
  46. * 带参数的error message
  47. *
  48. * @param key
  49. * @param message
  50. * @param object
  51. * @return
  52. */
  53. protected Error buildError(String key, String message, Object... object) {
  54. String msg = (String) errorResource.getProperty(key);
  55. if (StringUtils.isEmpty(msg)) {
  56. msg = message;
  57. }
  58. msg = MessageFormat.format(msg, object);
  59. return new Error(key, msg);
  60. }
  61. protected Error buildErrorByMsg(String msg, Object... params) {
  62. return new Error(MessageFormat.format(msg, params));
  63. }
  64. @InitBinder
  65. public void initBinder(WebDataBinder binder) {
  66. DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  67. dateFormat.setLenient(true);
  68. binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
  69. }
  70. @ModelAttribute
  71. public void prepareModel(HttpServletRequest request, Model model) {
  72. model.addAttribute("staticHost", staticHost);
  73. model.addAttribute("basePath", request.getContextPath());
  74. model.addAttribute("uploadPath",uploadPath);
  75. model.addAttribute("uploadPrivatePath",uploadPrivatePath);
  76. if(SecurityUtils.getSubject().getPrincipal() instanceof User){
  77. model.addAttribute("userData", TokenManager.getToken() );
  78. }
  79. /*model.addAttribute("accessKey",accessKey);
  80. model.addAttribute("accessSecret",accessSecret);*/
  81. }
  82. }