それで、私はこのトピックについて数日間読んでいますが、理解したと思うたびに、再び混乱します.
だから私が持っているもの:MySQLデータベース、Eclipse、Tomcat Apache、STRUTS2を使用したGUIの始まり
上記のデータベースの GUI をセットアップしており、JSP ページにデータ テーブルの 1 つを表示したいと考えています。だから私の問題...まあ、私は多くの問題を抱えていますが、最初は、テーブルを表示したいJSPページに私を送るアクションがあるというフォームがあります。別のアクションを使用して接続すると思いましたデータベースにアクセスしてすべての情報を取得しますが、それが意味を成すかどうかはわかりません。同じアクションを使用する必要がありますか?
さて...ここに私のコードの一部があります:
データを取得するアクション:
package net.upsmon.struts2.action;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import com.opensymphony.xwork2.ActionSupport;
public class Table extends ActionSupport {
List tableRows=new ArrayList();
public String execute() throws InstantiationException, IllegalAccessException, ClassNotFoundException{
List tableInfo=new ArrayList();
Properties props = new Properties();
props.setProperty("user", "user");
props.setProperty("password", "pass");
props.setProperty("databaseName", "db");
String dbUrl="jdbc:mysql://db:3306/db";
String ret = ERROR;
Connection myCon = null;
try{
Class.forName ("com.mysql.jdbc.Driver").newInstance();
myCon = DriverManager.getConnection("jdbc:mysql://dburl:3306/mqmonitordb",props);
String dbQuery = "SELECT * FROM table";
PreparedStatement ps = myCon.prepareStatement(dbQuery);
ResultSet result=ps.executeQuery(dbQuery);
while(result.next()){
tableInfo.add(result.getString("raisedAlertId"));
tableInfo.add(result.getString("alertHostIP"));
tableInfo.add(result.getString("portNumber"));
tableInfo.add(result.getString("qMgrName"));
tableInfo.add(result.getString("alertType"));
tableInfo.add(result.getString("alertGroupName"));
tableInfo.add(result.getString("alertEntity"));
tableInfo.add(result.getString("maintMode"));
tableInfo.add(result.getString("alertCreatedTime"));
tableInfo.add(result.getString("alertRaisedTime"));
tableInfo.add(result.getString("alertAckFlag"));
tableRows.add(tableInfo);
tableInfo.clear();
}
//request.setAttribute("tableRows",tableRows);
}catch(SQLException sqe){
}catch(Exception e){
}
return "yay";
}
public List tableRows() {
return tableRows();
}
public void setTableRows(List tableRows) {
this.tableRows = tableRows;
}
}
JSP ページ:
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<link type="text/css" rel="stylesheet" href="stylesheet.css" />
<title>Welcome</title>
</head>
<body>
<h2>
Welcome
</h2>
<table>
<thead>
<tr>
<th></th>
<th>column1</th>
<th>column2</th>
</tr>
</thead>
<tbody>
<s:iterator status="stat" value="tableRows">
<tr>
<td><s:property value="#stat.index+1"></s:property></td>
<td><s:property value="%{column1}"></s:property></td>
<td><s:property value="%{column2}"></s:property></td>
</tr>
</s:iterator>
</tbody>
</table>
<s:form action="logout" method="post" margin="auto">
<s:submit method="authenticate" key="label.logout" align="center" />
</s:form>
<s:form action="tolevelselector" method="post" margin="auto">
<s:submit method="authenticate" key="label.levelthree" align="center" />
</s:form>
</body>
</html>
どんなアドバイスも素晴らしいでしょう、ありがとう....