PubNoteAction.java
上传用户:jishiqi_cj
上传日期:2022-08-08
资源大小:24765k
文件大小:3k
源码类别:

Java编程

开发平台:

Java

  1. package StudyNote;
  2. import javax.servlet.http.HttpServletRequest;
  3. import javax.servlet.http.HttpServletResponse;
  4. import org.apache.struts.action.Action;
  5. import org.apache.struts.action.ActionForm;
  6. import org.apache.struts.action.ActionForward;
  7. import org.apache.struts.action.ActionMapping;
  8. import javax.servlet.ServletContext;
  9. import javax.sql.DataSource;
  10. import java.sql.Connection;
  11. import java.sql.Statement;
  12. import java.sql.ResultSet;
  13. import java.sql.SQLException;
  14. import java.util.*;
  15. import javax.servlet.http.*;
  16. import org.apache.struts.validator.DynaValidatorForm;
  17. import org.apache.struts.action.ActionMessage;
  18. import org.apache.struts.action.ActionMessages;
  19. public final class PubNoteAction extends Action{  
  20. public ActionForward execute(
  21. ActionMapping mapping,
  22. ActionForm form,
  23. HttpServletRequest request,  
  24. HttpServletResponse response) throws Exception {
  25.     DynaValidatorForm noteForm = (DynaValidatorForm) form;         
  26. String title = (String)noteForm.get("title");
  27. String content = (String)noteForm.get("content");
  28. String talker = (String)noteForm.get("talker");
  29.         HttpSession session = request.getSession();
  30. Vector noteList = new Vector();
  31.     ServletContext context = servlet.getServletContext();
  32. DataSource dataSource = 
  33. (DataSource)context.getAttribute(Constants.DATASOURCE_KEY);
  34.         DB db = new DB(dataSource);
  35.         
  36. String PageForward;
  37. Note note=new Note();
  38. note.setTitle(title);
  39. note.setContent(content);
  40. note.setTalker(talker);
  41. ActionMessages errors = new ActionMessages();
  42.     String username=(String)session.getAttribute(Constants.LOGIN_USERNAME_KEY);
  43.         
  44. //避免重复提交
  45. if (!isTokenValid(request)){
  46. PageForward="toNoteMain";
  47.   saveToken(request);  
  48.   }
  49. else {
  50.   resetToken(request);  //避免重复提交
  51. if (username==null){
  52.              errors.add(ActionMessages.GLOBAL_MESSAGE,
  53.                            new ActionMessage("errors.accessDeny"));
  54. if (!errors.isEmpty()) {
  55. saveErrors(request, errors);
  56.   PageForward="toWrong";  
  57. }
  58.          else if (note.Insert(db)){
  59. noteList = Note.SearchNoteTitle(db,0);
  60. session.setAttribute(Constants.NOTE_LIST_KEY,noteList);
  61. PageForward="toNoteMain";
  62. }
  63. else{
  64.              errors.add(ActionMessages.GLOBAL_MESSAGE,
  65.                            new ActionMessage("errors.insertFail"));
  66. if (!errors.isEmpty()) {
  67. saveErrors(request, errors);
  68.   PageForward="toWrong";  
  69. }
  70. }
  71.    
  72. db.close();
  73.     return (mapping.findForward(PageForward));
  74.   }
  75. }