2015-09-27 83 views
0

在我的應用程序中,我希望在應用程序啓動時存儲列表數據,並且我希望在需要時在jsp頁面上獲取它。爲此,我已經定義bean如下從春季會話中無法顯示jsp上的列表

<beans:bean id="systemService" class="com.mycompany.System" init-method="setMasterList" lazy-init="false"/> 

component如下

@Controller 
@Component("system") 
@Scope(WebApplicationContext.SCOPE_GLOBAL_SESSION) 
public class System 
{ 
    public Map<Integer, String> masterList = new HashMap<Integer, String>(); 

    public Map<Integer, String> populateMasterList() 
    { 
     masterList.put(1, "About US"); 
     masterList.put(2, "About US"); 
     return masterList; 
    } 

    public Map<Integer, String> getMasterList() 
    { 
     return masterList; 
    } 

    public void setMasterList() 
    { 
     this.masterList = populateMasterList(); 
    } 
} 

我試圖找回在JSP中主列表。我試圖訪問它在jsp上如下

Master : ${system.masterList} 

但在jsp上顯示爲空白。我也嘗試使用Scope作爲@Scope(WebApplicationContext.SCOPE_SESSION)。但不能成功。

回答

0

試試這個:

<beans:bean id="systemService" class="com.mycompany.System" init-method="populateMasterList" lazy-init="false" scope="application"/> 

並從系統級的所有註釋。

public class System 
{ 
    ... 
} 

在JSP中,使用

${systemService.masterList[1]} 

在視圖解析器,加

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    ... 
    ... 
    <property name="exposeContextBeansAsAttributes" value="true" /> 
</bean> 
+0

我嘗試過,但它不工作。 – Abhijit

+0

日誌在加載應用程序上下文時會說些什麼?它是否包含有關systemService bean的所有內容? – user2953113

+0

沒有。我試圖調試它。它執行得很好。 – Abhijit