?? boardaction.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("<", "<");
reply.replaceAll(">", ">");
reply.replaceAll(" ", " ");
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 + -