{"id":549,"date":"2023-03-23T21:19:42","date_gmt":"2023-03-23T13:19:42","guid":{"rendered":""},"modified":"2023-03-23T21:19:42","modified_gmt":"2023-03-23T13:19:42","slug":"Spring Struts\u96c6\u6210","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/549.html","title":{"rendered":"Spring Struts\u96c6\u6210"},"content":{"rendered":"


\n <\/head>
\n <\/p>\n

\n

Spring Struts\u96c6\u6210<\/h1>\n<\/p><\/div>\n
\n Spring\u6846\u67b6\u63d0\u4f9b\u4e86\u4e00\u79cd\u7ba1\u7406\u4f9d\u8d56\u9879\u7684\u7b80\u4fbf\u65b9\u6cd5\u3002\u5b83\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u4e0estruts 2\u6846\u67b6\u96c6\u6210\u3002\n <\/div>\n
\n ContextLoaderListener <\/b>\u7c7b\u7528\u4e8e\u4e0eStruts 2\u901a\u4fe1Spring\u5e94\u7528\u7a0b\u5e8f\u3002\u5fc5\u987b\u5728web.xml\u6587\u4ef6\u4e2d\u6307\u5b9a\u3002\n <\/div>\n
\n
\n \u60a8\u9700\u8981\u6267\u884c\u4ee5\u4e0b\u6b65\u9aa4:\n <\/div>\n

\u521b\u5efastruts2\u5e94\u7528\u7a0b\u5e8f\u5e76\u6dfb\u52a0spring jar\u6587\u4ef6\u3002<\/span>
\n \u5728 web.xml <\/b>\u6587\u4ef6\u4e2d\uff0c\u5b9a\u4e49ContextLoaderListener\u7c7b\u3002<\/span>
\n \u5728 struts.xml <\/b>\u6587\u4ef6\u4e2d\uff0c\u4e3a\u64cd\u4f5c\u7c7b\u5b9a\u4e49bean\u540d\u79f0\u3002<\/span>
\n \u5728 applicationContext.xml <\/b>\u6587\u4ef6\u4e2d\uff0c\u521b\u5efaBean\u3002\u5176\u7c7b\u522b\u540d\u79f0\u5e94\u4e3a\u52a8\u4f5c\u7c7b\u522b\u540d\u79f0\uff0c\u4f8b\u5982com.lidihuo.Login\u548cid\u5e94\u8be5\u4e0estruts.xml\u6587\u4ef6\u7684\u64cd\u4f5c\u7c7b(\u4f8b\u5982login)\u5339\u914d\u3002<\/span>
\n \u5728 action\u7c7b<\/b>\u4e2d\uff0c\u5b9a\u4e49\u5176\u4ed6\u5c5e\u6027\uff0c\u4f8b\u5982\u6d88\u606f\u3002<\/span> <\/p>\n


\n

Spring\u548cStruts 2\u96c6\u6210\u793a\u4f8b<\/h3>\n
\n \u60a8\u9700\u8981\u521b\u5efa\u4ee5\u4e0b\u6587\u4ef6\u4ee5\u7b80\u5316\u64cd\u4f5cspring and struts 2\u5e94\u7528\u7a0b\u5e8f:\n <\/div>\n

index.jsp <\/strong> <\/span>
\n web.xml <\/strong> <\/span>
\n struts.xml <\/strong> <\/span>
\n applicationContext.xml <\/strong> <\/span>
\n Login.java <\/strong> <\/span>
\n welcome.jsp <\/strong> <\/span>
\n error.jsp <\/strong> <\/span> <\/p>\n

\n 1)index.jsp <\/strong>\n <\/div>\n
\n \u6b64\u9875\u9762\u4ece\u7528\u6237\u90a3\u91cc\u83b7\u53d6\u540d\u79f0\u3002\n <\/div>\n
\n
\r\n<%@ taglib uri=\"\/struts-tags\"<\/span> prefix=\"s\"<\/span>%>\r\n<s:form action=\"login\"<\/span>>\r\n<s:textfield name=\"userName\"<\/span> label=\"UserName\"<\/span>><\/s:textfield>\r\n<s:submit><\/s:submit>\r\n<\/s:form>\r\n<\/pre>\n

<\/code>\n <\/div>\n


\n
\n 2)web.xml <\/strong>\n <\/div>\n
\n \u5b83\u4e3astruts 2\u548c
\n ContextLoaderListener <\/b>\u4fa6\u542c\u5668\u7c7b\u5b9a\u4e49\u4e86\u63a7\u5236\u5668\u7c7b\uff0c\u4ee5\u5728struts2\u548cspring\u5e94\u7528\u7a0b\u5e8f\u4e4b\u95f4\u5efa\u7acb\u8fde\u63a5\u3002\n <\/div>\n
\n
\r\n<?xml version=\"1.0\"<\/span> encoding=\"UTF-8\"<\/span>?>\r\n<web-app version=\"2.5\"<\/span> \r\n  xmlns=\"http:\/\/java.sun.com\/xml\/ns\/javaee\"<\/span> \r\n  xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"<\/span> \r\n  xsi:schemaLocation=\"http:\/\/java.sun.com\/xml\/ns\/javaee \r\n  http:\/\/java.sun.com\/xml\/ns\/javaee\/web-app_2_5.xsd\">\r\n  <welcome-file-list>\r\n    <welcome-file>index.jsp<\/welcome-file>\r\n  <\/welcome-file-list>\r\n  <filter>\r\n    <filter-name>struts2<\/filter-name>\r\n    <filter-class>\r\n      org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter\r\n    <\/filter-class>\r\n  <\/filter>\r\n  \r\n<listener> \r\n<listener-class>org.springframework.web.context.ContextLoaderListener<\/listener-class> \r\n<\/listener> \r\n  \r\n  <filter-mapping>\r\n    <filter-name>struts2<\/filter-name>\r\n    <url-pattern>\/*<\/url-pattern>\r\n  <\/filter-mapping>\r\n  \r\n  <\/web-app>\r\n<\/pre>\n

