struts2.1.6 bbs 07

<default-action-ref name="Category_list"/>

http://localhost:8080/Struts2_30_BBS2011_02  默認執行的action


  1. 登陸 Login.jsp  
  2. 註冊 Register.jsp  
  3. 查詢用戶信息 SelectUserInfo.jsp  
  4. 添加用戶信息 AddUserInfo.jsp  
  5. 刪除用戶信息 DelUserInfo.jsp  
  6. 修改用戶信息 UpdateUserInfo.jsp  
  7. ..........   
  8.   
  9. 登陸 Login  
  10. 註冊 Register  
  11. 用戶 User  
  12. 創建 Create  
  13. 修改 Update  
  14. 刪除 Delete  
  15. 查詢 Selete  
  16. 控制器 Controller  
  17. 用戶名 Username  
  18. 密碼 Password  
  19.   
  20. 文件名自己組合,比如登陸頁面Login.jsp,修改用戶信息頁面UpdateUser.jsp或Update.jsp,諸如此類  


  1. 1.         讀doc文檔:struts-tags  
  2.   
  3. 2.         設計約定(編碼規定)  
  4.   
  5. a)         原則:簡單就是美  
  6.   
  7. b)         庫名:項目名  
  8.   
  9. c)         表的命名:_Model名  
  10.   
  11. d)         字段:保持和屬性名一致(儘量不要起名和數據庫命名衝突)  
  12.   
  13. e)         用層來劃分包com.bjsxt.bbs.action model(bean) service dto(vo)  
  14.   
  15. f)          Action XXXXAction  
  16.   
  17. g)         *-*  
  18.   
  19. h)         /  
  20.   
  21. i)           /admin  
  22.   
  23. j)           package “action” adminAction  
  24.   
  25. 項目開發順序-以BBS2009的名義  
  26. 1.         建立界面原型  
  27.   
  28. 2.         建立Struts.xml  
  29.   
  30. a)         確定namespace  
  31.   
  32. b)         確定package  
  33.   
  34. c)         確定Action的名稱,空的方法  
  35.   
  36. d)         確定Result  
  37.   
  38. e)         將界面原型頁面進行修改,匹配現有設置  
  39.   
  40. f)          測試  
  41.   
  42. g)         做好規劃!!!!!  
  43.   
  44. 3.         建立數據庫(或者實體類)  
  45.   
  46. 4.         建立Model層  
  47.   
  48. 5.         建立Service層(後面講了Hibernate後再完善)  
  49.   
  50. a)         此時可以使用JUnit進行單元測試了  
  51.   
  52. 6.         着手開發  



  1. <default-action-ref name="index"/>  
  2.   
  3. <action name="*_*" class="com.gz.bbs2011.action.{1}Action" method="{2}">  
