- Struts 2 教程
- Struts2 - 主页
- Struts2 - 基本 MVC 架构
- Struts2 - 概述
- Struts2 - 环境设置
- Struts2 - 架构
- Struts2 - 示例
- Struts2 - 配置
- Struts2 - 动作
- Struts2 - 拦截器
- Struts2 - 结果类型
- Struts2 - 价值堆栈/OGNL
- Struts2 - 文件上传
- Struts2 - 数据库访问
- Struts2 - 发送电子邮件
- Struts2 - 验证
- Struts2 - 本地化
- Struts2 - 类型转换
- Struts2 - 主题/模板
- Struts2 - 异常处理
- Struts2 - 注释
- Struts 2 集成
- Struts2-Spring
- Struts2 - 瓷砖
- Struts2-Hibernate
- Struts 2 有用资源
- Struts2 - 问题与解答
- Struts2 - 快速指南
- Struts2 - 有用的资源
- Struts2 - 讨论
Struts 2 和 Spring 集成
Spring 是一个流行的 Web 框架,可以轻松集成许多常见的 Web 任务。那么问题来了,既然有了Struts2,为什么还需要Spring呢?嗯,Spring 不仅仅是一个 MVC 框架 - 它提供了 Struts 中没有的许多其他优点。
例如:对任何框架都有用的依赖注入。在本章中,我们将通过一个简单的示例来了解如何将 Spring 和 Struts2 集成在一起。
首先,您需要将以下文件从 Spring 安装添加到项目的构建路径中。您可以从https://www.springsource.org/download下载并安装最新版本的 Spring Framework
- org.springframework.asm-xyzM(a).jar
- org.springframework.beans-xyzM(a).jar
- org.springframework.context-xyzM(a).jar
- org.springframework.core-xyzM(a).jar
- org.springframework.expression-xyzM(a).jar
- org.springframework.web-xyzM(a).jar
- org.springframework.web.servlet-xyzM(a).jar
最后将struts2-spring-plugin-xyzjar添加到struts lib 目录中的WEB-INF/lib中。如果您使用 Eclipse,那么您可能会遇到异常java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener。
要解决此问题,您应该进入“标记”选项卡,然后逐一右键单击类依赖项,然后执行“快速修复”以发布/导出所有依赖项。最后确保标记选项卡下没有可用的依赖冲突。
现在让我们为 Struts-Spring 集成设置web.xml,如下所示 -
<?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文件放在同一级别
让我们创建一个名为User.java 的简单操作类,它具有两个属性 - firstName 和 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 文件夹下 -
<?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>
如上所示,我们已经配置了用户 bean,并将值Michael和Jackson注入到该 bean 中。我们还给这个 bean 命名为“userClass”,以便我们可以在其他地方重用它。接下来让我们在 WebContent 文件夹中创建User.jsp -
<%@ 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文件非常简单。它仅用于一个目的 - 显示用户对象的名字和姓氏的值。最后,让我们使用struts.xml文件将所有实体放在一起。
<?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 类进行依赖注入。
现在右键单击项目名称,然后单击“导出”>“WAR 文件”以创建一个 War 文件。然后将此 WAR 部署到 Tomcat 的 webapps 目录中。最后,启动 Tomcat 服务器并尝试访问 URL http://localhost:8080/HelloWorldStruts2/User.jsp。这将产生以下屏幕 -
我们现在已经了解了如何将两个出色的框架结合在一起。Struts - Spring 集成章节到此结束。