Spring採用視圖時,使用@SessionAttributes("")註解出現異常

Spring採用視圖時,使用@SessionAttributes("")註解出現Cannot expose session attribute 'user' because of an existing model object of the same name異常問題

錯誤原因:

因爲request  session默認是不會加入velicity context中的,所以要配置上

exposeRequestAttributes和exposeSessionAttributes.但是問題來了,往session加入重複值是就會出現 because of an existing model object of the same name異常,

解決方法一:

     添加屬性<property name="allowSessionOverride" value="true"/> 允許session覆蓋舊值

VelocityViewResolver配置:

<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
    <property name="suffix" value=".vm" />
    <property name="exposeSpringMacroHelpers" value="true" />
    <property name="exposeRequestAttributes" value="true" />
    <property name="exposeSessionAttributes" value="true" />
    <property name="contentType" value="text/html;charset=UTF-8" />
    <property name="allowSessionOverride" value="true"/>
    <property name="dateToolAttribute" value="dateTool" />
    <property name="toolboxConfigLocation" value="/WEB-INF/toolbox.xml" />
</bean>

解決方法二:

springBoot 設置是否允許HttpSession屬性覆蓋:

在application.properties加入:

spring.freemarker.allow-session-override = true

解決方法三:

使用   request.removeAttribute("") 將 該name的Attribute去掉

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章