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