这次项目中用Freemarker框架,取Session报错.报session未定义.
main.ftl中代码:
<#if session.currentUser?exists>
<#include "/control/con_login1.html">
<#else>
<#include "/control/con_login.html">
</#if>
con_login1.ftl中代码:
<#if Session["currentUser"]?exists>
<#assign currentUser = Session["currentUser"]>
</#if>
<div class="dengluchenggong">
<div class="index_login" style="margin-top:0;">
<div class="index_login_top"></div>
<div class="index_login_mid">
<div class="yonghu_pic">
<div class="yh_pic">
<img
src="/huigou/images/huigou/touxiang.gif" />
</div>
<font disabled>欢迎光临会购</font>
</div>
<p>
手机号:${currentUser.mobile?if_exists}
</p>
<p>
<#if currentUser.petname?exists>
昵称:${currentUser.petname?if_exists}
<#else>
昵称: ${currentUser.username?if_exists}
</#if>
</p>
<li>
<div class="login_bottom">
<a href="/huigou/huigou/user.jsp"> <img
src="/huigou/huigou/images/gerenzhongxin.gif"
width="67" height="19" border="0" /> </a>
<a href="/huigou/loginout.jsp"><img
src="/huigou/huigou/images/gif-0545.gif"
width="57" height="18" border="0" /> </a>
</div>
</li>
</div>
<div class="index_login_bot"></div>
</div>
<div class="clr"></div>
</div>
con_login中代码:
<script src="/huigou/dwr/util.js"
type="text/javascript"></script>
<script src="/huigou/dwr/engine.js"
type="text/javascript"></script>
<script src="/huigou/dwr/interface/userService.js"
type="text/javascript"></script>
<script type="text/javascript"
src="/huigou/js/huigou/reg.js"></script>
<script type="text/javascript">
<!--
function submitLoginForm(){
var username=document.getElementById("username");
var password=document.getElementById("password");
if(username.value==""){
alert("用户名不能为空!");
username.focus();
return false;
}
if(password.value==""){
alert("密码不能为空!");
password.focus();
return false;
}
userService.singleton(username.value,password.value,function(flag){
if(flag!=""){
switch(flag){
case '0':
alert("密码错误");break;
case '1':
alert("账号不存在");break;
case '-100':
alert("出错了!");break;
default:
document.getElementById("abc").innerHTML="<div class='dengluchenggong'>"
+"<div class='index_login' style='margin-top:0;'>"
+" <div class='index_login_top'></div>"
+"<div class='index_login_mid'>"
+"<div class='yonghu_pic'>"
+"<div class='yh_pic'>"
+"<img src='/huigou/images/huigou/touxiang.gif' />"
+"</div>"
+"<font disabled>欢迎光临会购</font>"
+"</div>"
+"<p>"
+"<font disabled>手机号:</font>"+flag
+"</p>"
+"<p>"
+"<font disabled>昵称:</font>"+username.value
+"</p>"
+"<li>"
+"<div class='login_bottom'>"
+"<a href='/huigou/huigou/user.jsp'> <img src='/huigou/huigou/images/gerenzhongxin.gif' width='67' height='19' border='0' /> </a>"
+" "
+"<a href='/huigou/loginout.jsp'><img src='/huigou/huigou/images/gif-0545.gif' width='57' height='18' border='0' /> </a>"
+"</div>"
+"</li>"
+"</div>"
+"<div class='index_login_bot'></div>"
+"</div>"
+"<div class='clr'></div>"
+"</div>";
}
}
})
}
//-->
</script>
<div class="index_login" id="abc">
<div class="index_login_top"></div>
<div class="index_login_mid">
<ul>
<li>
<font disabled>账号:</font>
<input type="text" name="username" size="6" id="username" />
</li>
<li>
<font disabled>密码:</font>
<input type="password" name="password" size="6" id="password" />
</li>
<li>
<div class="login_bottom">
<a href="#" onclick="submitLoginForm();">
<img
src="/huigou/huigou/images/index_denglu.gif"
width="73" height="23" border="0" /> </a>
<a
href="/huigou/c/user.shtml?method=toreg"><img
src="/huigou/huigou/images/index_zhuche.gif"
width="73" height="23" border="0" /> </a>
</div>
<font disabled> 卡用户密码"5higo.com"</font>
</li>
</ul>
</div>
<div class="index_login_bot"></div>
</div>
运行时就报错:
Expression Session is undefined on line 128, column 54 in main.ftl.
The problematic instruction:
----------
==> if-else [on line 128, column 49 in main.ftl]
----------
Java backtrace for programmers:
----------
freemarker.core.InvalidReferenceException: Expression Session is undefined on line 128, column 54 in main.ftl.
at freemarker.core.TemplateObject.assertNonNull(TemplateObject.java:124)
at freemarker.core.DynamicKeyName._getAsTemplateModel(DynamicKeyName.java:75)
at freemarker.core.Expression.getAsTemplateModel(Expression.java:89)
at freemarker.core.BuiltIn$existsBI._getAsTemplateModel(BuiltIn.java:636)
at freemarker.core.BuiltIn$existsBI.isTrue(BuiltIn.java:647)
at freemarker.core.IfBlock.accept(IfBlock.java:80)