程序运行流程:.jsp请求—>struts.xml—>请求对应的Action—>请求接口类—>请求实现接口的类—>访问数据库。
文件目录:
首先在sql server中创建一个如下所示的user_table表: 表中的userId应设置为标识符并且自增1,否则进行操作时需要手动添加这个字段。
User.java
public class User { private int userId; private String sno; private String name; private String age; private String sex; private String major; private String telephone; public int getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; } public String getSno() { return sno; } public void setSno(String sno) { this.sno = sno; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getMajor() { return major; } public void setMajor(String major) { this.major = major; } public String getTelephone() { return telephone; } public void setTelephone(String telephone) { this.telephone = telephone; } }访问数据库的工具类:UserDao 在访问数据库的工具类中添加如下代码,进行学生信息的插入:
public static void main(String arg[]) { UserDao dc=new UserDao();//构造函数加载驱动程序 String sql="insert into user_table values('005','Tom','20','男','生物工程','15100000102')"; dc.executeUpdate(sql); }UserList.jsp
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GB18030"> <title>学生列表</title> <style type="text/css"> .br_TR{ background-color:#ffecd2; font-size:15px; text-align:center; } .br_head{ background-color: #fcb69f; font-size:15px; border: 1px solid #9DB3C5; padding: 1px; width:90%; text-align:center; margin:20px auto; } </style> </head> <body> <div align="center"> <h3><font color="blue">学生信息列表</font></h3> <s:form action="userDeletes" theme="simple"> <table width="640" border="1" align="center" bordercolor="99CCFF"> <tr class="br_head"> <td>select</td> <td>学号</td> <td>姓名</td> <td>年龄</td> <td>性别</td> <td>专业</td> <td>电话</td> <td>编辑</td> <td>删除</td> </tr> <s:iterator value="usersList"> <tr class="br_TR"> <td> <input type="checkbox" name="userIds" value='<s:property value="userId"/>'> </td> <td><s:property value="sno" /></td> <td><s:property value="name" /></td> <td><s:property value="age" /></td> <td><s:property value="sex" /></td> <td><s:property value="major" /></td> <td><s:property value="telephone" /></td> <td><a href='<s:url action="userEdit"><s:param name="userId" value="userId"/></s:url>'>Edit</a></td> <td><a href='<s:url action="userDelete"><s:param name="userId" value="userId"/></s:url>'>Delete</a></td> </tr> </s:iterator> </table> <s:submit value="RemoveAll" /> <p><a href="/工程名/curp/UserAdd.jsp">Add</a></p> </s:form> </div> </body> </html>创建接口UserService,用于定义得到用户信息的抽象方法。 UserService.java
public interface UserService { public List<User> getUserList(); }创建一个实现接口的类UserServiceImpl。在UserServiceImpl类中实现上面定义的抽象方法,得到用户列表 UserServiceImpl.java
public class UserServiceImpl implements UserService { UserDao ub=new UserDao(); ResultSet rs=null; @Override public List<User> getUserList() { // TODO Auto-generated method stub List<User> users=new ArrayList(); String strSql="select * from user_table"; rs=ub.executeQuery(strSql); try { while(rs.next()) { User user=new User(); user.setUserId(rs.getInt("userId")); user.setSno(rs.getString("sno")); user.setName(rs.getString("name")); user.setAge(rs.getString("age")); user.setSex(rs.getString("sex")); user.setMajor(rs.getString("major")); user.setTelephone(rs.getString("telephone")); users.add(user); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return users; } }创建一个UserManagementAction,拦截器通过set、get方法设置,获取数据。
UserManagementAction.java
public class UserManagementAction extends ActionSupport { List<User> usersList=new ArrayList(); UserService us=new UserServiceImpl(); public List<User> getUsersList() { return usersList; } public void setUsersList(List<User> usersList) { this.usersList = usersList; } public String getUserList() { String str="userListsucc"; usersList=us.getUserList(); return str; } }struts.xml
<action name="userList" class="UserManagementAction的全路径名" method="getUserList"> <result name="userListsucc">/curp/UserList.jsp</result> <result name="userListfail">/curp/error.jsp</result> </action>将程序部署到tomcat,在浏览器中输入http://localhost:8080/工程名/userList (userList与struts.xml中的name值一致)