<\/code>\n <\/div>\n


\n
\n 3)struts.xml <\/strong>\n <\/div>\n
\n \u5b83\u5b9a\u4e49\u5305\u542b\u64cd\u4f5c\u548c\u7ed3\u679c\u7684\u7a0b\u5e8f\u5305\u3002\u5728\u8fd9\u91cc\uff0c\u52a8\u4f5c\u7c7b\u540d\u79f0\u662flogin\uff0c\u5c06\u5728applicationContext.xml\u6587\u4ef6\u4e2d\u8fdb\u884c\u641c\u7d22\u3002\n <\/div>\n
\n
\r\n<?xml version=\"1.0\"<\/span> encoding=\"UTF-8\"<\/span> ?>\r\n<!DOCTYPE struts public <\/span>\"-\/\/Apache Software Foundation\/\/DTD Struts Configuration 2.1\/\/EN\"<\/span> \r\n\"http:\/\/struts.apache.org\/dtds\/struts-2.1.dtd\"<\/span>>\r\n<struts>\r\n<package <\/span>name=\"abc\"<\/span> extends=\"struts-default\"<\/span>>\r\n<action name=\"login\"<\/span><\/span> class=\"login\">\r\n<result name=\"success\"<\/span>>welcome.jsp<\/result>\r\n<\/action>\r\n<\/package>\r\n<\/struts>    \r\n<\/pre>\n

<\/code>\n <\/div>\n


