一.
<%@ taglib prefix="sec"
uri="http://www.springframework.org/security/tags"%>
<%
<input type="hidden" value="<sec:authentication property="principal.username" />">
<!--购物车表单-->
以上代码是在购物车中,用了spring的安全模块里的标签语句。
二.报错:
org.springframework.beans.NotReadablePropertyException: Invalid property 'principal.username' of bean class [org.springframework.security.authentication.AnonymousAuthenticationToken]: Bean property 'principal.username' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
三.分析日志报告是安全模块的principal.username不可用。
于是做如下调试:
<!--<input type="hidden" value="<sec:authentication property="principal.username" />"> -->
把该标签语句在jsp中注释掉,重启服务器,发现还是报相同的错误。
接下来,删除该input标签,重启服务器,不报错了,恢复正常。
后来分析发现,<sec:authentication property="principal.username" />只有在用户登录了(即在spring安全模块中用户已通过认证),才能用,这里要配合其他标签语句一起用,检测用户是否登录。
四.调整
<!-- 用户登录后才显示如下代码 ,开始-->
<sec:authorize access="isAuthenticated()">
<input type="hidden"
value="<sec:authentication property="principal.username" />">
</sec:authorize>
<!-- 用户登录后才显示如下代码,结束 -->
五.总结
1.调试代码要注意jsp中的HTML注释 <!-- 注释内容 --> 在编译时可见,即里面的内容如果有错误,编译器还是会报错,所以调试时直接删除才行,不然还是会有编译错误。
<!-- this is an html comment.it will show up int the response. -->
JSP页注释(隐藏注释):指注释虽然写在JSP程序中,但不会发送给客户,因此在客户端查看源代码时不能看见注释。这样的注释在JSP编译时被忽略掉。
<%--this is a JSP comment.it will only be seen in jsp code--%>
Java注释:只能出现在Java代码区中,不允许直接出现在页面中。//单行注释 /*多行注释*/