{"id":185326,"date":"2024-11-10T20:37:21","date_gmt":"2024-11-10T12:37:21","guid":{"rendered":"https:\/\/server.hk\/cnblog\/185326\/"},"modified":"2024-11-10T20:37:22","modified_gmt":"2024-11-10T12:37:22","slug":"%e4%bd%bf%e7%94%a8struts2%e4%b8%8b%e6%8b%89%e5%88%97%e8%a1%a8%e6%93%8d%e4%bd%9c%e6%95%b8%e6%93%9a%e5%ba%ab%e6%95%b8%e6%93%9a%ef%bc%88struts2%e4%b8%8b%e6%8b%89%e5%88%97%e8%a1%a8%e6%95%b8%e6%93%9a","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/185326\/","title":{"rendered":"\u4f7f\u7528Struts2\u4e0b\u62c9\u5217\u8868\u64cd\u4f5c\u6578\u64da\u5eab\u6578\u64da\uff08struts2\u4e0b\u62c9\u5217\u8868\u6578\u64da\u5eab\uff09"},"content":{"rendered":"<h1 id=\"%e4%bd%bf%e7%94%a8struts2%e4%b8%8b%e6%8b%89%e5%88%97%e8%a1%a8%e6%93%8d%e4%bd%9c%e6%95%b8%e6%93%9a%e5%ba%ab%e6%95%b8%e6%93%9a-ZiozOfMuMU\">\u4f7f\u7528Struts2\u4e0b\u62c9\u5217\u8868\u64cd\u4f5c\u6578\u64da\u5eab\u6578\u64da<\/h1>\n<p>\u5728\u73fe\u4ee3\u7684Web\u61c9\u7528\u7a0b\u5e8f\u958b\u767c\u4e2d\uff0c\u4f7f\u7528\u6846\u67b6\u4f86\u7c21\u5316\u958b\u767c\u6d41\u7a0b\u662f\u975e\u5e38\u666e\u904d\u7684\u505a\u6cd5\u3002Apache Struts2\u662f\u4e00\u500b\u6d41\u884c\u7684Java Web\u61c9\u7528\u6846\u67b6\uff0c\u63d0\u4f9b\u4e86\u5f37\u5927\u7684\u529f\u80fd\u4f86\u8655\u7406\u7528\u6236\u8acb\u6c42\u548c\u6578\u64da\u4ea4\u4e92\u3002\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5011\u5c07\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528Struts2\u4f86\u5275\u5efa\u4e00\u500b\u4e0b\u62c9\u5217\u8868\uff0c\u4e26\u5f9e\u6578\u64da\u5eab\u4e2d\u7372\u53d6\u6578\u64da\u3002<\/p>\n<h2 id=\"struts2%e7%b0%a1%e4%bb%8b-ZiozOfMuMU\">Struts2\u7c21\u4ecb<\/h2>\n<p>Struts2\u662f\u4e00\u500b\u57fa\u65bcMVC\uff08\u6a21\u578b-\u8996\u5716-\u63a7\u5236\u5668\uff09\u67b6\u69cb\u7684\u6846\u67b6\uff0c\u65e8\u5728\u7c21\u5316Web\u61c9\u7528\u7a0b\u5e8f\u7684\u958b\u767c\u3002\u5b83\u63d0\u4f9b\u4e86\u8a31\u591a\u529f\u80fd\uff0c\u5305\u62ec\u8868\u55ae\u8655\u7406\u3001\u6578\u64da\u9a57\u8b49\u548c\u570b\u969b\u5316\u7b49\u3002\u4f7f\u7528Struts2\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u66f4\u8f15\u9b06\u5730\u69cb\u5efa\u53ef\u7dad\u8b77\u548c\u53ef\u64f4\u5c55\u7684\u61c9\u7528\u7a0b\u5e8f\u3002<\/p>\n<h2 id=\"%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5-ZiozOfMuMU\">\u6578\u64da\u5eab\u9023\u63a5<\/h2>\n<p>\u5728\u958b\u59cb\u4e4b\u524d\uff0c\u6211\u5011\u9700\u8981\u78ba\u4fdd\u80fd\u5920\u9023\u63a5\u5230\u6578\u64da\u5eab\u3002\u9019\u901a\u5e38\u901a\u904e\u914d\u7f6e\u6578\u64da\u6e90\u4f86\u5be6\u73fe\u3002\u5728Struts2\u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528Hibernate\u6216JDBC\u4f86\u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528JDBC\u9023\u63a5\u6578\u64da\u5eab\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>import java.sql.Connection;\nimport java.sql.DriverManager;\nimport java.sql.SQLException;\n\npublic class DatabaseConnection {\n    private static final String URL = \"jdbc:mysql:\/\/localhost:3306\/your_database\";\n    private static final String USER = \"your_username\";\n    private static final String PASSWORD = \"your_password\";\n\n    public static Connection getConnection() throws SQLException {\n        return DriverManager.getConnection(URL, USER, PASSWORD);\n    }\n}\n<\/code><\/pre>\n<h2 id=\"%e5%89%b5%e5%bb%ba%e4%b8%8b%e6%8b%89%e5%88%97%e8%a1%a8-ZiozOfMuMU\">\u5275\u5efa\u4e0b\u62c9\u5217\u8868<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u5c07\u5275\u5efa\u4e00\u500b\u4e0b\u62c9\u5217\u8868\uff0c\u8a72\u5217\u8868\u5c07\u986f\u793a\u5f9e\u6578\u64da\u5eab\u4e2d\u7372\u53d6\u7684\u6578\u64da\u3002\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u5275\u5efa\u4e00\u500bAction\u985e\u4f86\u8655\u7406\u6578\u64da\u7684\u7372\u53d6\uff1a<\/p>\n<pre><code>import com.opensymphony.xwork2.ActionSupport;\nimport java.sql.Connection;\nimport java.sql.ResultSet;\nimport java.sql.Statement;\nimport java.util.ArrayList;\nimport java.util.List;\n\npublic class DropdownAction extends ActionSupport {\n    private List items;\n\n    public String execute() {\n        items = new ArrayList();\n        try (Connection connection = DatabaseConnection.getConnection();\n             Statement statement = connection.createStatement()) {\n            ResultSet resultSet = statement.executeQuery(\"SELECT name FROM your_table\");\n            while (resultSet.next()) {\n                items.add(resultSet.getString(\"name\"));\n            }\n        } catch (Exception e) {\n            e.printStackTrace();\n        }\n        return SUCCESS;\n    }\n\n    public List getItems() {\n        return items;\n    }\n}\n<\/code><\/pre>\n<h2 id=\"%e9%85%8d%e7%bd%aestruts-xml-ZiozOfMuMU\">\u914d\u7f6estruts.xml<\/h2>\n<p>\u5728\u914d\u7f6e\u6587\u4ef6struts.xml\u4e2d\uff0c\u6211\u5011\u9700\u8981\u5b9a\u7fa9Action\u548c\u7d50\u679c\uff1a<\/p>\n<pre><code>&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\n\n&lt;struts&gt;\n    &lt;package name=\"default\" extends=\"struts-default\"&gt;\n        &lt;action name=\"dropdown\" class=\"DropdownAction\"&gt;\n            &lt;result name=\"success\"&gt;\/WEB-INF\/pages\/dropdown.jsp&lt;\/result&gt;\n        &lt;\/action&gt;\n    &lt;\/package&gt;\n&lt;\/struts&gt;\n<\/code><\/pre>\n<h2 id=\"%e5%89%b5%e5%bb%bajsp%e9%a0%81%e9%9d%a2-ZiozOfMuMU\">\u5275\u5efaJSP\u9801\u9762<\/h2>\n<p>\u6700\u5f8c\uff0c\u6211\u5011\u9700\u8981\u5275\u5efa\u4e00\u500bJSP\u9801\u9762\u4f86\u986f\u793a\u4e0b\u62c9\u5217\u8868\uff1a<\/p>\n<pre><code>&lt;%@ taglib uri=\"\/struts-tags\" prefix=\"s\" %&gt;\n&lt;html&gt;\n&lt;head&gt;\n    &lt;title&gt;\u4e0b\u62c9\u5217\u8868\u793a\u4f8b&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n    &lt;h1&gt;\u9078\u64c7\u4e00\u500b\u9805\u76ee&lt;\/h1&gt;\n    &lt;s:form action=\"dropdown\"&gt;\n        &lt;s:select name=\"selectedItem\" list=\"items\" label=\"\u9805\u76ee\u5217\u8868\"\/&gt;\n        &lt;s:submit value=\"\u63d0\u4ea4\"\/&gt;\n    &lt;\/s:form&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-ZiozOfMuMU\">\u7e3d\u7d50<\/h2>\n<p>\u901a\u904e\u4ee5\u4e0a\u6b65\u9a5f\uff0c\u6211\u5011\u6210\u529f\u5730\u4f7f\u7528Struts2\u5275\u5efa\u4e86\u4e00\u500b\u4e0b\u62c9\u5217\u8868\uff0c\u4e26\u5f9e\u6578\u64da\u5eab\u4e2d\u7372\u53d6\u6578\u64da\u3002\u9019\u7a2e\u65b9\u6cd5\u4e0d\u50c5\u7c21\u5316\u4e86\u6578\u64da\u7684\u986f\u793a\uff0c\u9084\u63d0\u9ad8\u4e86\u7528\u6236\u9ad4\u9a57\u3002\u5c0d\u65bc\u9700\u8981\u8655\u7406\u5927\u91cf\u6578\u64da\u7684Web\u61c9\u7528\u7a0b\u5e8f\uff0c\u9019\u662f\u4e00\u500b\u975e\u5e38\u5be6\u7528\u7684\u89e3\u6c7a\u65b9\u6848\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u90e8\u7f72\u60a8\u7684Web\u61c9\u7528\u7a0b\u5e8f\uff0cServer.HK\u63d0\u4f9b\u4e86\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u7684\u5ba2\u6236\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6\u670d\u52d9\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u7a69\u5b9a\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u5728Struts2\u4e2d\u64cd\u4f5c\u4e0b\u62c9\u5217\u8868\u4ee5\u5f9e\u6578\u64da\u5eab\u7372\u53d6\u548c\u986f\u793a\u6578\u64da\uff0c\u7c21\u5316\u7528\u6236\u9078\u64c7\u548c\u6578\u64da\u7ba1\u7406\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-185326","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/185326","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=185326"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/185326\/revisions"}],"predecessor-version":[{"id":185327,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/185326\/revisions\/185327"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=185326"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=185326"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=185326"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}