Struts1.x 基本原理及註冊模組的實現,struts1.x基本原理

來源:互聯網
上載者:User

Struts1.x 基本原理及註冊模組的實現,struts1.x基本原理

1、編寫JavaBean:User,必須繼承於ActionForm類

 1 package myuser; 2  3 import org.apache.struts.action.ActionForm; 4  5 public class User extends ActionForm{ 6      7     //不加這個 eclipse 有警告 8     private static final long serialVersionUID = 5599822869891501922L; 9 10     private Integer id;11     12     private String userName;13     14     private String pwd;15 16     public Integer getId() {17         return id;18     }19 20     public void setId(Integer id) {21         this.id = id;22     }23 24     public String getUserName() {25         return userName;26     }27 28     public void setUserName(String userName) {29         this.userName = userName;30     }31 32     public String getPwd() {33         return pwd;34     }35 36     public void setPwd(String pwd) {37         this.pwd = pwd;38     }39 }


2、在struts-config.xml中註冊User的對象user

    FormBean的概念,顧名思義,Form中的bean,表單中的資料,在Struts1.x由專門的ActionForm類負責接收,配置在 struts-config.xml中

1      <form-beans>2          <form-bean name="user" type="myuser.User"/>3      </form-beans>

  一些反覆編寫的,沒有任何技術含量的代碼,都有架構來實現,比如:
     User user = new User();
     getParameter 等。
     FormBean的作用,為操作(action)提供資料。
3、編寫UserDao類的方法 addUser(User user)

 1     public void addUser(User user){ 2         String sql = "insert into users(userName,pwd) values(?,?)"; 3         //擷取串連 4         Connection conn = DBLib.getConn(); 5         //建立資料庫操作對象 6         PreparedStatement pstmt; 7         try { 8             pstmt = conn.prepareStatement(sql); 9             pstmt.setString(1, user.getUserName());10             pstmt.setString(2, user.getPwd());11             //執行資料庫操作12             pstmt.execute();13             pstmt.close();14         } catch (SQLException e) {15             e.printStackTrace();16         } finally{17             try {18                 //關閉資料庫連接19                 conn.close();20             } catch (SQLException e) {21                 e.printStackTrace();22             }23         }24         25     }


4、編寫AddUserAction,調用UserDAO類中的addUser方法,添加使用者,此類必須繼承於Action。

 1 package action; 2  3 import javax.servlet.http.HttpServletRequest; 4 import javax.servlet.http.HttpServletResponse; 5  6 import org.apache.struts.action.Action; 7 import org.apache.struts.action.ActionForm; 8 import org.apache.struts.action.ActionForward; 9 import org.apache.struts.action.ActionMapping;10 11 import dao.UserDAO;12 import myuser.User;13 14 public class AddUserAction extends Action {15 16     public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,17             HttpServletResponse response) throws Exception {18         User user = (User)form;19         UserDAO dao = new UserDAO();20         dao.addUser(user);21         return mapping.findForward("list");//跳轉到userlist.jsp頁面22     }23     24 }


5、配置AddUserAction, path,name,scope,forward,添加成功後轉向userlist.jsp

 1 <?xml version="1.0" encoding="UTF-8"?> 2  3  <!DOCTYPE struts-config PUBLIC 4      "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" 5      "http://struts.apache.org/dtds/struts-config_1_3.dtd"> 6  <struts-config> 7      <form-beans> 8          <form-bean name="user" type="myuser.User"/> 9      </form-beans>10      11      <action-mappings>12          <action path="/addUser" type="action.AddUserAction" name="user">13              <forward name="list" path="/userlist.jsp" />14          </action>15      </action-mappings>16  </struts-config>

配置說明:

form-beans:用來放置多個formbean

from-bean:單個的formbean

       | name:from-bean的名字,用於下面action標籤的引用

       | type:所對應的類,該類一定要繼承 ActionForm

action-mappings:用來放置多個action

action:單個的action

       | path:通過*.do(一般是,這要看web.xml url-pattern的配置)過濾後的路徑名稱,必須加"/"

       | type:所對應的類,該類一定要繼承 Action(或者其子類,後面會有例子)

       | name:form-beans中的某個form-bean

       | scope:該formbean的有效範圍,兩個值:request、session

       | attribute:formbean在該有效範圍中的名字,如果沒有設定該屬性預設就是name的值,一般不用設定該屬性

              forward:action 處理完成之後跳轉到哪個頁面

                     | name:給action中的 mapping.findForward(name) 用的

                     | path:頁面路徑,以前一定要加"/"

5、JSP頁面代碼

 1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2     pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset==UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body>10 使用者註冊<br/>11 12 <form method="post" action="addUser.do">13     <table>14         <tr>15             <td>使用者名稱:</td>16             <td><input type="text" name="userName"/></td>17         </tr>18         <tr>19             <td>密碼:</td>20             <td><input type="password" name="pwd"/></td>21         </tr>22         <tr>23             <td colspan="2">24                 <input type="submit" value="提 交"/>25             </td>26         </tr>27     </table>28 </form>29 <div id="msg"></div>30 <br/>31 <br/>32 <a href="main.jsp">返回首頁面</a>33 </body>34 </html>


代碼連結:http://pan.baidu.com/s/1qY4PEOW 提取碼:gy0n

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.