📄 boardaction.java
字号:
package anni.gbook.struts;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import anni.gbook.BoardInfo;
import anni.gbook.FileDAO;
import anni.gbook.IBoardDAO;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
/**
* 看啊,看啊,这个是用struts改造以后的action.
* 感觉怎么样?
* @since 2006-01-03 19:59
* @author Lingo
* @version 1.0
*/
public class BoardAction extends DispatchAction {
/**
* 日志.
*/
private Log log = LogFactory.getLog(BoardAction.class);
/**
* dao.
*/
private IBoardDAO dao;
/**
* 构造方法.
*/
public BoardAction() {
dao = FileDAO.getInstance();
}
/**
* 显示所有的留言.
* @since 2006-01-03 19:55
* @author Lingo
* @param mapping 转发请求的映射
* @param form ActionForm
* @param request 请求
* @param response 响应
* @return ActionForward 请求转发
* @throws Exception 可能抛出任何异常
*/
public final ActionForward browse(final ActionMapping mapping,
final ActionForm form, final HttpServletRequest request,
final HttpServletResponse response) throws Exception {
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 mapping.findForward("success");
}
/**
* 添加一条信息.
* 对重复提交进行了简单的处理,就是在jsp里生成一次性的标志
* 判断,如果不是重复提交就进行处理,如果是重复提交,就跳转到错误页面
* @since 2006-01-03 20:20
* @author Lingo
* @param mapping 转发请求的映射
* @param form ActionForm
* @param request 请求
* @param response 响应
* @return ActionForward 请求转发
* @throws Exception 可能抛出任何异常
*/
public final ActionForward add(final ActionMapping mapping,
final ActionForm form, final HttpServletRequest request,
final HttpServletResponse response) throws Exception {
log.trace("add start");
//检测是否发生重复提交
if (isTokenValid(request)) {
//销毁当前的标志
resetToken(request);
} else {
//如果是重复提交,跳转到报错页面
log.warn("token error");
return mapping.findForward("token_error");
}
/*
//首先需要检测token,判断是否重复提交
String token1 = request.getParameter("token");
HttpSession session = request.getSession();
String token2 = (String)session.getAttribute("token");
if(!token1.equals(token2))
{
//发生了重复提交
log.warn("token error");
return mapping.findForward("token_error");
//return "/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");
*/
BoardForm boardForm = (BoardForm) form;
String name = boardForm.getName();
String email = boardForm.getEmail();
String pageName = boardForm.getPageName();
String pageUrl = boardForm.getPageUrl();
int sex = boardForm.getSex();
String img = boardForm.getImg();
String content = boardForm.getContent();
//客户留言者IP
String ip = request.getRemoteAddr();
//对字段进行处理
//
if (pageName == null) {
pageName = "";
}
if ((pageUrl == null) || pageUrl.equals("http://")) {
pageUrl = "";
} else {
if (pageName.equals("")) {
pageName = "homePage";
}
}
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");
/*
int sex=0;
try
{
sex = Integer.parseInt(sex0);
}
catch (NumberFormatException ex)
{
log.warn(ex.getMessage());
sex = 0;
}
*/
//添加记录
//
BoardInfo info = new BoardInfo();
info.setName(name);
info.setEmail(email);
info.setPageName(pageName);
info.setPageUrl(pageUrl);
info.setSex(sex);
info.setImg(img);
info.setContent(content);
info.setIp(ip);
info.setDateTime(new Date());
info.setReply("");
dao.insert(info);
log.trace("add end - insert_success");
return mapping.findForward("insert_success");
}
/**
* 删除一条数据.
* 首先判断id对应的数据是否存在
* 如果你不是在浏览的时候手工修改了文件的数据的话,是不会出现这种情况的
* @since 2006-01-03 20:29
* @author Lingo
* @param mapping 转发请求的映射
* @param form ActionForm
* @param request 请求
* @param response 响应
* @return ActionForward 请求转发
* @throws Exception 可能抛出任何异常
*/
public final ActionForward delete(final ActionMapping mapping,
final ActionForm form, final HttpServletRequest request,
final HttpServletResponse response) throws Exception {
log.trace("delete start");
String inpass = request.getParameter("inpass");
HttpSession session = request.getSession();
String pass = (String) session.getAttribute("pass");
if ((pass == null) | !pass.equals(inpass)) {
log.warn("delete_pass_invalid");
return mapping.findForward("delete_pass_invalid");
}
String id0 = request.getParameter("id");
log.trace("get id from request : " + id0);
int id;
try {
id = Integer.parseInt(id0);
} catch (NumberFormatException ex) {
log.warn(ex.getMessage());
id = 0;
}
log.trace(id + "");
if (dao.findById(id) == null) {
log.warn("delete_id_error");
return mapping.findForward("delete_id_error");
}
dao.delete(id);
log.trace("delete end - delete_success");
return mapping.findForward("delete_success");
}
/**
* 根据id查找相应用户的ip.
* @since 2006-01-03 20:36
* @author Lingo
* @param mapping 转发请求的映射
* @param form ActionForm
* @param request 请求
* @param response 响应
* @return ActionForward 请求转发
* @throws Exception 可能抛出任何异常
*/
public final ActionForward viewIp(final ActionMapping mapping,
final ActionForm form, final HttpServletRequest request,
final HttpServletResponse response) throws Exception {
log.trace("viewIp start");
String inpass = request.getParameter("inpass");
HttpSession session = request.getSession();
String pass = (String) session.getAttribute("pass");
if ((pass == null) | !pass.equals(inpass)) {
log.warn("viewip_pass_invalid");
return mapping.findForward("viewip_pass_invalid");
//return "/viewip_pass_invalid.jsp";
}
String id0 = request.getParameter("id");
log.trace("get id from request : " + id0);
int id;
try {
id = Integer.parseInt(id0);
} catch (NumberFormatException ex) {
log.warn(ex.getMessage());
id = 0;
}
log.trace(id + "");
BoardInfo info = dao.findById(id);
if (info == null) {
log.warn("viewip_id_error");
return mapping.findForward("viewip_id_error");
}
request.setAttribute("id", String.valueOf(info.getId()));
request.setAttribute("ip", info.getIp());
log.trace("viewIp end - viewip_success");
return mapping.findForward("viewip_success");
}
/**
* 根据id查找相应用户的reply.
* @since 2006-01-03 20:44
* @author Lingo
* @param mapping 转发请求的映射
* @param form ActionForm
* @param request 请求
* @param response 响应
* @return ActionForward 请求转发
* @throws Exception 可能抛出任何异常
*/
public final ActionForward viewReply(final ActionMapping mapping,
final ActionForm form, final HttpServletRequest request,
final HttpServletResponse response) throws Exception {
log.trace("viewReply start");
String id0 = request.getParameter("id");
log.trace("get id from request : " + id0);
int id;
try {
id = Integer.parseInt(id0);
} catch (NumberFormatException ex) {
log.warn(ex.getMessage());
id = 0;
}
log.trace("" + id);
BoardInfo info = dao.findById(id);
if (info == null) {
log.warn("reply_id_error");
return mapping.findForward("reply_id_error");
}
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");
}
log.trace("viewReply end - view_reply_success");
return mapping.findForward("view_reply_success");
}
/**
* 根据id回复相应用户的留言.
* @since 2006-01-03 20:50
* @author Lingo
* @param mapping 转发请求的映射
* @param form ActionForm
* @param request 请求
* @param response 响应
* @return ActionForward 请求转发
* @throws Exception 可能抛出任何异常
*/
//public String reply(HttpServletRequest request)
public final ActionForward reply(final ActionMapping mapping,
final ActionForm form, final HttpServletRequest request,
final HttpServletResponse response) throws Exception {
log.trace("reply start");
String inpass = request.getParameter("inpass");
HttpSession session = request.getSession();
String pass = (String) session.getAttribute("pass");
if ((pass == null) | !pass.equals(inpass)) {
log.warn("reply_pass_invalid");
return mapping.findForward("reply_pass_invalid");
}
String id0 = request.getParameter("id");
log.trace("get id from request : " + id0);
int id;
try {
id = Integer.parseInt(id0);
} catch (NumberFormatException ex) {
log.warn(ex.getMessage());
id = 0;
}
log.trace(id + "");
BoardInfo info = dao.findById(id);
if (info == null) {
log.warn("reply_id_error");
return mapping.findForward("reply_id_error");
}
String reply = request.getParameter("reply");
reply = new String(reply.getBytes("ISO-8859-1"), "GB2312");
String html = request.getParameter("html");
if (html.equals("yes")) {
reply.replaceAll("<", "<");
reply.replaceAll(">", ">");
reply.replaceAll(" ", " ");
reply.replaceAll("\r\n", "<br>");
}
info.setReply(reply);
dao.update(info);
log.trace("reply end reply_success");
return mapping.findForward("reply_success");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -