Struts 2 简明教程
Struts 2 & Spring Integration
Spring 是一个流行的 Web 框架,它提供了与许多常见 Web 任务的简单集成。因此问题是,当我们有了 Struts2 时,我们为什么需要 Spring?好吧,Spring 不仅仅是一个 MVC 框架 - 它提供了 Struts 中没有的许多其他好处。
Spring is a popular web framework that provides easy integration with lots of common web tasks. So the question is, why do we need Spring when we have Struts2? Well, Spring is more than a MVC framework - it offers many other goodies which are not available in Struts.
例如:依赖项注入对于任何框架都很有用。在本章中,我们将通过一个简单的示例了解如何将 Spring 和 Struts2 集成在一起。
For example: dependency injection that can be useful to any framework. In this chapter, we will go through a simple example to see how to integrate Spring and Struts2 together.
首先,你需要从 Spring 安装将以下文件添加到项目的构建路径中。你可以从 https://www.springsource.org/download 下载并安装最新版本的 Spring 框架
First of all, you need to add the following files to the project’s build path from Spring installation. You can download and install latest version of Spring Framework from https://www.springsource.org/download
-
org.springframework.asm-x.y.z.M(a).jar
-
org.springframework.beans-x.y.z.M(a).jar
-
org.springframework.context-x.y.z.M(a).jar
-
org.springframework.core-x.y.z.M(a).jar
-
org.springframework.expression-x.y.z.M(a).jar
-
org.springframework.web-x.y.z.M(a).jar
-
org.springframework.web.servlet-x.y.z.M(a).jar
最后在 struts lib 目录中,在 struts2-spring-plugin-x.y.z.jar 中添加 WEB-INF/lib 。如果您正在使用 Eclipse,您可能会遇到一个异常 java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener。
Finally add struts2-spring-plugin-x.y.z.jar in your WEB-INF/lib from your struts lib directory. If you are using Eclipse then you may face an exception java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener.
要解决此问题,您应该在 Marker 选项卡中右键单击类依赖项,然后逐一执行快速修复来发布/导出所有依赖项。最后,确保标记选项卡中没有可用的依赖冲突。
To fix this problem, you should have to go in Marker tab and righ click on the class dependencies one by one and do Quick fix to publish/export all the dependences. Finally make sure there is no dependency conflict available under the marker tab.
现在,让我们按照以下步骤为 Struts-Spring 集成设置 web.xml −
Now let us setup the web.xml for the Struts-Spring integration as follows −
<?xml version = "1.0" Encoding = "UTF-8"?>
<web-app xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns = "http://java.sun.com/xml/ns/javaee"
xmlns:web = "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id = "WebApp_ID" version = "3.0">
<display-name>Struts 2</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
这里需要注意的重要一点是我们配置的侦听器。需要 ContextLoaderListener 来加载 Spring 上下文文件。Spring 的配置文件被称为 applicationContext.xml 文件,它必须与 web.xml 文件位于同级
The important thing to note here is the listener that we have configured. The ContextLoaderListener is required to load the spring context file. Spring’s configuration file is called applicationContext.xml file and it must be placed at the same level as the web.xml file
让我们创建一个名为 User.java 的简单动作类,其中包含两个属性 - firstName 和 lastName。
Let us create a simple action class called User.java with two properties - firstName and lastName.
package com.tutorialspoint.struts2;
public class User {
private String firstName;
private String lastName;
public String execute() {
return "success";
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
现在,让我们创建 applicationContext.xml spring 配置文件并实例化 User.java 类。如前所述,此文件应位于 WEB-INF 文件夹下 −
Now let us create the applicationContext.xml spring configuration file and instantiate the User.java class. As mentioned earlier, this file should be under the WEB-INF folder −
<?xml version = "1.0" Encoding = "UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id = "userClass" class = "com.tutorialspoint.struts2.User">
<property name = "firstName" value = "Michael" />
<property name = "lastName" value = "Jackson" />
</bean>
</beans>
如上所示,我们已经配置了 user bean,并且我们已经将值 Michael 和 Jackson 注入到了 bean 中。我们还给这个 bean 起了个名字 “userClass”,以便我们可以在其他地方重复使用它。接下来,让我们在 WebContent 文件夹中创建 User.jsp −
As seen above, we have configured the user bean and we have injected the values Michael and Jackson into the bean. We have also given this bean a name "userClass", so that we can reuse this elsewhere. Next let us create the User.jsp in the WebContent folder −
<%@ page language = "java" contentType = "text/html; charset = ISO-8859-1"
pageEncoding = "ISO-8859-1"%>
<%@ taglib prefix = "s" uri = "/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h1>Hello World From Struts2 - Spring integration</h1>
<s:form>
<s:textfield name = "firstName" label = "First Name"/><br/>
<s:textfield name = "lastName" label = "Last Name"/><br/>
</s:form>
</body>
</html>
User.jsp 文件非常简单。它只起到一个作用 - 显示用户对象的 firstname 和 lastname 的值。最后,让我们使用 struts.xml 文件将所有实体放在一起。
The User.jsp file is pretty straight forward. It serves only one purpose - to display the values of the firstname and lastname of the user object. Finally, let us put all entities together using the struts.xml file.
<?xml version = "1.0" Encoding = "UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name = "struts.devMode" value = "true" />
<package name = "helloworld" extends = "struts-default">
<action name = "user" class="userClass"
method = "execute">
<result name = "success">/User.jsp</result>
</action>
</package>
</struts>
需要注意的重要一点是,我们正在使用 id userClass 来引用类。这意味着我们正在使用 Spring 来对 User 类进行依赖注入。
The important thing to note is that we are using the id userClass to refer to the class. This means that we are using spring to do the dependency injection for the User class.
现在,右键单击项目名称,然后单击 Export > WAR File 来创建一个 War 文件。然后,将该 WAR 部署在 Tomcat 的 webapps 目录中。最后,启动 Tomcat 服务器并尝试访问 URL http://localhost:8080/HelloWorldStruts2/User.jsp 。这将生成以下屏幕 −
Now right click on the project name and click Export > WAR File to create a War file. Then deploy this WAR in the Tomcat’s webapps directory. Finally, start Tomcat server and try to access URL http://localhost:8080/HelloWorldStruts2/User.jsp. This will produce the following screen −
我们现在已经了解了如何将两个伟大的框架结合在一起。这就结束了 Struts - Spring 集成章节。
We have now seen how to bring two great frameworks together. This concludes the Struts - Spring integration chapter.