Javaの基本的な話なのかもしれませんが、教えてください。
HandlerInterceptorをimplementsして作成したpreHandleメソッドで
共通の変数を設定し、@Controllerや@Serviceでそれを使いまわすことはできるでしょうか?
例を挙げると、現在java.util.Localeをコントローラの@RequestMappingしたメソッドの
引数に指定してロケールによってメッセージプロパティを条件分岐しているのですが、
全てのコントローラのすべてのメソッドの引数にLocaleを追加するのもおかしい気がします。
java
1@Controller 2public class UserController { 3 @Autowired 4 protected MessageSource messageSource; 5 6 @RequestMapping(value = "/insert", method = RequestMethod.GET) 7 public ModelAndView insert(HttpServletRequest req, Locale locale, ModelAndView mav) { 8 String p1 =req.getParameter("param1"); 9 String message = messageSource.getMessage("hello.world", null, locale); 10 } 11}
理想↓
java
1@Controller 2public class UserController extends BaseController { 3 4 @RequestMapping(value = "/insert", method = RequestMethod.GET) 5 public ModelAndView insert(HttpServletRequest req, ModelAndView mav) { 6 7 String p1 =req.getParameter("param1"); 8 // ↓スーパークラスでmessageSource.getMessage("hello.world", null, locale)を呼び出し 9 String message = getMessage("hello.world"); 10 } 11}
Springフレームワークのバージョンは3.1.1です。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。