在做一個(gè)用Struts做的電子商務(wù)項(xiàng)目時(shí),碰到了一個(gè)這樣的問題:未登陸用戶購買商品時(shí),要顯示用戶登陸畫面;而登陸用戶購買商品時(shí),則不顯示登陸畫面而顯示用戶信息。可以用struts 1.1 的bean:PResent實(shí)現(xiàn):
在jsp頁面頭部一定要加上相應(yīng)的<%@ taglib 等標(biāo)記,否則無法實(shí)現(xiàn)。我開始時(shí)就是忘了加<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %> ,而導(dǎo)致無論是使用 present 還是 notPresent ,網(wǎng)頁中都顯示用戶登陸頁面。:)。在jsp頁面實(shí)現(xiàn)代碼:
<%@ page language="java"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-Html" prefix="html"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %>
<logic:notPresent name="userid" scope="session">
// 這里是登陸表單代碼
</logic:notPresent>
<logic:present name="userid" scope="session">
<bean:write key="userid" /> //顯示session中的用戶id (userid)
</logic:present>
在Struts的action的 execute 方法里面可以這樣定義session變量:
String userId="123456";
request.getSession(true).setAttribute("userid",userId);
(注:request.getSeesion(boolean),這個(gè)方法里面?zhèn)髁艘粋€(gè)boolean值,這個(gè)值假如是true,那么假如當(dāng)前的request的session不可用,那么就創(chuàng)建新的會話,假如存在就返回當(dāng)前的會話。假如參數(shù)是false,那么在request的當(dāng)前會話不存在的時(shí)候就返回null。)
新聞熱點(diǎn)
疑難解答
圖片精選