使用struts2中s標籤查詢使用者列表資訊
阿新 • • 發佈:2018-12-31
首先,在資料建立使用者表並插入資料,用jdbc連線資料庫。本次案例是使用jsp+struts+jdbc。
1.建立javabean:UserDTO.java
package com.itheima.web.dto; public class UserDTO { private String uid; private String uname; private String upassword; private int upower; private String udate; private String uaddr; private String utel; private String urealname; private String usex; private int udeleteStatic; private String userMail; public String getUid() { return uid; } public void setUid(String uid) { this.uid = uid; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getUpassword() { return upassword; } public void setUpassword(String upassword) { this.upassword = upassword; } public int getUpower() { return upower; } public void setUpower(int upower) { this.upower = upower; } public String getUdate() { return udate; } public void setUdate(String udate) { this.udate = udate; } public String getUaddr() { return uaddr; } public void setUaddr(String uaddr) { this.uaddr = uaddr; } public String getUtel() { return utel; } public void setUtel(String utel) { this.utel = utel; } public String getUrealname() { return urealname; } public void setUrealname(String urealname) { this.urealname = urealname; } public String getUsex() { return usex; } public void setUsex(String usex) { this.usex = usex; } public int getUdeleteStatic() { return udeleteStatic; } public void setUdeleteStatic(int udeleteStatic) { this.udeleteStatic = udeleteStatic; } public String getUserMail() { return userMail; } public void setUserMail(String userMail) { this.userMail = userMail; } @Override public String toString() { return "UserDTO [uid=" + uid + ", uname=" + uname + ", upassword=" + upassword + ", upower=" + upower + ", udate=" + udate + ", uaddr=" + uaddr + ", utel=" + utel + ", urealname=" + urealname + ", usex=" + usex + ", udeleteStatic=" + udeleteStatic + ", userMail=" + userMail + "]"; } }
2.建立dao層:UserDAO.java
package com.itheima.web.dao; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Vector; import com.itheima.web.dto.UserDTO; import com.itheima.web.util.DataAccess; public class UserDAO { Connection conn = null; //全域性變數,try內區域性變數不能用於finally java.sql.Statement stat = null; java.sql.Statement stat1 = null; java.sql.PreparedStatement prep = null; ResultSet rs = null; //查詢所有 public Vector<UserDTO> findAllUserInfo(){ Vector<UserDTO> v = new Vector<UserDTO>(); try{ conn=DataAccess.getConnection(); stat = conn.createStatement(); //3.建立sql語句物件並執行 String sql="select * from users where user_delete_static=1"; //4.查詢 rs = stat.executeQuery(sql); while(rs.next()) { UserDTO s = new UserDTO(); s.setUid(rs.getString("user_id")); s.setUname(rs.getString("user_nickname")); s.setUpassword(rs.getString("user_password")); s.setUpower(rs.getInt("user_power")); s.setUdate(rs.getString("user_date")); s.setUaddr(rs.getString("user_addr")); s.setUtel(rs.getString("user_Tel")); s.setUsex(rs.getString("user_sex")); s.setUrealname(rs.getString("user_realname")); s.setUdeleteStatic(rs.getInt("user_delete_static")); s.setUserMail(rs.getString("user_Mail")); v.add(s); } } catch (SQLException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return v; } //關閉資源 public void Final() { try{ if(stat!=null) { stat.close(); stat=null; } if(conn!=null) { conn.close(); conn=null; } if(rs!=null) { rs.close(); rs=null; } if(prep!=null) { prep.close(); prep=null; } } catch (SQLException e) { System.out.println("關閉連線,語句及結果集時出現錯誤!"); e.printStackTrace(); } } }
3.controller層:UserAction.java
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import com.itheima.web.dao.UserDAO; import com.itheima.web.dto.UserDTO; import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport{ //查詢 private Vector<UserDTO> vuser; public Vector<UserDTO> getVuser() { return vuser; } public void setVuser(Vector<UserDTO> vuser) { this.vuser = vuser; } public String select() throws Exception{ UserDAO userdao = new UserDAO(); vuser = userdao.findAllUserInfo(); return "success"; } }
配置檔案struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <!-- dtd說明了才能用struts標籤 -->
<struts>
<constant name="struts.action.extension" value="do" ></constant>
<constant name="struts.configuration.xml.reload" value="true"></constant>
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
<package name=" p1" extends="struts-default">
<action name="allUser" class="com.itheima.web.action.UserAction" method="select">
<result name="success">/allUser.jsp</result>
</action>
</package>
</struts>
4.前端頁面:allUser.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%> <!-- 宣告struts標籤庫才能用 -->
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>allUser</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<center><h3>使用者列表資訊</h3></center>
<table width="100%" border="1px">
<tr>
<td>使用者賬號</td>
<td>使用者名稱</td>
<td>密碼</td>
<td>許可權(1.管理員 2普通使用者)</td>
<td>註冊日期</td>
<td>收貨地址</td>
<td>收貨電話</td>
<td>真實姓名</td>
<td>性別</td>
<td>郵箱</td>
<td></td>
<td></td>
</tr>
<s:iterator value="%{vuser}" id="vuser" >
<tr>
<td><s:property value="#vuser.uid"/></td>
<td><s:property value="#vuser.uname"/></td>
<td><s:property value="#vuser.upassword"/></td>
<td><s:property value="#vuser.upower"/></td>
<td><s:property value="#vuser.udate"/></td>
<td><s:property value="#vuser.uaddr"/></td>
<td><s:property value="#vuser.utel"/></td>
<td><s:property value="#vuser.urealname"/></td>
<td><s:property value="#vuser.usex"/></td>
<%-- <td><s:property value="#vuser.udeleteStatic"/></td> --%>
<td><s:property value="#vuser.userMail"/></td>
<td><s:a href="toupdate.do?uid=%{#vuser.uid}">修改</s:a></td>
<td><s:a href="delete.do?uid=%{#vuser.uid}">刪除</s:a></td>
</tr>
</s:iterator>
</table>
</body>
</html>
工程資料夾:
訪問網址:hwork是工程名字,allUser是struts.xml裡面action的名字,.do是struts.xml裡面宣告的字尾名。