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

📄 boardaction.java

📁 struts spring hibernate制作的留言本
💻 JAVA
字号:
package anni.gbook.springmvc;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import anni.gbook.BoardInfo;
import anni.gbook.IBoardDAO;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;


/**
 * springmvc的控制器.
 *
 * @author Lingo
 * @version 1.0
 * @since 2006-08-08
 */
public class BoardAction implements Controller {
    /**
     * 日志.
     */
    private Log log = LogFactory.getLog(BoardAction.class);

    /**
     * dao.
     */
    private IBoardDAO dao;

    /**
     * getter.
     * @return IBoardDAO dao
     */
    public final IBoardDAO getDao() {
        return dao;
    }

    /**
     * setter.
     * @param dao dao
     */
    public final void setDao(final IBoardDAO dao) {
        this.dao = dao;
    }

    /**
     * 分发所有请求,实际上与MutiActionController功能相似,但当时我还不会用.
     * @param request 请求
     * @param response 响应
     * @return ModelAndView mv
     * @throws ServletException servlet异常
     * @throws IOException io异常
     */
    public final ModelAndView handleRequest(
        final HttpServletRequest request,
        final HttpServletResponse response)
        throws ServletException, IOException {
        String method = request.getParameter("method");

        if ((method == null) || method.trim().equals("")) {
            method = "browse";
        }

        method = method.trim();

        if (method.equals("browse")) {
            return browse(request, response);
        } else if (method.equals("add")) {
            return add(request, response);
        } else if (method.equals("delete")) {
            return delete(request, response);
        } else if (method.equals("viewIp")) {
            return viewIp(request, response);
        } else if (method.equals("viewReply")) {
            return viewReply(request, response);
        } else if (method.equals("reply")) {
            return reply(request, response);
        } else {
            return browse(request, response);
        }
    }

    /**
     * 显示所有的留言.
     * @since 2006-08-08 10:59
     * @author Lingo
     * @param request 请求
     * @param response 响应
     * @return ModelAndView mv
     * @throws ServletException servlet异常
     * @throws IOException io异常
     */
    public final ModelAndView browse(final HttpServletRequest request,
        final HttpServletResponse response)
        throws ServletException, IOException {
        log.trace("browse start");

        List all = dao.findAll();
        //Collections.reverse(all);
        request.setAttribute("all", all);

        int count = all.size();

        String currentPage0 = request.getParameter("currentPage");
        log.trace("get currentPage from request : " + currentPage0);

        int currentPage = 0;

        try {
            currentPage = Integer.parseInt(currentPage0);
        } catch (NumberFormatException ex) {
            log.warn(ex.getMessage());
            log.warn("currentPage : " + currentPage);
        }

        if ((currentPage < 0) || (currentPage > (count / 6))) {
            currentPage = 0;
        }

        request.setAttribute("start", (currentPage * 6) + "");
        request.setAttribute("currentPage", currentPage + "");
        log.trace("currentPage : " + currentPage);

        //为form添加一个避免重复提交的标志
        //saveToken(request);
        log.trace("browse end - success");

        return new ModelAndView("browse.jsp");
    }

    /**
     * 添加一条信息.
     * 对重复提交进行了简单的处理,就是在jsp里生成一次性的标志
     * 判断,如果不是重复提交就进行处理,如果是重复提交,就跳转到错误页面
     * @author Lingo
     * @since 2006-08-08 12:32
     * @param request 请求
     * @param response 响应
     * @return ModelAndView mv
     * @throws ServletException servlet异常
     * @throws IOException io异常
     */
    public final ModelAndView add(final HttpServletRequest request,
        final HttpServletResponse response)
        throws ServletException, IOException {
        log.trace("start");

        //首先需要检测token,判断是否重复提交
        String token1 = request.getParameter("token");
        HttpSession session = request.getSession();
        String token2 = (String) session.getAttribute("token");

        if (!token1.equals(token2)) {
            //发生了重复提交
            return new ModelAndView("/token_error.jsp");
        } else {
            //没有发生重复提交
            //首先处理判断重复提交的标志
            session.setAttribute("token", "");
        }

        //先从form中取出各个字段的值
        //
        String name = request.getParameter("newname");
        String email = request.getParameter("newemail");
        String pageName = request.getParameter("newpagename");
        String pageUrl = request.getParameter("newpageurl");
        String sex0 = request.getParameter("sex"); //0-boy,1-girl
        String img = request.getParameter("img");
        String content = request.getParameter("newtext");

        //客户留言者IP
        String ip = request.getRemoteAddr();

        //对字段进行处理
        //
        if (pageName == null) {
            pageName = "";
        }

        if ((pageUrl == null) || pageUrl.equals("http://")) {
            pageUrl = "";
        } else {
            if (pageName.equals("")) {
                pageName = "homePage";
            }
        }

        try {
            name = new String(name.getBytes("ISO-8859-1"), "GB2312");
            pageName = new String(pageName.getBytes("ISO-8859-1"), "GB2312");
            content = new String(content.getBytes("ISO-8859-1"), "GB2312");
        } catch (UnsupportedEncodingException ex) {
            System.err.println(ex);
        }

        int sex = 0;

        try {
            sex = Integer.parseInt(sex0);
        } catch (NumberFormatException ex) {
            sex = 0;
        }

        //添加记录
        //
        BoardInfo info = new BoardInfo();
        info.setName(name);
        info.setEmail(email);
        info.setPageName(pageName);
        info.setPageUrl(pageUrl);
        info.setSex(new Integer(sex));
        info.setImg(img);
        info.setContent(content);
        info.setIp(ip);
        info.setDateTime(new Date());
        info.setReply("");

        dao.insert(info);

        return new ModelAndView("/index.jsp");
    }

