0

私は豆を持っています

<bean class="myprogram.FileList">

定義されています。

ここで、この Bean に JSP からアクセスできるようにしたいと考えています。これを達成する方法は?

最初に考えたのは、コントローラー メソッドのどこかで Bean にアクセスし、それをモデルに配置することです。

@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
    logger.info("Welcome home! The client locale is {}.", locale);

    FileList fileList = // some code to access fileList bean

    model.addAttribute("fileList", fileList);

    return "home";
}

しかし、おそらくこれは必須ではないか、Bean 構成のどこかに記述できますか?

アップデート

答えはexposedContextBeanNamesパラメータです。

4

1 に答える 1

2

まず、@Autowiredアノテーションを使用して Bean をコントローラーに注入します。

@Autowired
private FileList fileList;

次に、既に行ったようにモデルに追加しますmodel.addAttribute("fileList", fileList);

JSP では、JSTLを使用してアクセスします。例:

Some property from File List bean: <c:out value="${fileList.someProperty}"/>
于 2013-05-03T11:40:33.893 に答える