本稿はSpringFramewordの@Controllerについて説明します。
@Controllerとは
@ControllerはMVCのコントローラーに該当するクラスに付与します。
@Componentと同じく、Spirngのコンポーネントとして認識され、ApplicationContextに登録されることで、DI対象のクラスとなります。
以下、コントローラーのサンプルです。
|
@Controller
public class HomeController {
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
String formattedDate = dateFormat.format(date);
model.addAttribute("serverTime", formattedDate );
return "home";
}
}
|