java - Spring MVC & Freemarker session attribute not exposed -
i've got problem freemarker's viewresolver spring - session attributes not exposed view, in spring's internalresourceviewresolver.
now, important part is: when change spring's resolver freemarker's one, session attribute not passed, it's null. when spring's resolver working, session passed.
my code:
dispatcher-servlet.xml:
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> <bean id="usersession" class="com.revicostudio.web.session.usersession" scope="session"> </bean> <context:component-scan base-package="com.revicostudio.web" /> <mvc:annotation-driven /> <!-- <bean id="freemarkerconfig" class="org.springframework.web.servlet.view.freemarker.freemarkerconfigurer"> <property name="templateloaderpath" value="/web-inf/ftl/"/> <property name="freemarkervariables"> <map> <entry key="xml_escape" value-ref="fmxmlescape"/> </map> </property> </bean> <bean id="viewresolver" class="org.springframework.web.servlet.view.freemarker.freemarkerviewresolver"> <property name="cache" value="true"/> <property name="prefix" value=""/> <property name="suffix" value=".jsp"/> <property name="exposespringmacrohelpers" value="true"/> <property name="exposerequestattributes" value="true"/> <property name="allowrequestoverride" value="false" /> <property name="exposesessionattributes" value="true"/> <property name="allowsessionoverride" value="false" /> <property name="exposepathvariables" value="true"/> </bean> <bean id="fmxmlescape" class="freemarker.template.utility.xmlescape"/>--> <bean id="viewresolver" class="org.springframework.web.servlet.view.internalresourceviewresolver"> <property name="prefix" value="/web-inf/ftl/" /> <property name="suffix" value=".jsp" /> <property name="exposecontextbeansasattributes" value="true" /> </bean> </beans>
logincontroller.java:
@controller @requestmapping("/login") @sessionattributes("usersession") public class logincontroller { @autowired private usersdatabaseservice usersdatabaseservice; @requestmapping(method = requestmethod.post) public string login( @modelattribute userlogincredentials userlogincredentials, usersession usersession, final redirectattributes redirectattributes) { int failedloginattempts = usersession.getfailedloginattempts(); if (failedloginattempts < loginconfig.maxlogintries || system.currenttimemillis()-usersession.getfailedloginswaittimestart() > loginconfig.failedloginswaitminutes*60*1000) { if (usersdatabaseservice.isvalidpassword(userlogincredentials.getusername(), userlogincredentials.getpassword())) { usersession.setuser(usersdatabaseservice.getuser(userlogincredentials.getusername())); usersession.setfailedloginswaittimestart(system.currenttimemillis()); } else { failedloginattempts++; if (failedloginattempts == loginconfig.maxlogintries) { redirectattributes.addflashattribute("error", "you've entered invalid username or password more " + loginconfig.maxlogintries + " times. try again in " + loginconfig.failedloginswaitminutes +" minutes."); } else { redirectattributes.addflashattribute("error", "invalid username or password"); usersession.setfailedloginattempts(failedloginattempts); system.out.println(failedloginattempts); } } } else { redirectattributes.addflashattribute("error", "you've entered invalid username or password more " + loginconfig.maxlogintries + " times. try again in " + loginconfig.failedloginswaitminutes +" minutes."); } return "redirect:/"; } }
indexcontroller:
@controller @requestmapping("/index") public class indexcontroller { @requestmapping(method=requestmethod.get) public string getindex(model model) { return "index"; } @modelattribute("userregistercredentials") public userregistercredentials getuserregistercredentials() { return new userregistercredentials(); } @modelattribute("userlogincredentials") public userlogincredentials getuserlogincredentials() { return new userlogincredentials(); } }
index.jsp:
<body> <!-- code freemarker --> <#if error??> ${error} </#if> <#if usersession??> ${usersession} </#if> <#if !(usersession??)> b </#if> <!-- code jstl --> ${usersession}
you have session problem because of redirectattributes.addflashattribute(). means addflashattribute stores attributes in flashmap (which internally maintained in users session , removed once next redirected request gets fulfilled),
just try without redirectattributes.addflashattribute() session.
may you
Comments
Post a Comment