这个 Struts 2 教程将解释如何使用 struts 2 从 MySQL
数据库中获取数据。在这个演示中,我们将使用 Java Collection List
来存储所有数据并显示数据集合使用 Struts 2 iterator
标签。
在这个演示中,我们需要创建这些页面......
index.jps 用于调用动作并显示记录。
FetchDataAction.java 用于将传入的数据存储在 List
中。
FetchDataDAO.java 用于连接数据库并检索数据。
FetchDataBean.java 代表表数据。
web.xml 用于定义过滤器和欢迎页面。
struts.xml 用于定义动作和结果。
请注意,此演示是在 MVC Architecture
上设计的。
要使用 Struts 2,我们需要 Struts 核心 jar 和 MySQL 连接器 jar。下载 Struts jars click here。
在开始代码之前,我们需要在 web.xml
中定义 struts 2 filter
。
web.xml
<web-app>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
创建与数据库通信并从表中检索数据的 DAO 和 Bean 类。
FetchDataDAO.java
package org.websparrow;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class FetchDataDAO {
public ResultSet fetchData() {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "");
String query = "SELECT NAME, EMAIL , MOBILE , COLLEGE , COURSE, SKILLS , MARKS1 , MARKS2 , MARKS3 FROM pdfreport";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
return rs;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
FetchDataBean.java
package org.websparrow;
public class FetchDataBean {
String name, email, mobile, college, course, skills;
int marks1, marks2, marks3;
// Generates Getters and Setters...
}
Action 类包含 ArrayList
对象作为数据成员和 execute
方法。
FetchDataAction.java
package org.websparrow;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
public class FetchDataAction extends ActionSupport {
List<FetchDataBean> dataList = null;
ResultSet rs = null;
String fetchData = null;
FetchDataDAO daoObj = null;
// Generate Getters and Setters...
public String execute() throws Exception {
try {
if (fetchData.equals("FetchRecords")) {
dataList = new ArrayList<FetchDataBean>();
FetchDataBean dataBean = null;
rs = new FetchDataDAO().fetchData();
if (rs != null) {
while (rs.next()) {
dataBean = new FetchDataBean();
dataBean.setName(rs.getString("NAME"));
dataBean.setEmail(rs.getString("EMAIL"));
dataBean.setMobile(rs.getString("MOBILE"));
dataBean.setCollege(rs.getString("COLLEGE"));
dataBean.setCourse(rs.getString("COURSE"));
dataBean.setSkills(rs.getString("SKILLS"));
dataBean.setMarks1(rs.getInt("MARKS1"));
dataBean.setMarks2(rs.getInt("MARKS2"));
dataBean.setMarks3(rs.getInt("MARKS3"));
dataList.add(dataBean);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return "SUCCESS";
}
}
最后,在 struts.xml
中定义动作和结果
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="default" extends="struts-default" namespace="/">
<action name="FetchRecords" class="org.websparrow.FetchDataAction">
<result name="SUCCESS">/index.jsp</result>
</action>
</package>
</struts>
现在创建与用户交互的页面。
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<style>
table, td, th {
border: 1px solid black;
}
table {
border-collapse: collapse;
width: 100%;
}
th {
height: 50px;
}
button {
background-color: #008CBA;
border: none;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
border-radius: 12px;
}
</style>
</head>
<body style="text-align: center;">
<s:form action="FetchRecords.action" method="POST">
<button type="submit" name="fetchData" value="FetchRecords">Fetch
Records</button>
</s:form>
<s:if test="fetchData=='FetchRecords'">
<div style="margin-top: 40px; margin-right: 150px; margin-left: 150px;">
<table>
<thead>
<tr style="background-color: #E0E0E1;">
<th>NAME</th>
<th>EMAIL</th>
<th>MOBILE</th>
<th>COLLEGE</th>
<th>COURSE</th>
<th>SKILLS</th>
<th>MARKS 1</th>
<th>MARKS 2</th>
<th>MARKS 3</th>
</tr>
</thead>
<s:iterator value="dataList">
<tr>
<td><s:property value="name" /></td>
<td><s:property value="email" /></td>
<td><s:property value="mobile" /></td>
<td><s:property value="college" /></td>
<td><s:property value="course" /></td>
<td><s:property value="skills" /></td>
<td><s:property value="marks1" /></td>
<td><s:property value="marks2" /></td>
<td><s:property value="marks3" /></td>
</tr>
</s:iterator>
</table>
</div>
</s:if>
</body>
</html>
在测试中,应用程序在您的浏览器中点击了这个 URL http://localhost:8080/Struts2FetchData/
。我们将得到如下所示的输出。
Screen 1
Screen 2
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://www.websparrow.org/struts/how-to-fetch-data-from-database-in-jsp-using-struts2
内容来源于网络,如有侵权,请联系作者删除!