⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 post.jsp

📁 Struts+Spring+Hibernate开发的BBS,功能很强大很完善
💻 JSP
📖 第 1 页 / 共 2 页
字号:
<%@ page contentType="text/html;charset=ISO8859_1" %>
<jsp:useBean id="myEnv" scope="session" class="com.jdon.web.UserEnvFront"/>
<jsp:setProperty name="myEnv" property="*"/>
<%
/**
 *	$RCSfile: post.jsp,v $
 *	$Revision: 1.3 $
 *	$Date: 2002/10/23 14:36:14 $
 */
%>

<%@ page import="java.net.*,
                 java.text.*,
                 java.util.*,
                 com.jivesoftware.util.*,
                 com.jivesoftware.forum.*,
                 com.jivesoftware.forum.util.*"
    errorPage="error.jsp"
%>

<%! // Formatter for short dates
    static final SimpleDateFormat shortDateFormatter =
            new SimpleDateFormat("MMM, yyyy", JiveGlobals.getLocale());
%>

<%  // Login code. Usually the following code (below the parameter code) is in
    // global.jsp, but on this page we're not including global.jsp so we can
    // have better control over the way UnauthorizedExceptions are handled.

    // First, get the message parameters because we might be entering this page
    // after submitting the message form to post a message.
    long forumID = ParamUtils.getLongParameter(request,"forum",-1L);
	long threadID = ParamUtils.getLongParameter(request,"thread",-1L);
    long messageID = ParamUtils.getLongParameter(request,"message",-1L);
    boolean reply = ParamUtils.getBooleanParameter(request,"reply");
    boolean doPost = ParamUtils.getBooleanParameter(request,"doPost");
    boolean fromPreview = ParamUtils.getBooleanParameter(request,"fromPreview");
    boolean quote = ParamUtils.getBooleanParameter(request,"quote");
    String subject = ParamUtils.getParameter(request,"subject");
    String body = ParamUtils.getParameter(request,"body");
    String name = ParamUtils.getParameter(request,"name");
    String email = ParamUtils.getParameter(request,"email");
    // Special parameters to tell us whether the user was logged in or not
    boolean wasLoggedIn = ParamUtils.getBooleanParameter(request, "isLoggedIn");
	////added by Cherami
    final int BODYLENGTH=45000;
	////
    // Check to see if a Jive authorization token exists. Wrap that check in a
    // try/catch block so we can correctly redirect to login if necessary
    Authorization authToken = null;
    try {
        authToken = SkinUtils.getUserAuthorization(request, response);
        if (doPost && wasLoggedIn && authToken == null) {
            // This case means the session died or the appserver restarted and
            // wiped the session data. Throw an UnauthorizedException so we kick
            // the user to the login page. After they login, they'll redirect
            // to this page.
            throw new UnauthorizedException("用户已登录但是授权标记丢失。");
        }
    }
    catch (UnauthorizedException ue) {

        // Redirect to login.jsp. If the subject & body are not null, post those
        // in the session.
        if (subject != null) {
            session.setAttribute("jive.post.subject."+forumID+"."+threadID, subject);
        }
        if (body != null) {
            session.setAttribute("jive.post.body."+forumID+"."+threadID, body);
        }

		// Next construct the login.jsp referrer (the link it will redirect to)
        StringBuffer url = new StringBuffer("post.jsp");
        url.append("?forum=").append(forumID);
        url.append("&thread=").append(threadID);
        url.append("&message=").append(messageID);
        url.append("&reply=").append(reply);
        url.append("&fromPreview=").append(true);     // XXX always set to true?
        url.append("&quote=").append(quote);
        url.append("&doPost=").append(doPost);
        if (name != null) { url.append("&name=").append(name); }
        if (email != null) { url.append("&email=").append(email); }
        // Do the redirect
        String redirect = "login.jsp?unauth=true&referrer=" + URLEncoder.encode(url.toString());
        response.sendRedirect(redirect);
        return;
    }
    // If authToken is null, make an anonymous login:
    if (authToken == null) {
        authToken = AuthorizationFactory.getAnonymousAuthorization();
    }
    boolean isGuest = authToken.isAnonymous();
    // Get the forum factory object.
    ForumFactory forumFactory = ForumFactory.getInstance(authToken);
    // Get the user of this page
    User pageUser = null;
    if (!isGuest) {
	    pageUser = myEnv.getForumFactory().getUserManager().getUser(authToken.getUserID());
    }

    boolean autoWatchNewTopics = false;
    boolean autoWatchReplies = false;
    boolean addWatch = ParamUtils.getBooleanParameter(request,"addWatch");
    if (!isGuest) {
        autoWatchNewTopics = "true".equals(pageUser.getProperty("jiveAutoWatchNewTopics"));
        autoWatchReplies = "true".equals(pageUser.getProperty("jiveAutoWatchReplies"));
    }
    boolean wasAutoWatchReplies = ParamUtils.getBooleanParameter(request,"wasAutoWatchReplies");
    if (reply && (addWatch != wasAutoWatchReplies)) {
        autoWatchReplies = addWatch;
    }
    boolean wasAutoWatchNewTopics = ParamUtils.getBooleanParameter(request,"wasAutoWatchNewTopics");
    if (!reply && (addWatch != wasAutoWatchNewTopics)) {
        autoWatchNewTopics = addWatch;
    }

    // The last time the user visited this page
    Date lastVisited = new Date(SkinUtils.getLastVisited(request,response));

    // The following is the rest of the normal post logic

    String formButton = ParamUtils.getParameter(request,"formButton");
    boolean preview = doPost && ("预 览".equals(formButton));

    // Grab the subject & body from the session if we're coming from
    // a message preview (these values can't be passed as parameters)
	if (fromPreview) {
 	    subject = (String)session.getAttribute("jive.post.subject."+forumID+"."+threadID);
        body = (String)session.getAttribute("jive.post.body."+forumID+"."+threadID);
	}

    // Error variables
	boolean errors = false;
	String errorMessage = "";

	// Error checks

    // Check to see the subject & body are not blank
	if (doPost && (subject == null)) {
 	    errors = true;
		errorMessage = "请输入标题!";
    }
	else if (doPost && (body == null)) {
 	    errors = true;
        errorMessage = "对不起,您不可以发表空信息,请重新输入!";
	}

    // Load the forum we're posting in.
    Forum forum = myEnv.getForumFactory().getForum(forumID);

    // Security check. Determine if this user can post new messages or reply to
    // threads
    boolean login = false;
    if (reply) {
        if (!forum.hasPermission(ForumPermissions.CREATE_MESSAGE)
                && !myEnv.getForumFactory().hasPermission(ForumPermissions.CREATE_MESSAGE))
        {
            if (isGuest) {
                login = true;
            } else {
                throw new UnauthorizedException("没有在此论坛发表回复的权限");
            }
        }
    }
    // check to see if this use can create new topics
    else {
        if (!forum.hasPermission(ForumPermissions.CREATE_THREAD)
                && !myEnv.getForumFactory().hasPermission(ForumPermissions.CREATE_THREAD))
        {
            if (isGuest) {
                login = true;
            } else {
                throw new UnauthorizedException("没有在此论坛发表新主题的权限");
            }
        }
    }
    if (login) {
        // If we need to login, send the referrer along as a link to this page
        StringBuffer postLink = new StringBuffer("post.jsp?forum=").append(forumID);
        if (threadID != -1L) {
            postLink.append("&thread=").append(threadID);
        }
        if (messageID != -1L) {
            postLink.append("&message=").append(messageID);
        }
        if (reply) {
            postLink.append("&reply=").append(reply);
        }
        response.sendRedirect("login.jsp?unauth=true&referrer=" +
            java.net.URLEncoder.encode(postLink.toString()));
        return;
    }

    // Check if we need to do a message preview
    if (preview && !errors) {
        // Put the message subject & body in the session
        session.setAttribute("jive.post.subject."+forumID+"."+threadID, subject);
        session.setAttribute("jive.post.body."+forumID+"."+threadID, body);

        // Redirect to the preview page:
        response.sendRedirect(
            "preview.jsp?forum=" + forumID + "&thread=" + threadID + "&message="
            + messageID + "&reply=" + reply
            + "&name=" + ((name!=null)?URLEncoder.encode(name):"")
            + "&email=" + ((email!=null)?URLEncoder.encode(email):"")
        );
        return;
    }

    // Load the thread we're posting in (if this is a reply).
	ForumThread thread = null;
    // The parent message we're replying to (if this is a reply)
	ForumMessage parentMessage = null;
	if (reply) {
     	thread = forum.getThread(threadID);
        // a message ID might not be passed in. If this is the case, we're
        // replying to the root message so get the message ID from the root
        // message of the thread
        if (messageID == -1L) {
            parentMessage = thread.getRootMessage();
            messageID = parentMessage.getID();
        } else {
            parentMessage = thread.getMessage(messageID);
        }
	}

    // Determine if moderation is turned on for this forum
    boolean isThreadModOn = (
        forum.getModerationDefaultThreadValue()
        < forum.getModerationMinThreadValue()
    );
    boolean isMsgModOn = (
        forum.getModerationDefaultMessageValue()
        < forum.getModerationMinMessageValue()
    );

    // A user manager lets us get and set user information
    UserManager userManager = myEnv.getForumFactory().getUserManager();

    // The new message we're creating
    ForumMessage newMessage = null;

    // Do a message preview. For this, we populate a blank ForumMessage object.
    // We don't add it to the thread though.
	if (doPost) {
		// Create a new message/thread
		if (!errors) {
			// Create a new message object
            if (isGuest) {
			    newMessage = myEnv.getForumFactory().createMessage();
            } else {
			    newMessage = myEnv.getForumFactory().createMessage(pageUser);
            }
			newMessage.setSubject(subject);
			////modified by Cherami
			if (body.length()>BODYLENGTH) {
				newMessage.setBody(body.substring(0,BODYLENGTH));
				System.out.println("内容超长,截断");
			}
			else {
			    newMessage.setBody(body);
			}
			////source code
			////newMessage.setBody(body);
			//
            // set message properties if this is a guest posting
            if (isGuest) {
                if (name != null) { newMessage.setProperty("name", name); }
                if (email != null) { newMessage.setProperty("email", email); }
            }
			// if this is a reply, add it to the thread
			if (reply) {
				thread.addMessage(parentMessage,newMessage);
			}
			else {
				// it is a new posting
				forum.addThread(myEnv.getForumFactory().createThread(newMessage));
                thread = newMessage.getForumThread();
			}
            // Get the user's watch preferences:
            if (pageUser != null) {
                boolean autoAddEmailWatch = "true".equals(pageUser.getProperty("jiveAutoAddEmailWatch"));
                // Check if we need to add a watch to this message
                if (autoWatchNewTopics || (reply && autoWatchReplies)) {
                    WatchManager watchManager = myEnv.getForumFactory().getWatchManager();
                    watchManager.createWatch(pageUser, thread, WatchManager.NORMAL_WATCH);
                    if (autoAddEmailWatch) {
                        watchManager.createWatch(pageUser, thread, WatchManager.EMAIL_NOTIFY_WATCH);
                    }
                }
            }

            session.removeAttribute("jive.post.subject."+forumID+"."+threadID);
            session.removeAttribute("jive.post.body."+forumID+"."+threadID);
   			// Figure out where we need to redirect to.
            String redirect = "thread.jsp?forum="+forumID+"&thread="+thread.getID();
            // Do a standard redirect back to the new thread page if we're
            // not replying to a message
            if (!reply) {
                // However, if thread moderation is turned on, we shoud just
                // redirect back to the forum's thread listing:
                if (isThreadModOn) {
                    response.sendRedirect("forum.jsp?forum="+forumID);
                    return;
                }
                else {
                    // Standard redirect back to the new thread page
                    response.sendRedirect(redirect);
                    return;
                }
            }
            // Otherwise, we need to redirect back to the new posted reply.
            // Compute the correct thread page to go to.
            else {
                // However, if message moderation is turned on, we should just
                // redirect back to the top of the thread listing
                if (isMsgModOn) {
                    response.sendRedirect("thread.jsp?forum=" + forumID
                        + "&thread=" + threadID);
                    return;
                }
                else {
                    // the ID of the newly created message
                    long newMessageID = newMessage.getID();
                    // the number of threads a user wants to display on a topic page
                    int threadRange = myEnv.du.getThreadRange(request, response, pageUser);
                    // the number of messages in this thread
                    int messageCount = thread.getMessageCount();
                    // number of pages in the topic listing (we'll want to be at
                    // the end of that since new messages end up at the end of the
                    // thread list)
                    int numPages = (int)Math.ceil((double)messageCount/(double)threadRange);
                    // starting point:
                    int start = (numPages-1) * threadRange;
                    // do the redirect:
        			response.sendRedirect(
                        "thread.jsp?forum=" + forumID + "&thread=" + thread.getID()
                        + "&start=" + start + "&msRange=" + threadRange
                    );
                    return;
                }
            }
		}
    }
%>

<%  String title = "论坛 - 发布消息 " + forum.getName(); %>
<%@ include file="header.jsp" %>

<table cellpadding="0" cellspacing="0" border="0" width="100%">
<tr>
    <td valign="top" width="98%">
    <font face="<%= JiveGlobals.getJiveProperty("skin.default.fontFace") %>" color="<%= JiveGlobals.getJiveProperty("skin.default.linkColor") %>">
    <%  if (reply) { %>
        <b>回复</b>
    <%  } else { %>
        <b>发新贴</b>
    <%  } %>
    </font>
    <br>

    <%-- Breadcrumbs --%>

    <font size="-1" face="<%= JiveGlobals.getJiveProperty("skin.default.fontFace") %>" color="<%= JiveGlobals.getJiveProperty("skin.default.linkColor") %>">
    <b>
    <a href="<%= JiveGlobals.getJiveProperty("skin.default.homeURL") %>"
    >首页</a>
    &raquo;
    <a href="index.jsp" title="返回论坛列表"
    >论坛</a>
    &raquo;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -