1. 两个JSP文件:
Index01.jsp
<%@ page contentType="text/html;charset=gb2312" language="java" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<!DOCTYPE HTML PUBLIC "-//W 3C//DTD HTML 4.01 Transitional//EN">
<html:html>
<head>
<html:base />
<title>submit</title>
</head>
<body>
<html:form action="/display.do" method="post">
<html:submit value="显示全部数据"/>
</html:form>
</body>
</html:html>
display_all.jsp
<%@ page contentType="text/html;charset=gb2312" language="java"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<!DOCTYPE HTML PUBLIC "-//W 3C//DTD HTML 4.01 Transitional//EN">
<html:html locale="true">
<head>
<html:base />
<title>
display_all.jsp
</title>
<body>
<table>
<logic:present name="array" scope="request">
<logic:iterate name="array" id="array" type="com.sql.struts.AActionForm">
<tr align="center">
<td class="table2">
<bean:write name="array" property="id" />
</td>
<td class="table2">
<bean:write name="array" property="name" />
</td>
<td class="table2">
<bean:write name="array" property="password" />
</td>
</tr>
</logic:iterate>
</logic:present>
</table>
</body>
</html:html>
2. form bean 文件:
AActionForm.java
package com.sql.struts;
import org.apache.struts.action.ActionForm;
public class AActionForm extends ActionForm{
String id=null;
String name=null;
String password=null;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
3. Action 文件:
DisplayAction.java
package com.sql.struts;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class DisplayAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
DBConn db = new DBConn();
boolean okFlag = false;
try {
ResultSet rs = db.executeQuery("select * from test");
ArrayList array = new ArrayList();
while (rs.next()) {
AActionForm actionForm = new AActionForm();
actionForm.setId(rs.getString("id"));
actionForm.setName(rs.getString("name"));
actionForm.setPassword(rs.getString("password"));
array.add(actionForm);
}
request.setAttribute("array", array);
// return (mapping.findForward("success"));
} catch (SQLException ex) {
System.out.print("Logon Error:" + ex.getMessage());
} finally {
db.CloseConn();
okFlag = true;
}
if(okFlag){
return (mapping.findForward("success"));
}else{
return (mapping.findForward("failure"));
}
}
}
4. 数据库连接文件:
DBConn.java
package com.sql.struts;
import java.sql.*;
public class DBConn{
String sDBDriver="org.gjt.mm.mysql.Driver";
String sConnStr="jdbc:mysql://127.0.0.1:3306/test?user=root&password=xiaotu&useUnicode=true&characterEncoding=ISO-8859-1";
Connection conn=null;
ResultSet rs=null;
public DBConn(){
try{
Class.forName(sDBDriver);
}
catch(java.lang.ClassNotFoundException e){
System.out.println("Jdbc_conn():"+e.getMessage());
}
}
public void executeUpdate(String sql)throws Exception{
//sql=new String(sql.getBytes("GBK"),"ISO8859_1");
try{
conn=DriverManager.getConnection(sConnStr);
Statement stmt=conn.createStatement();
stmt.executeUpdate(sql);
conn.close();
stmt.close();
}
catch(SQLException ex){
System.out.println("sql.executeUpdate:"+ex.getMessage());
}
}
public ResultSet executeQuery(String sql)throws Exception{
rs=null;
try{
sql=new String(sql.getBytes("ISO-8859-1"));
conn=DriverManager.getConnection(sConnStr);
Statement stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
}
catch(SQLException ex){
System.out.println("sql.executeQuery:"+ex.getMessage());
}
return rs;
}
public void CloseConn()
throws Exception {
try
{
if (rs!=null)
rs.close();
if (conn!=null)
conn.close();
}
catch(SQLException ex)
{
System.out.println ("closeConn: " + ex);
}
}
}
5. struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<form-beans>
<form-bean name="aActionForm" type="com.sql.struts.AActionForm">
<form-property name="id" type="java.lang.String" />
<form-property name="name" type="java.lang.String" />
<form-property name="password" type="java.lang.String" />
</form-bean>
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings>
<action path="/display"
type="com.sql.struts.DisplayAction"
name="aActionForm"
scope="request"
input="/index01.jsp">
<forward name="success" path="/display_all.jsp" />
<forward name="failure" path="/index01.jsp"/>
</action>
</action-mappings>
<message-resources parameter="com.sql.struts.ApplicationResources" />
</struts-config>
6. web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
6. 运行:http://localhost:8089//test01/index01.jsp
6. 运行:http://localhost:8089//test01/index01.jsp