提交 5bf49622 编写于 作者: W wizardforcel

2020-06-12 21:15:00

上级 845b8961
......@@ -189,7 +189,7 @@ public class AppUI extends UI {
<version>${org.springframework.version}</version>
</dependency>
<!-- Commons Logging is required with Spring 4.x -->
<!-- Commons Logging is required with Spring4.x -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
......
# Struts2 Hello World 示例
# Struts2 HelloWorld 示例
> 原文: [https://howtodoinjava.com/struts2/struts-2-hello-world-example-application/](https://howtodoinjava.com/struts2/struts-2-hello-world-example-application/)
在我以前的文章中,我写了许多关于 [**JAX-RS RESTEasy**](//howtodoinjava.com/restful-web-service/ "resteasy tutorials")[**Spring3**](//howtodoinjava.com "Spring3 tutorials")[**的示例和教程 ] Hibernate**](//howtodoinjava.com/hibernate-tutorials/ "hibernate tutorials") 和其他 Java 框架,例如 [**Maven**](//howtodoinjava.com/maven/ "maven tutorials")[**junit**](//howtodoinjava.com/junit/ "junit tutorials") 。 我也有很多要求在 **Struts2** 上写一些东西。 好吧,这里我从世界示例开始。 在下一篇文章中,我将尝试涵盖涉及 Struts2 的最大领域和概念。因此,请继续关注。
在我以前的文章中,我写了许多关于 [**JAX-RS RESTEasy**](//howtodoinjava.com/restful-web-service/ "resteasy tutorials")[**Spring3**](//howtodoinjava.com "Spring3 tutorials")**Hibernate** 和其他 Java 框架,例如 [**Maven**](//howtodoinjava.com/maven/ "maven tutorials")[**junit**](//howtodoinjava.com/junit/ "junit tutorials") 的示例和教程。 我也有很多要求在 **Struts2** 上写一些东西。 好吧,这里我从世界示例开始。 在下一篇文章中,我将尝试涵盖涉及 Struts2 的最大领域和概念。因此,请继续关注。
```java
Sections in this post:
......@@ -20,7 +20,7 @@ Testing the application
## 创建 Maven Web 项目
我在这里不会吃太多空间。 您可以阅读有关 [**如何创建 Maven Eclipse Web 项目**](//howtodoinjava.com/maven/how-to-create-a-eclipse-web-application-using-maven/ "How to create a eclipse web application using maven") 的更多详细信息。 简而言之,使用以下命令。
我在这里不会吃太多空间。 您可以阅读有关[**如何创建 Maven Eclipse Web 项目**](//howtodoinjava.com/maven/how-to-create-a-eclipse-web-application-using-maven/ "How to create a eclipse web application using maven")的更多详细信息。 简而言之,使用以下命令。
```java
mvn archetype:generate -DgroupId=com.howtodoinjava.struts2.example -DartifactId=struts2example -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
......@@ -48,13 +48,13 @@ mvn eclipse:eclipse -Dwtpversion=2.0
![Struts2 jar files](img/4e306a9c4559a0413080ca658adc5234.png)
Struts2 jar files
Struts2 jar 文件
## web.xml 更改了
## 修改后的`web.xml`
需要以某种方式将 Struts 插入您的 Web 应用。 这意味着应将对应用的传入请求移交给 strut 进行处理。 这是通过在 web.xml 文件中添加过滤器定义来完成的。 此筛选器实质上将所有传入请求重定向到 [**StrutsPrepareAndExecuteFilter**](https://struts.apache.org/maven/struts2-core/apidocs/org/apache/struts2/dispatcher/filter/StrutsPrepareAndExecuteFilter.html "StrutsPrepareAndExecuteFilter") ,然后使用配置来处理该请求。
需要以某种方式将 Struts 插入您的 Web 应用。 这意味着应将对应用的传入请求移交给 strut 进行处理。 这是通过在`web.xml`文件中添加过滤器定义来完成的。 此筛选器实质上将所有传入请求重定向到[**`StrutsPrepareAndExecuteFilter`**](https://struts.apache.org/maven/struts2-core/apidocs/org/apache/struts2/dispatcher/filter/StrutsPrepareAndExecuteFilter.html "StrutsPrepareAndExecuteFilter"),然后使用配置来处理该请求。
```java
<!DOCTYPE web-app PUBLIC
......@@ -76,9 +76,9 @@ Struts2 jar files
```
## 知道 struts.xml 配置文件
## 了解`struts.xml`配置文件
因此,StrutsPrepareAndExecuteFilter 有一个要处理的请求。 怎么办? 它将使用该配置来知道如何处理特定请求。 此配置在 struts.xml 文件中定义。 该文件将具有特定于应用工作流及其操作处理器的 url 映射。 它还定义了输入/成功/错误视图。
因此,`StrutsPrepareAndExecuteFilter`有一个要处理的请求。 怎么办? 它将使用该配置来知道如何处理特定请求。 此配置在 struts.xml 文件中定义。 该文件将具有特定于应用工作流及其操作处理器的 url 映射。 它还定义了输入/成功/错误视图。
```java
<?xml version="1.0" encoding="UTF-8"?>
......@@ -103,9 +103,9 @@ Struts2 jar files
```
## 使用 struts.properties 文件
## 使用`struts.properties`文件
Struts 使用一些默认属性在运行时配置其行为。 您可以在 stuts.properties 文件中覆盖这些默认值。
Struts 使用一些默认属性在运行时配置其行为。 您可以在`stuts.properties`文件中覆盖这些默认值。
```java
#see http://struts.apache.org/2.0.14/docs/strutsproperties.html
......@@ -117,7 +117,7 @@ struts.custom.i18n.resources=global
## 编写第一个动作类
这很重要,因为您将在此处编写实际的应用逻辑。 Struts2 动作通常扩展 [**ActionSupport**](https://struts.apache.org/maven/struts2-core/apidocs/com/opensymphony/xwork2/ActionSupport.html "ActionSupport") 类,这些类提供了一些方法来覆盖和更改应用流,并在两者之间注入业务逻辑。
这很重要,因为您将在此处编写实际的应用逻辑。 Struts2 动作通常扩展[**`ActionSupport`**](https://struts.apache.org/maven/struts2-core/apidocs/com/opensymphony/xwork2/ActionSupport.html "ActionSupport")类,这些类提供了一些方法来覆盖和更改应用流,并在两者之间注入业务逻辑。
```java
package com.howtodoinjava.struts2.example.web;
......@@ -155,13 +155,13 @@ public class TestAction extends ActionSupport
```
**注意:** Struts2 动作看起来像 POJO 类,因为它们必须充当动作形式,它们也是 Struts 1 中的单独实体。
**注意:** Struts2 动作看起来像 POJO 类,因为它们必须充当动作形式,它们也是 Struts1 中的单独实体。
## 组成视图文件
这是一般步骤,涉及编写视图层,例如,在我们的例子中,我们正在编写 jsp 文件。 您可以使用消息资源从属性文件中获取消息,稍后在 i18n 中提供帮助。
**index.jsp**
**`index.jsp`**
```java
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
......@@ -190,7 +190,7 @@ public class TestAction extends ActionSupport
```
**success.jsp**
**`success.jsp`**
```java
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
......@@ -214,7 +214,7 @@ public class TestAction extends ActionSupport
他们正在使用的消息资源文件是:
**global.properties**
**`global.properties`**
```java
submit=Submit
......@@ -226,9 +226,9 @@ error.enter.message=Please enter your name !!
## 测试应用
现在好运行我们的 hello world 应用。 让我们点击浏览器。
现在好运行我们的 helloworld 应用。 让我们点击浏览器。
**键入 http:// localhost:8080 / struts2example /,然后按 Enter**
**键入`http://localhost:8080/struts2example/`,然后按`Enter`**
![struts2-hello-world-1](img/b4a6f2983528f91eb754fc988790757d.png)
......@@ -240,7 +240,8 @@ error.enter.message=Please enter your name !!
![struts2-hello-world-3](img/d0130d03643abb28625695765e4c3db3.png)
这就是 Struts2 hello world 应用的全部朋友。 如果要下载本教程的源代码,请遵循以下给定的下载链接。
这就是 Struts2 helloworld 应用的全部朋友。 如果要下载本教程的源代码,请遵循以下给定的下载链接。
[**源码下载**](https://docs.google.com/file/d/0B7yo2HclmjI4d2sxRGtSS1pONEE/edit?usp=sharing "Struts2 hello world 源码下载")
**Happy Learning !!**
\ No newline at end of file
**学习愉快!**
\ No newline at end of file
# Struts2 Hello World 注释示例
# Struts2 HelloWorld 注解示例
> 原文: [https://howtodoinjava.com/struts2/struts-2-hello-world-with-annotations/](https://howtodoinjava.com/struts2/struts-2-hello-world-with-annotations/)
在我以前的文章中,我写了许多关于 [**JAX-RS RESTEasy**](//howtodoinjava.com/restful-web-service/ "RESTful Web services Tutorials")[**Spring3**](//howtodoinjava.com "Spring3 Tutorials")[**的示例和教程 ] Hibernate**](//howtodoinjava.com/hibernate-tutorials/ "Hibernate Tutorials") 和其他 Java 框架,例如 [**Maven**](//howtodoinjava.com/maven/ "Maven Tutorials")[**junit** **4**](//howtodoinjava.com/junit/ "JUnit Tutorials") 。 我还在与 [**Struts2 hello world 和 xml 配置**](//howtodoinjava.com/struts-2/struts-2-hello-world-example-application/ "Struts2 hello world example application") 相关的帖子中写道。 在本文中,我将更新先前的示例,以使用注释来配置 Struts2 应用。
在我以前的文章中,我写了许多关于 [**JAX-RS RESTEasy**](//howtodoinjava.com/restful-web-service/ "resteasy tutorials")[**Spring3**](//howtodoinjava.com "Spring3 tutorials")**Hibernate** 和其他 Java 框架,例如 [**Maven**](//howtodoinjava.com/maven/ "maven tutorials")[**junit**](//howtodoinjava.com/junit/ "junit tutorials") 的示例和教程。 我还写了与 [**Struts2 helloworld 和 xml 配置**](//howtodoinjava.com/struts-2/struts-2-hello-world-example-application/ "Struts2 hello world example application") 相关的帖子。 在本文中,我将更新先前的示例,以使用注释来配置 Struts2 应用。
有关信息,struts 注解是 [**struts 常规插件**](https://struts.apache.org/docs/convention-plugin.html "Struts2 convention plugin") 的一部分。
有关信息,struts 注解是 [**struts 常规插件**](https://struts.apache.org/docs/convention-plugin.html "Struts2 convention plugin")的一部分。
```java
Sections in this post:
......@@ -19,7 +19,7 @@ Testing the application
## 创建 Maven Web 项目
我在这里不会吃太多空间。 您可以阅读有关 [**如何创建 Maven Eclipse Web 项目**](//howtodoinjava.com/maven/how-to-create-a-eclipse-web-application-using-maven/ "How to create a eclipse web application using maven") 的更多详细信息。 简而言之,使用以下命令。
我在这里不会吃太多空间。 您可以阅读有关[**如何创建 Maven Eclipse Web 项目**](//howtodoinjava.com/maven/how-to-create-a-eclipse-web-application-using-maven/ "How to create a eclipse web application using maven")的更多详细信息。 简而言之,使用以下命令。
```java
mvn archetype:generate -DgroupId=com.howtodoinjava.struts2.example -DartifactIad=struts2example
......@@ -50,11 +50,11 @@ mvn eclipse:eclipse -Dwtpversion=2.0
```
## web.xml 的更改
## `web.xml`的更改
需要以某种方式将 Struts 插入您的 Web 应用。 这意味着应将对应用的传入请求移交给 strut 进行处理。 这是通过在 web.xml 文件中添加过滤器定义来完成的。 此筛选器实质上将所有传入请求重定向到 **StrutsPrepareAndExecuteFilter** ,然后使用配置来处理该请求。
需要以某种方式将 Struts 插入您的 Web 应用。 这意味着应将对应用的传入请求移交给 strut 进行处理。 这是通过在`web.xml`文件中添加过滤器定义来完成的。 此筛选器实质上将所有传入请求重定向到 `StrutsPrepareAndExecuteFilter`,然后使用配置来处理该请求。
另外,我传递了“ **actionPackages** ” init 参数,以便可以扫描此包以查找必需的带注释的类。
另外,我传递了“`actionPackages`”初始化参数,以便可以扫描此包以查找必需的带注释的类。
```java
<!DOCTYPE web-app PUBLIC
......@@ -82,7 +82,7 @@ mvn eclipse:eclipse -Dwtpversion=2.0
## 编写第一个类
这很重要,因为您将在此处编写实际的应用逻辑。 Struts2 操作通常扩展 **ActionSupport** 类,该类提供了一些方法来覆盖和更改应用流,并在两者之间注入业务逻辑。
这很重要,因为您将在此处编写实际的应用逻辑。 Struts2 操作通常扩展`ActionSupport`类,该类提供了一些方法来覆盖和更改应用流,并在两者之间注入业务逻辑。
```java
@Namespace("/default")
......@@ -130,13 +130,13 @@ public class TestAction extends ActionSupport
```
注意:Struts2 动作看起来像 POJO 类,因为它们也必须充当动作形式,它们也是 Struts 1 中的单独实体。
注意:Struts2 动作看起来像 POJO 类,因为它们也必须充当动作形式,它们也是 Struts1 中的单独实体。
## 构成视图文件
这是一般步骤,涉及编写视图层,例如,在我们的例子中,我们正在编写 jsp 文件。 您可以使用消息资源从属性文件中获取消息,稍后在 i18n 中提供帮助。
**index.jsp**
**`index.jsp`**
```java
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
......@@ -165,7 +165,7 @@ public class TestAction extends ActionSupport
```
**success.jsp**
**`success.jsp`**
```java
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
......@@ -189,7 +189,7 @@ public class TestAction extends ActionSupport
我正在使用的消息资源文件是:
**TestAction.properties**
**`TestAction.properties`**
```java
submit=Submit
......@@ -201,9 +201,9 @@ error.enter.message=Please enter your name !!
## 测试应用
现在好运行我们的 hello world 应用。 让我们点击浏览器。
现在好运行我们的 helloworld 应用。 让我们点击浏览器。
**键入 http:// localhost:8080 / struts2example /,然后按 Enter**
**键入`http://localhost:8080/struts2example/`,然后按`Enter`**
![struts2-hello-world-1](img/b4a6f2983528f91eb754fc988790757d.png)
......@@ -219,7 +219,8 @@ error.enter.message=Please enter your name !!
![struts-folder-tree](img/ab6f074ff3a7767b519c8852fb057c90.png)
就是这个带有注解的 Struts2 hello world 应用的朋友。 如果要**下载本教程的源代码**,请按照以下给定的下载链接进行操作。
就是这个带有注解的 Struts2 helloworld 应用的朋友。 如果要**下载本教程的源代码**,请按照以下给定的下载链接进行操作。
[**源码下载**](https://docs.google.com/file/d/0B7yo2HclmjI4Q0NZUlgxbm90Vk0/edit?usp=sharing "Struts2 annotations hello world source code")
**Happy Learning !!**
\ No newline at end of file
**学习愉快!**
\ No newline at end of file
# 使用@InterceptorRef 示例的 Struts2 自定义拦截器
# 使用`@InterceptorRef`的 Struts2 自定义拦截器示例
> 原文: [https://howtodoinjava.com/struts2/struts-2-custom-interceptor-with-interceptorref-example/](https://howtodoinjava.com/struts2/struts-2-custom-interceptor-with-interceptorref-example/)
在以前的帖子中,我们了解了 [hello world 应用](//howtodoinjava.com/struts-2/struts-2-hello-world-with-annotations/ "Struts2 hello world with annotations")[设置 Struts2 应用](//howtodoinjava.com/struts-2/how-to-correctly-set-result-path-in-struts-2/ "How to correctly set result path in Struts2")的结果路径。 现在,在这篇文章中,我将举一个使用注释的自定义或用户定义的拦截器配置示例。
在以前的帖子中,我们了解了 [helloworld 应用](//howtodoinjava.com/struts-2/struts-2-hello-world-with-annotations/ "Struts2 hello world with annotations")[设置 Struts2 应用](//howtodoinjava.com/struts-2/how-to-correctly-set-result-path-in-struts-2/ "How to correctly set result path in Struts2")的结果路径。 现在,在这篇文章中,我将举一个使用注释的自定义或用户定义的拦截器配置示例。
拦截器是一个类,其每次访问已配置的服务器资源时都会调用其预定义方法。 这可以在资源访问之前或之后访问。 请注意,此处的资源访问也意味着 HTTP 请求处理。
......@@ -14,7 +14,7 @@ Steps summary:
4) Apply on any Action class
```
**Important**: Please note that Struts2 comes with many ready-made interceptor implementations, So make sure you really need to create your own interceptor or something can work for you.
**重要**:请注意,Struts2 随附了许多现成的拦截器实现,因此请确保您确实需要创建自己的拦截器,或者有什么适合您的。
## 1)& 2)创建自定义拦截器类并实现重写的方法
......@@ -51,7 +51,7 @@ public class DemoCustomInterceptor implements Interceptor
```
## 3)在 struts.xml 文件中给出定义
## 3)在`struts.xml`文件中给出定义
这是非常重要的一步,因为在这里您可以在 struts 上下文和运行时中注册拦截器类。
......@@ -82,9 +82,9 @@ public class DemoCustomInterceptor implements Interceptor
```
## 4)申请任何动作类
## 4)应用任何动作类
我在一个这样的 TestAction 类中应用了上面的拦截器。
我在一个这样的`TestAction`类中应用了上面的拦截器。
```java
package com.howtodoinjava.struts2.example.web;
......@@ -138,7 +138,7 @@ public class TestAction extends ActionSupport
```
请注意,我已经使用@InterceptorRef(value =” customStack”)定义应用了拦截器。 它可以帮助您一次性应用多个拦截器。 如果仅只需要特定的拦截器,请使用其名称,如下所示:@InterceptorRef(value =“ demoCustomInterceptor”)
请注意,我已经使用`@InterceptorRef(value="customStack")`定义应用了拦截器。 它可以帮助您一次性应用多个拦截器。 如果仅只需要特定的拦截器,请使用其名称,如下所示:`@InterceptorRef(value="demoCustomInterceptor")`
## 测试应用
......@@ -159,8 +159,4 @@ com.howtodoinjava.struts2.example.web.TestAction
请点击下面的链接下载该项目的源代码。
```java
**下载源码**
```
**祝您学习愉快!**
\ No newline at end of file
......@@ -2,15 +2,15 @@
> 原文: [https://howtodoinjava.com/struts2/how-to-correctly-set-result-path-in-struts-2/](https://howtodoinjava.com/struts2/how-to-correctly-set-result-path-in-struts-2/)
在这里,结果路径表示 Struts2 在执行 Action 类中的代码后将解析的 JSP 文件或其他视图文件的位置。 这些**结果路径在 Action 类顶部的@Result 注解**的“位置”中提到。
在这里,结果路径表示 Struts2 在执行 Action 类中的代码后将解析的 JSP 文件或其他视图文件的位置。 这些**结果路径在`Action`类顶部的`@Result`注解**的“位置”中提到。
一旦动作类完成执行,它将把控件传递给视图解析器。 视图解析器尝试查找需要渲染的视图文件的位置。 该解决方案主要可以通过两种方式提及:
## 1)将整个相对路径添加到“位置”属性
现在,在这里,一个简单的“ /”可以改变整个含义,所以要小心。
现在,在这里,一个简单的“`/`”可以改变整个含义,所以要小心。
**A)在位置属性**中带有“ /”字符
**A)在位置属性中带有“`/`”字符**
```java
@Namespace("/default")
......@@ -32,7 +32,7 @@ public class TestAction extends ActionSupport
```
**B)位置属性**中没有“ /”字符
**B)位置属性中没有“`/`”字符**
```java
@Namespace("/default")
......@@ -47,20 +47,20 @@ public class TestAction extends ActionSupport
```
Above definition will resolve the location of jsp file as ::
上面的定义会将jsp文件的位置解析为:
```java
%PROJECT_PATH%/WEB-INF/content/WEB-INF/jsp/index.jsp
```
**原因:**背后的原因是,开始时的**“ /”在项目根**处解析,而其余路径在项目根目录下解析。 但是,当**不在开始时使用“ /”时,默认的根目录假定为“ / WEB-INF / content”文件夹**,其余的路径与此相对。 因此,当您下次注意时,请在@Result 注解中指定 location 属性。
**原因:**背后的原因是,开始时的**“`/`”在项目根处解析,而其余路径在项目根目录下解析。 但是,**当不在开始时使用“`/`”时,默认的根目录假定为“`/WEB-INF/content`”文件夹**,其余的路径与此相对。 因此,当您下次注意时,请在`@Result`注解中指定`location`属性。
## 2)将资源根路径定义为常量“ struts.convention.result.path
## 2)将资源根路径定义为常量“`struts.convention.result.path`
您可以在此常量中定义资源的固定部分,即 JSP 文件,然后只需指定 jsp 文件的名称即可,而不必担心其在项目中的位置。 **的另一个优点是,您以后可以在**上将视图文件**移动到其他文件夹中,甚至**重命名文件夹名称**,而不必担心其影响。 您只需要更改此常数中的路径。**
您可以在此常量中定义资源的固定部分,即 JSP 文件,然后只需指定 jsp 文件的名称即可,而不必担心其在项目中的位置。 **另一个优点是,您以后可以在**上将视图文件**移动到其他文件夹中**,甚至**重命名文件夹名称**,而不必担心其影响。 您只需要更改此常数中的路径。
可以在 struts.xml 中将该常量定义为::
可以在`struts.xml`中将该常量定义为:
```java
<struts>
......@@ -69,14 +69,14 @@ Above definition will resolve the location of jsp file as ::
```
struts.properties 文件中为::
`struts.properties`文件中为:
```java
struts.convention.result.path=/WEB-INF/jsp/
```
指定此常量后,在 Action 类中,我们只需要指定 JSP 文件名即可。
指定此常量后,在`Action`类中,我们只需要指定 JSP 文件名即可。
```java
@Namespace("/default")
......@@ -91,8 +91,6 @@ public class TestAction extends ActionSupport
```
```java
The second approach involving constant "struts.convention.result.path" is recommended approach.
```
推荐使用涉及常量“`struts.convention.result.path`”的第二种方法。
**祝您学习愉快!**
\ No newline at end of file
# Spring 4 + Struts2 + Hibernate 集成教程
# Spring4 + Struts2 + Hibernate 集成教程
> 原文: [https://howtodoinjava.com/struts2/spring-4-struts-2-hibernate-integration-tutorial/](https://howtodoinjava.com/struts2/spring-4-struts-2-hibernate-integration-tutorial/)
以前,我已经介绍了 [**Spring3 +Hibernate 集成**](//howtodoinjava.com/spring/spring-orm/spring-3-and-hibernate-integration-tutorial-with-example/ "Spring3 and hibernate integration tutorial with example") 示例和 [**Struts2 hello world**](//howtodoinjava.com/struts-2/struts-2-hello-world-example-application/ "Struts2 hello world example application") 示例。 在本教程中,我将讨论将 spring 框架与 Struts 和 Hibernate 结合使用时要记住的所有重要点。 另外,请注意,本教程使用了其他次要但很重要的概念,例如日志记录,TLD 的使用和事​​务以及已集成到本教程中。 因此,请记住还要检查其详细信息。
以前,我已经介绍了 [**Spring3 + Hibernate 集成**](//howtodoinjava.com/spring/spring-orm/spring-3-and-hibernate-integration-tutorial-with-example/ "Spring3 and hibernate integration tutorial with example")示例和 [**Struts2 helloworld**](//howtodoinjava.com/struts-2/struts-2-hello-world-example-application/ "Struts2 hello world example application") 示例。 在本教程中,我将讨论将 spring 框架与 Struts 和 Hibernate 结合使用时要记住的所有重要点。 另外,请注意,本教程使用了其他次要但很重要的概念,例如日志记录,TLD 的使用和事​​务以及已集成到本教程中。 因此,请记住还要检查其详细信息。
在本教程中,我将使用简单的功能(例如全部获取,添加和删除)来构建员工管理屏幕。 它看起来像这样:
......@@ -26,22 +26,23 @@
## 1)整合概述
在进入集成细节之前,让我们先确定一下 ***为什么我们需要此集成*** 本身。 像 Struts 一样,Spring 也可以充当 MVC 实现。 两种框架都有其优缺点,仍然有很多人会同意 **Spring 更好**,并且提供了更广泛的功能。 对我来说,只有两种情况,您需要本教程中提供的信息:
在进入集成细节之前,让我们先确定一下***为什么我们需要此集成***本身。 像 Struts 一样,Spring 也可以充当 MVC 实现。 两种框架都有其优缺点,仍然有很多人会同意 **Spring 更好**,并且提供了更广泛的功能。 对我来说,只有两种情况,您需要本教程中提供的信息:
**i)**您有一个用 Struts 编写的旧应用,并且想要使用 spring 来提高应用的功能很多倍。
**ii)**您确实想根据自己的原因来学习它。
i)您有一个用 Struts 编写的旧应用,并且想要使用 spring 来提高应用的功能很多倍。
ii)您确实想根据自己的原因来学习它。
否则,我不知道为什么有人会在 Spring 选择支柱。 如果您知道其他一些好的理由,请与我们所有人分享。 那挺棒的。
继续,在本教程中,我将**委托从 Struts 到 Spring** 进行动作管理。 进行委派的原因是,通过 Spring 上下文实例化 Action 类时,它可以使用 spring 在其自己的 MVC 实现中为其提供的 Controller 类的所有其他功能。 因此,您将获得所有 spring 功能以及 struts Action 类,以具有包括 ActionForm 概念在内的控制器逻辑。
继续,在本教程中,我将**委托从 Struts 到 Spring** 进行动作管理。 进行委派的原因是,通过 Spring 上下文实例化`Action`类时,它可以使用 spring 在其自己的 MVC 实现中为其提供的`Controller`类的所有其他功能。 因此,您将获得所有 spring 功能以及 struts `Action`类,以具有包括`ActionForm`概念在内的控制器逻辑。
## 2)Spring + Struts 集成
这是核心逻辑,从在 web.xml 中注册`ContextLoaderListener``StrutsPrepareAndExecuteFilter`开始。 `ContextLoaderListener`带有初始化参数 ***contextConfigLocation*** ,并负责设置和启动 Spring `WebApplicationContext`。 现在,struts 将在与 Spring 相关的服务中特别是在[依赖项注入](//howtodoinjava.com/spring/spring-core/inversion-of-control-ioc-and-dependency-injection-di-patterns-in-spring-framework-and-related-interview-questions/ "Inversion of control (IoC) and dependency injection (DI) patterns in spring framework and related interview questions")中利用此上下文。
这是核心逻辑,从在`web.xml`中注册`ContextLoaderListener``StrutsPrepareAndExecuteFilter`开始。 `ContextLoaderListener`带有初始化参数`contextConfigLocation`,并负责设置和启动 Spring `WebApplicationContext`。 现在,struts 将在与 Spring 相关的服务中特别是在[依赖项注入](//howtodoinjava.com/spring/spring-core/inversion-of-control-ioc-and-dependency-injection-di-patterns-in-spring-framework-and-related-interview-questions/ "Inversion of control (IoC) and dependency injection (DI) patterns in spring framework and related interview questions")中利用此上下文。
`StrutsPrepareAndExecuteFilter`在类路径中查找 ***struts.xml*** 文件,并配置 strut 的特定内容,例如动作映射,全局转发和其他在 struts.xml 文件中定义的内容。
`StrutsPrepareAndExecuteFilter`在类路径中查找`struts.xml`文件,并配置 strut 的特定内容,例如动作映射,全局转发和其他在`struts.xml`文件中定义的内容。
**web.xml**
**`web.xml`**
```java
<?xml version="1.0" encoding="UTF-8"?>
......@@ -107,9 +108,9 @@
```
第二步,将在 **struts.xml** 文件中创建操作映射,如下所示:
第二步,将在`struts.xml`文件中创建操作映射,如下所示:
**struts.xml**
**`struts.xml`**
```java
<?xml version="1.0" encoding="UTF-8" ?>
......@@ -157,11 +158,11 @@
```
In strut’s alone application we would have full Action Class with it’s package information in “class” attribute. Here we have given the class name as editEmployeeAction. Where is it defined? We will ask Spring to lookup for us.
在单独的 strut 应用程序中,我们将在“`class`”属性中具有完整的`Action`类及其包装信息。 在这里,我们将类名命名为`editEmployeeAction`。 它在哪里定义? 我们将要求 Spring 为我们查找。
Spring 上下文文件 **beans.xml** 是典型的 Spring 单独上下文文件,具有 Web 应用运行所需的所有内容,其中包括 struts 正在寻找的 bean 定义`editEmployeeAction`
Spring 上下文文件`beans.xml`是典型的 Spring 单独上下文文件,具有 Web 应用运行所需的所有内容,其中包括 struts 正在寻找的 bean 定义`editEmployeeAction`
**beans.xml**
**`beans.xml`**
```java
<?xml version="1.0" encoding="UTF-8"?>
......@@ -239,9 +240,9 @@ Spring 上下文文件 **beans.xml** 是典型的 Spring 单独上下文文件
```
这是我们要做的**将支撑架与 spring** 框架集成在一起的所有步骤。 现在,您的动作类如下所示:
这是我们要做的**将 Struts 与 spring** 框架集成在一起的所有步骤。 现在,您的动作类如下所示:
**EditEmployeeAction.java**
**`EditEmployeeAction.java`**
```java
package com.howtodoinjava.controller;
......@@ -304,9 +305,9 @@ public class EditEmployeeAction extends ActionSupport implements Preparable
```
Spring 还将 DAO 引用注入到 Manager 类。
Spring 还将 DAO 引用注入到`Manager`类。
**EmployeeManagerImpl.java**
**`EmployeeManagerImpl.java`**
```java
package com.howtodoinjava.service;
......@@ -355,9 +356,9 @@ public class EmployeeManagerImpl implements EmployeeManager
现在我们必须将 Hibernate 集成到应用中。 最好的地方是利用 Spring 的强大功能进行集成,以充分利用依赖注入来与不同的 ORM 一起使用。
上面 **beans.xml** 文件中已经给出了 Spring 所需的 Hibernate 依赖关系。 您将需要的其他文件是:
上面`beans.xml`文件中已经给出了 Spring 所需的 Hibernate 依赖关系。 您将需要的其他文件是:
**hibernate.cfg.xml**
**`hibernate.cfg.xml`**
```java
<?xml version='1.0' encoding='utf-8'?>
......@@ -373,7 +374,7 @@ public class EmployeeManagerImpl implements EmployeeManager
```
**jdbc.properties**
**`jdbc.properties`**
```java
jdbc.driverClassName=com.mysql.jdbc.Driver
......@@ -384,7 +385,7 @@ jdbc.password=password
```
**EmployeeDaoImpl.java**
**`EmployeeDaoImpl.java`**
```java
package com.howtodoinjava.dao;
......@@ -433,9 +434,9 @@ public class EmployeeDaoImpl implements EmployeeDAO
```
供您参考,EmployeeEntity 类如下所示:
供您参考,`EmployeeEntity`类如下所示:
**EmployeeEntity.java**
**`EmployeeEntity.java`**
```java
package com.howtodoinjava.entity;
......@@ -478,9 +479,9 @@ public class EmployeeEntity {
#### a)Log4j
Spring 通过扫描类路径中的`log4j`自动配置日志记录。 我们使用`pom.xml`文件添加了 log4j 依赖项。 现在,您只需要在类路径中放置一个 log4j.xml 或 log4j.properties 文件。
Spring 通过扫描类路径中的 log4j 自动配置日志记录。 我们使用`pom.xml`文件添加了 log4j 依赖项。 现在,您只需要在类路径中放置一个`log4j.xml``log4j.properties`文件。
**log4j.properties**
**`log4j.properties`**
```java
log4j.rootLogger=info, stdout, R
......@@ -502,7 +503,7 @@ log4j.appender.R.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
如果您查看`web.xml`文件,我们在其中包含了一些 TLD。 我们可以随时在视图层中使用它们,如下所示:
**editEmployeeList.jsp**
**`editEmployeeList.jsp`**
```java
<%@ taglib prefix="s" uri="/struts-tags"%>
......@@ -574,9 +575,9 @@ log4j.appender.R.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
```
#### c)交易
#### c)事务
EmployeeManagerImpl.java 在诸如 getAllEmployees()和 deleteEmployee()之类的方法中使用注解@Transactional。 这实际上是在单个事务中运行在此方法下执行的所有数据库查询。 像这样在`beans.xml`上下文文件中声明了事务依赖项。
`EmployeeManagerImpl.java`在诸如`getAllEmployees()``deleteEmployee()`之类的方法中使用注解`@Transactional`。 这实际上是在单个事务中运行在此方法下执行的所有数据库查询。 像这样在`beans.xml`上下文文件中声明了事务依赖项。
```java
<!-- Run SQL queries in transactions -->
......@@ -591,16 +592,17 @@ EmployeeManagerImpl.java 在诸如 getAllEmployees()和 deleteEmployee()之类
## 5)要记住的要点
**a)** **如果运行时无法找到 lib 文件夹中存在的类,则将来自项目依赖项的 jar 文件添加到项目部署程序集**中。
**a)如果运行时无法找到`lib`文件夹中存在的类,则将来自项目依赖项的 jar 文件添加到项目部署程序集中。**
**b)**使`bean.xml`中定义的`Action`类的**作用域为“原型”**。 Spring 提供的 bean 的默认范围是单例,并且必须为每个请求创建新的 Struts `Action`类,因为它包含特定于用户会话的数据。 为了容纳两者,请将`Action`类 bean 标记为原型。
**b)**使 bean.xml 中定义的 Action 类的**作用域为“原型”** 。 Spring 提供的 bean 的默认范围是单例,并且必须为每个请求创建新的 Struts Action 类,因为它包含特定于用户会话的数据。 为了容纳两者,请将 Action 类 bean 标记为原型。
**c)**在运行此应用之前,请不要忘记**设置数据库**。 如果安装不正确,将导致您出现一些异常。
**c)**在运行此应用之前,请不要忘记**设置数据库**。 如果安装不正确,将导致您出现一些异常。
**d)**另外,请**在项目运行时依赖项中也包括** `struts2-spring-plugin`
**d)**另外,请**在项目运行时依赖项中也包括**`struts2-spring-plugin`
## 6)教程中使用的数据库架构
下表已在 MySQL 中的名为“ test”的数据库中创建。
下表已在 MySQL 中的名为“`test`”的数据库中创建。
```java
CREATE TABLE EMPLOYEE
......@@ -619,13 +621,13 @@ CREATE TABLE EMPLOYEE
![directory structure for spring struts hibernate integration](img/72fb9341ae56b76bae73c5c72b7d7b06.png)
Directory structure for spring struts hibernate integration
Spring Struts Hibernate 集成的目录结构
此项目中使用的`pom.xml`文件具有所有项目相关性(有些额外),如下所示:
**pom.xml**
**`pom.xml`**
```java
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
......@@ -843,12 +845,12 @@ Directory structure for spring struts hibernate integration
## 7)下载源代码
下载以上示例的源代码或获取.war 文件。
下载以上示例的源代码或获取`.war`文件。
[下载源码](https://drive.google.com/file/d/0B7yo2HclmjI4S2c5YldiNk44aVE/edit?usp=sharing "spring + struts + hibernate tutorial source code link")
[Download .war File](https://drive.google.com/file/d/0B7yo2HclmjI4T2NrS19iRU5Hb3c/edit?usp=sharing "spring + struts + hibernate tutorial .war file link")
[下载`.war`文件](https://drive.google.com/file/d/0B7yo2HclmjI4T2NrS19iRU5Hb3c/edit?usp=sharing "spring + struts + hibernate tutorial .war file link")
这是 **spring 4 + Struts2 + hibernate 集成**教程的全部内容。 让我知道您的想法和疑问。
这是 **Spring4 + Struts2 + hibernate 集成**教程的全部内容。 让我知道您的想法和疑问。
**祝您学习愉快!**
\ No newline at end of file
......@@ -18,7 +18,7 @@
2. 之后,使用初始上下文对象查找队列对象。
3. 同样,我们将使用初始上下文对象来查找队列连接工厂。
4. 然后,使用队列连接工厂来创建队列连接,因为它表示 JMS 服务器的物理连接。
5. 创建队列连接工厂后,我们将创建队列会话,其中第一个参数将决定会话是否被事务处理。 但是,我们将使用非交易会话。
5. 创建队列连接工厂后,我们将创建队列会话,其中第一个参数将决定会话是否被事务处理。 但是,我们将使用非事务会话。
6. 此后,为队列创建一个队列发送者,然后创建一条消息。
7. 然后将诸如“`Hello World`”之类的消息发送到队列对象。
8. 关闭队列连接后,关闭队列连接后,它将自动关闭会话和队列发送者。
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册