\n
\n 4)applicationContext.xml <\/strong>\n <\/div>\n
\n \u5b83\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5177\u6709ID\u767b\u5f55\u540d\u7684bean\u3002\u8be5bean\u5bf9\u5e94\u4e8emypack.Login\u7c7b\u3002\n <\/div>\n
\n \u5b83\u5e94\u8be5\u4f4d\u4e8eWEB-INF\u76ee\u5f55\u4e2d\u3002\n <\/div>\n
\n
\r\n<?xml version=\"1.0\"<\/span> encoding=\"UTF-8\"<\/span>?>\r\n<beans\r\n  xmlns=\"http:\/\/www.springframework.org\/schema\/beans\"<\/span>\r\n  xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"<\/span>\r\n  xmlns:p=\"http:\/\/www.springframework.org\/schema\/p\"<\/span>\r\n  xsi:schemaLocation=\"http:\/\/www.springframework.org\/schema\/beans \r\nhttp:\/\/www.springframework.org\/schema\/beans\/spring-beans-2.5.xsd\">\r\n<bean id=\"login\"<\/span> class=\"mypack.Login\"<\/span>>\r\n<property name=\"message\"<\/span> value=\"Welcome Spring\"<\/span>><\/property>\r\n<\/bean>\r\n<\/beans>\r\n<\/pre>\n

<\/code>\n <\/div>\n


\n
\n 5)Login.java <\/strong>\n <\/div>\n
\n \u5b83\u5b9a\u4e49\u4e86\u4e24\u4e2a\u5c5e\u6027userName\u548c\u5177\u6709execute\u65b9\u6cd5\u7684\u6d88\u606f\uff0c\u5176\u4e2d\u8fd4\u56de\u6210\u529f\u3002\n <\/div>\n
\n
\r\npackage <\/span>mypack;\r\npublic <\/span>class <\/span>Login {\r\nprivate <\/span>String userName,message;\r\npublic <\/span>String getMessage() {\r\n  return <\/span>message;\r\n}\r\npublic void <\/span>setMessage(String message) {\r\n  this<\/span>.message = message;\r\n}\r\npublic <\/span>String getUserName() {\r\n  return <\/span>userName;\r\n}\r\npublic void <\/span>setUserName(String userName) {\r\n  this<\/span>.userName = userName;\r\n}\r\npublic <\/span>String execute(){\r\n  return <\/span>\"success\"<\/span>;\r\n}\r\n}\r\n<\/pre>\n

<\/code>\n <\/div>\n


\n
\n 6)welcome.jsp <\/strong>\n <\/div>\n
\n \u5b83\u663e\u793auserName\u548c\u6d88\u606f\u5c5e\u6027\u7684\u503c\u3002\n <\/div>\n
\n
\r\n<%@ taglib uri=\"\/struts-tags\"<\/span> prefix=\"s\"<\/span>%>\r\nWelcome, <s:property value=\"userName\"<\/span>\/><br\/>\r\n${message} \r\n<\/pre>\n

<\/code>\n <\/div>\n


\n
\n 7)error.jsp <\/strong>\n <\/div>\n
\n \u8fd9\u662f\u9519\u8bef\u9875\u9762\u3002\u4f46\u8fd9\u4e0d\u662f\u5fc5\u9700\u7684\uff0c\u56e0\u4e3a\u6211\u4eec\u6ca1\u6709\u5728\u52a8\u4f5c\u7c7b\u7684execute\u65b9\u6cd5\u4e2d\u5b9a\u4e49\u4efb\u4f55\u903b\u8f91\u3002\n <\/div>\n
\n
\r\nSorry! \r\n<\/pre>\n

<\/code>\n <\/div>\n

\n \u8f93\u51fa\n <\/div>\n

Spring Struts\u96c6\u6210_https:\/\/bianchenghao6.com_\u3010Spring \u6559\u7a0b\u3011_\u7b2c1\u5f20
\n

\n Spring Struts\u96c6\u6210_https:\/\/bianchenghao6.com_\u3010Spring \u6559\u7a0b\u3011_\u7b2c2\u5f20
\n
\n <\/body>
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"Spring Struts\u96c6\u6210zh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[105],"tags":[],"class_list":["post-549","post","type-post","status-publish","format-standard","hentry","category-bc_spring_jc"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/549"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=549"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/549\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=549"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=549"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=549"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}