    /**
     * 删除一条数据.
     * 不过里边没有判断id对应的数据是否存在
     * 如果你不是在浏览的时候手工修改了文件的数据的话,是不会出现这种情况的
     * @author Lingo
     * @since 2006-08-08 12:51
     * @param request 请求
     * @param response 响应
     * @return ModelAndView mv
     * @throws ServletException servlet异常
     * @throws IOException io异常
     */
    public final ModelAndView delete(final HttpServletRequest request,
        final HttpServletResponse response)
        throws ServletException, IOException {
        String inpass = request.getParameter("inpass");
        HttpSession session = request.getSession();
        String pass = (String) session.getAttribute("pass");

        if ((pass == null) | !pass.equals(inpass)) {
            return new ModelAndView("delete_pass_invalid.jsp");
        }

        String id0 = request.getParameter("id");

        //Log.info(id0);
        int id;

        try {
            id = Integer.parseInt(id0);
        } catch (NumberFormatException ex) {
            log.warn(ex.getMessage());
            id = 0;
        }

        //Log.info(id+"");
        if (dao.findById(new Integer(id)) == null) {
            return new ModelAndView("delete_id_error.jsp");
        }

        dao.delete(new Integer(id));

        return new ModelAndView("delete_success.jsp");
    }

    /**
     * 根据id查找相应用户的ip.
     * @author Lingo
     * @since 2006-08-08 12:55
     * @param request 请求
     * @param response 响应
     * @return ModelAndView mv
     * @throws ServletException servlet异常
     * @throws IOException io异常
     */
    public final ModelAndView viewIp(final HttpServletRequest request,
        final HttpServletResponse response)
        throws ServletException, IOException {
        String inpass = request.getParameter("inpass");
        HttpSession session = request.getSession();
        String pass = (String) session.getAttribute("pass");

        if ((pass == null) | !pass.equals(inpass)) {
            return new ModelAndView("viewip_pass_invalid.jsp");
        }

        String id0 = request.getParameter("id");

        //Log.info(id0);
        int id;

        try {
            id = Integer.parseInt(id0);
        } catch (NumberFormatException ex) {
            log.warn(ex.getMessage());
            id = 0;
        }

        //Log.info(id+"");
        BoardInfo info = dao.findById(new Integer(id));

        if (info == null) {
            return new ModelAndView("viewip_id_error.jsp");
        }

        request.setAttribute("id", String.valueOf(info.getId()));
        request.setAttribute("ip", info.getIp());

        return new ModelAndView("viewip_success.jsp");
    }

    /**
     * 根据id查找相应用户的reply.
     * @author Lingo
     * @since 2006-08-08 13:00
     * @param request 请求
     * @param response 响应
     * @return ModelAndView mv
     * @throws ServletException servlet异常
     * @throws IOException io异常
     */
    public final ModelAndView viewReply(final HttpServletRequest request,
        final HttpServletResponse response)
        throws ServletException, IOException {
        String id0 = request.getParameter("id");

        //Log.info(id0);
        int id;

        try {
            id = Integer.parseInt(id0);
        } catch (NumberFormatException ex) {
            log.warn(ex.getMessage());
            id = 0;
        }

        //Log.info(id+"");
        BoardInfo info = dao.findById(new Integer(id));

        if (info == null) {
            return new ModelAndView("reply_id_error.jsp");
        }

        String reply = info.getReply();

        request.setAttribute("id", String.valueOf(info.getId()));
        request.setAttribute("reply", reply);

        //如果回复信息中存在html标签,就说明上次没有对html标签进行转换
        //把上次对html设置的方式也保留到request中
        if (reply.indexOf("<") != -1) {
            request.setAttribute("html", "no");
        }

        return new ModelAndView("reply.jsp");
    }

    /**
     * 根据id回复相应用户的留言.
     * @author Lingo
     * @since 2006-01-02 23:00
     * @param request 请求
     * @param response 响应
     * @return ModelAndView mv
     * @throws ServletException servlet异常
     * @throws IOException io异常
     */
    public final ModelAndView reply(final HttpServletRequest request,
        final HttpServletResponse response)
        throws ServletException, IOException {
        String inpass = request.getParameter("inpass");
        HttpSession session = request.getSession();
        String pass = (String) session.getAttribute("pass");

        if ((pass == null) | !pass.equals(inpass)) {
            return new ModelAndView("reply_pass_invalid.jsp");
        }

        String id0 = request.getParameter("id");

        //Log.info(id0);
        int id;

        try {
            id = Integer.parseInt(id0);
        } catch (NumberFormatException ex) {
            log.warn(ex.getMessage());
            id = 0;
        }

        //Log.info(id+"");
        BoardInfo info = dao.findById(new Integer(id));

        if (info == null) {
            return new ModelAndView("reply_id_error.jsp");
        }

        String reply = request.getParameter("reply");

        try {
            reply = new String(reply.getBytes("ISO-8859-1"), "GB2312");
        } catch (UnsupportedEncodingException ex) {
            System.err.println(ex);
        }

        String html = request.getParameter("html");

        if (html.equals("yes")) {
            reply.replaceAll("<", "&lt;");
            reply.replaceAll(">", "&gt;");
            reply.replaceAll(" ", "&nbsp;");
            reply.replaceAll("\r\n", "<br>");
        }

        info.setReply(reply);

        dao.update(info);

        return new ModelAndView("reply_success.jsp");
    }
}

⌨️ 快捷键说明

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