這兩個不能一起用,有bug
  1. package com.gz.bbs2011.util;  
  2.   
  3. import java.sql.Connection;  
  4. import java.sql.DriverManager;  
  5. import java.sql.PreparedStatement;  
  6. import java.sql.ResultSet;  
  7. import java.sql.SQLException;  
  8.   
  9.   
  10. public class DB {  
  11.     public static Connection createConnection() {  
  12.         Connection conn = null;  
  13.         try {  
  14.             Class.forName("com.mysql.jdbc.Driver");  
  15.             conn = DriverManager.getConnection("jdbc:mysql://localhost/bbs2011""root""root");  
  16.         } catch (ClassNotFoundException e) {  
  17.             e.printStackTrace();  
  18.         } catch (SQLException e) {  
  19.             e.printStackTrace();  
  20.         }  
  21.         return conn;  
  22.     }  
  23.   
  24.     public static PreparedStatement prepare(Connection conn, String sql) {  
  25.         PreparedStatement pstm = null;  
  26.         try {  
  27.             pstm = conn.prepareStatement(sql);  
  28.         } catch (SQLException e) {  
  29.             e.printStackTrace();  
  30.         }  
  31.         return pstm;  
  32.     }  
  33.     public static void close(Connection conn) {  
  34.         try {  
  35.             conn.close();  
  36.             conn = null//設爲空 垃圾收集器馬上可以回收  
  37.         } catch (SQLException e) {  
  38.             e.printStackTrace();  
  39.         }  
  40.     }  
  41.     public static void close(PreparedStatement pstm) {  
  42.         try {  
  43.             pstm.close();  
  44.             pstm = null;  
  45.         } catch (SQLException e) {  
  46.             e.printStackTrace();  
  47.         }  
  48.     }  
  49.     public static void close(ResultSet rs) {  
  50.         try {  
  51.             rs.close();  
  52.             rs = null;  
  53.         } catch (SQLException e) {  
  54.             e.printStackTrace();  
  55.         }  
  56.     }  
  57. }  

  1. package com.gz.bbs2011.model;  
  2.   
  3. public class Category {  
  4.     private int id;  
  5.     private String name;  
  6.     private String description;  
  7.     public int getId() {  
  8.         return id;  
  9.     }  
  10.     public void setId(int id) {  
  11.         this.id = id;  
  12.     }  
  13.     public String getName() {  
  14.         return name;  
  15.     }  
  16.     public void setName(String name) {  
  17.         this.name = name;  
  18.     }  
  19.     public String getDescription() {  
  20.         return description;  
  21.     }  
  22.     public void setDescription(String description) {  
  23.         this.description = description;  
  24.     }  
  25.       
  26. }  

  1. package com.gz.bbs2011.action;  
  2. import java.util.List;  
  3. import com.gz.bbs2011.model.Category;  
  4. import com.gz.bbs2011.service.CategoryService;  
  5. import com.opensymphony.xwork2.ActionSupport;  
  6.   
  7. public class CategoryAction extends ActionSupport{  
  8.     private List<Category> categories;  
  9.     private CategoryService categoryService = new CategoryService();  
  10.     private Category category;  
  11.     private int id;  
  12.       
  13.     public String list() {  
  14.         categories = categoryService.list();  
  15.         return SUCCESS;   
  16.     }  
  17.     public String add() {  
  18.         categoryService.add(category);  
  19.         return SUCCESS;  
  20.     }  
  21.     public String delete() {  
  22.         categoryService.deleteById(id);     
  23. //      categoryService.delete(category);     
  24.         return SUCCESS;  
  25.     }  
  26.     public String update() {  
  27.         categoryService.update(category);  
  28.         return SUCCESS;  
  29.     }  
  30.     public String addInput() {  
  31.         return INPUT ;  
  32.     }  
  33.     public String updateInput() {  
  34.         this.category = this.categoryService.loadById(id);  
  35.         return INPUT;  
  36.     }  
  37.     public List<Category> getCategories() {  
  38.         return categories;  
  39.     }  
  40.     public void setCategories(List<Category> categories) {  
  41.         this.categories = categories;  
  42.     }  
  43.     public CategoryService getCategoryService() {  
  44.         return categoryService;  
  45.     }  
  46.     public void setCategoryService(CategoryService categoryService) {  
  47.         this.categoryService = categoryService;  
  48.     }  
  49.     public Category getCategory() {  
  50.         return category;  
  51.     }  
  52.     public void setCategory(Category category) {  
  53.         this.category = category;  
  54.     }  
  55.     public int getId() {  
  56.         return id;  
  57.     }  
  58.     public void setId(int id) {  
  59.         this.id = id;  
  60.     }  
  61.       
  62. }  

  1. package com.gz.bbs2011.service;  
  2.   
  3. import java.sql.Connection;  
  4. import java.sql.PreparedStatement;  
  5. import java.sql.ResultSet;  
  6. import java.sql.SQLException;  
  7. import java.util.ArrayList;  
  8. import java.util.List;  
  9.   
  10. import com.gz.bbs2011.model.Category;  
  11. import com.gz.bbs2011.util.DB;  
  12.   
  13. public class CategoryService {  
  14.     public void add(Category c) {  
  15.         Connection conn = DB.createConnection();  
  16.         String sql = "insert into _category values(null,?,?)";  
  17.         PreparedStatement pstm = null;  
  18.         try {  
  19.             pstm = DB.prepare(conn, sql);  
  20.             pstm.setString(1, c.getName());  
  21.             pstm.setString(2, c.getDescription());  
  22.             pstm.executeUpdate();  
  23.               
  24.         } catch (SQLException e) {  
  25.             e.printStackTrace();  
  26.         }  
  27.         DB.close(conn);  
  28.         DB.close(pstm);  
  29.     }  
  30.     public List<Category> list() {  
  31.         Connection conn = DB.createConnection();  
  32.         String sql = "select * from _category";  
  33.         PreparedStatement pstm = DB.prepare(conn, sql);  
  34.         List<Category> list = new ArrayList<Category>();  
  35.         try {  
  36.             ResultSet rs = pstm.executeQuery();  
  37.             Category c = null;  
  38.             while(rs.next()) {  
  39.                 c = new Category();  
  40.                 c.setId(rs.getInt("id"));  
  41.                 c.setName(rs.getString("name"));  
  42.                 c.setDescription(rs.getString("description"));  
  43.                 list.add(c);  
  44.             }  
  45.         } catch (SQLException e) {  
  46.             e.printStackTrace();  
  47.         }  
  48.         DB.close(pstm);  
  49.         DB.close(conn);  
  50.         return list;  
  51.     }  
  52.     public void delete(Category c) {  
  53.         deleteById(c.getId());  
  54.     }  
  55.     public void deleteById(int id) {  
  56.         Connection conn = DB.createConnection();  
  57.         String sql = "delete from _category where id=?";  
  58.         PreparedStatement pstm = null;  
  59.         try {  
  60.             pstm = DB.prepare(conn, sql);  
  61.             pstm.setInt(1, id);  
  62.             pstm.executeUpdate();  
  63.         } catch (SQLException e) {  
  64.             e.printStackTrace();  
  65.         }  
  66.         DB.close(pstm);  
  67.         DB.close(conn);  
  68.           
  69.     }  
  70.       
  71.     public void update(Category c) {  
  72.         Connection conn = DB.createConnection();  
  73.         String sql = "update _category set name=?,description=? where id=?";  
  74.         PreparedStatement pstm = null;  
  75.         try {  
  76.             pstm = DB.prepare(conn, sql);  
  77.             pstm.setString(1, c.getName());  
  78.             pstm.setString(2, c.getDescription());  
  79.             pstm.setInt(3, c.getId());  
  80.             pstm.executeUpdate();  
  81.         } catch (SQLException e) {  
  82.             e.printStackTrace();  
  83.         }  
  84.         DB.close(pstm);  
  85.         DB.close(pstm);  
  86.     }  
  87.     public Category loadById(int id) {  
  88.         Connection conn = DB.createConnection();  
  89.         String sql = "select * from _category where id=?";  
  90.         PreparedStatement pstm = DB.prepare(conn, sql);  
  91.         ResultSet rs = null;  
  92.         Category c = null;  
  93.         try {  
  94.             pstm.setInt(1, id);  
  95.             rs = pstm.executeQuery();  
  96.             if(rs.next()) {  
  97.                 c = new Category();  
  98.                 c.setId(rs.getInt("id"));  
  99.                 c.setName(rs.getString("name"));  
  100.                 c.setDescription(rs.getString("description"));  
  101.             }  
  102.               
  103.         } catch (SQLException e) {  
  104.             e.printStackTrace();  
  105.         }  
  106.         DB.close(pstm);  
  107.         DB.close(conn);  
  108.         return c;  
  109.     }  
  110. }  

  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5.   
  6. <struts>  
  7.         <!--   
  8.       
  9.     <package name="bbs2009_default" extends="struts-default">  
  10.          <global-exception-mappings>  
  11.             <exception-mapping result="exception_handle" exception="Exception"></exception-mapping>  
  12.         </global-exception-mappings>  
  13.     </package>  
  14.        -->   
  15.          
  16.          
  17.     <package name="admin" namespace="/admin" extends="struts-default" >  
  18.           
  19.   
  20.         <action name="index">  
  21.             <result>/admin/index.html</result>  
  22.         </action>  
  23.           
  24.       
  25.        <action name="*_*" class="com.gz.bbs2011.action.{1}Action" method="{2}">  
  26.             <result>/admin/{1}_{2}.jsp</result>  
  27.             <result name="input">/admin/{1}_{2}.jsp</result>  
  28.        </action>  
  29.        <!--   
  30.        <action name="category" class="com.gz.bbs2011.action.{1}Action">  
  31.         <result name="add_input">/admin/Category_add_input.jsp</result>  
  32.         <result name="update_input">/admin/Category_update_input.jsp</result>  
  33.        </action>  
  34.         -->  
  35.     </package>  
  36.        
  37.     <package name="front" namespace="/" extends="struts-default" >  
  38.        <action name="Category_list" class="com.gz.bbs2011.action.CategoryAction" method="list">  
  39.             <result>/admin/Category_list.jsp</result>  
  40.        </action>  
  41.     </package>  
  42.       
  43.   
  44. </struts>  


  1. <body>  
  2.     Category_list  
  3.     <a href="admin/Category_addInput">添加Category</a>  
  4.     
  5.     <hr/>  
  6.     <s:iterator value="categories" var="c">  
  7. <table border="1" width="30%">  
  8.     <tr>  
  9.         <td><s:property value="#c.name"/></td>  
  10.         <td><s:property value="#c.description"/></td>  
  11.         <td><a href="admin/Category_delete?id=<s:property value="#c.id"/>">刪除Category</a></td>  
  12.         <td><a href="admin/Category_updateInput?id=<s:property value="#c.id"/>">更新Category</a></td>  
  13.     </tr>  
  14. </table>  
  15.       
  16.     </s:iterator>  
  17.     <s:debug></s:debug>  
  18.   </body>  
  1. <body>  
  2.   <form action="admin/Category_add" method="post">  
  3.     name:<input name="category.name" />  
  4.     description:<textarea name="category.description"></textarea>  
  5.     <input type="submit" value="add" />   
  6.   </form>  
  7.   </body>  

  1. <body>  
  2.   <form action="admin/Category_update" method="post">  
  3.     <input type="hidden" name="category.id" value="<s:property value="category.id"/>"/>  
  4.     name:<input name="category.name" value="<s:property value="category.name"/>"/>  
  5.     description:<textarea name="category.description"><s:property value="category.description"/></textarea>  
  6.     <input type="submit" value="update" />   
  7.   </form>  
  8.   </body> 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章