2013-05-03 67 views
0

我有豆如何在Spring MVC中從控制器訪問bean?

<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的配置描述的地方?

UPDATE

答案是exposedContextBeanNames參數。

@Autowired 
private FileList fileList; 

然後將其添加到模型就像你已經做了:model.addAttribute("fileList", fileList);

+1

'$ {fileList}'? – NINCOMPOOP 2013-05-03 11:34:08

+0

fileList應該被注入到這個代碼中! – MariuszS 2013-05-03 11:34:57

回答

2

首先,使用@Autowired註解注入你的bean到控制器中。

在JSP中使用JSTL來訪問它。例如:

Some property from File List bean: <c:out value="${fileList.someProperty}"/>