| <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd"> <struts-config> </struts-config> |
![]() 图 16 Struts配置文件维护对话框 |
![]() 图 17 两个Struts配置文件 |
![]() 图 18 选择不同的配置文件 |
| String bookId;//图书ID,对应T_BOOK表的BOOK_ID,是主键。 String isbn;//isbn String createDate;//创建日期 String bookName;//书名 String author;//作者 |
| 1. package bookstore; 2. 3. import javax.servlet.http.HttpServletRequest; 4. import org.apache.struts.action.*; 5. import java.sql.*; 6. 7. public class BookActionForm 8. extends ActionForm { 9. … 10. public ActionErrors validate(ActionMapping actionMapping, 11. HttpServletRequest httpServletRequest) { 12. ActionErrors errors = new ActionErrors(); 13. Connection conn = null; 14. try { 15. conn = DBConnection.getConnection(); 16. PreparedStatement pStat = conn.prepareStatement( 17. "select count(*) count from T_BOOK where BOOK_ID=?"); 18. pStat.setString(1, this.bookId); 19. ResultSet rs = pStat.executeQuery(); 20. if (rs.next()&& rs.getInt(1) > 0) { 21. errors.add("bookId ", 22. new ActionMessage("bookstore.duplicate.bookId", 23. "图书ID和数据库中已经有的ID重复")); 24. } 25. } 26. catch (SQLException se) { 27. se.printStackTrace(); 28. errors.add("bookId", 29. new ActionMessage("bookstore.dbaccess.error", "访问数据库时出错")); 30. } 31. finally { 32. try { 33. if (conn != null) { 34. conn.close(); 35. } 36. } 37. catch (SQLException ex) { 38. ex.printStackTrace(); 39. errors.add("bookId", 40. new ActionMessage("bookstore.dbaccess.error", 41. "访问数据库时出错")); 42. } 43. } 44. return errors; 45. } 46. 47. public void reset(ActionMapping actionMapping, 48. HttpServletRequest servletRequest) { 49. this.createDate = getCurrDateStr(); 50. } 51. 52. //获取当前时间字符 53. private static String getCurrDateStr() { 54. SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); 55. return sdf.format(new Date()); 56. } 57. } |