| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- package com.goafanti.common.controller;
- import java.text.DateFormat;
- import java.text.MessageFormat;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import javax.annotation.Resource;
- import javax.servlet.http.HttpServletRequest;
- import org.apache.commons.lang3.StringUtils;
- import org.apache.shiro.SecurityUtils;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.beans.propertyeditors.CustomDateEditor;
- import org.springframework.core.io.support.ResourcePropertySource;
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.Model;
- import org.springframework.web.bind.WebDataBinder;
- import org.springframework.web.bind.annotation.InitBinder;
- import org.springframework.web.bind.annotation.ModelAttribute;
- import com.goafanti.common.bo.Error;
- import com.goafanti.common.model.User;
- import com.goafanti.core.shiro.token.TokenManager;
- @Controller
- public class BaseController {
- @Resource(name = "errorResource")
- private ResourcePropertySource errorResource;
- @Value(value = "${static.host}")
- private String staticHost = null;
-
- @Value(value = "${upload.path}")
- private String uploadPath = null;
-
- @Value(value = "${upload.private.path}")
- private String uploadPrivatePath = null;
-
- @Value(value = "${accessKey}")
- private String accessKey = null;
-
- @Value(value = "${accessSecret}")
- private String accessSecret = null;
- protected Error buildError(String key) {
- return buildError(key, null);
- }
- protected Error buildError(String key, String message) {
- String msg = (String) errorResource.getProperty(key);
- if (StringUtils.isEmpty(msg)) {
- msg = message;
- }
- return new Error(key, msg);
- }
- /**
- * 带参数的error message
- *
- * @param key
- * @param message
- * @param object
- * @return
- */
- protected Error buildError(String key, String message, Object... object) {
- String msg = (String) errorResource.getProperty(key);
- if (StringUtils.isEmpty(msg)) {
- msg = message;
- }
- msg = MessageFormat.format(msg, object);
- return new Error(key, msg);
- }
- protected Error buildErrorByMsg(String msg, Object... params) {
- return new Error(MessageFormat.format(msg, params));
- }
- @InitBinder
- public void initBinder(WebDataBinder binder) {
- DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- dateFormat.setLenient(true);
- binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
- }
- @ModelAttribute
- public void prepareModel(HttpServletRequest request, Model model) {
-
- model.addAttribute("staticHost", staticHost);
- model.addAttribute("basePath", request.getContextPath());
- model.addAttribute("uploadPath",uploadPath);
- model.addAttribute("uploadPrivatePath",uploadPrivatePath);
- if(SecurityUtils.getSubject().getPrincipal() instanceof User){
- model.addAttribute("userData", TokenManager.getToken() );
- }
-
- /*model.addAttribute("accessKey",accessKey);
- model.addAttribute("accessSecret",accessSecret);*/
- }
- }
|