Struts2 Spring 3集成示例

时间:2020-02-23 14:41:54  来源:igfitidea点击:

在本教程中,我们将看到如何集成Spring 3和Struts 2.现在首次出现其中为什么要使用Struts 2集成Spring2?
Spring提供了一些在Struts中不可用的功能。
在它们中间最强大的是依赖注入。
要了解有关依赖注入的更多信息,可以在Spring 链路中引用依赖注入。

创建名为"Struts2spring3IntegrationExample"的项目。
首先,我们需要将以下文件复制到Web-Inf/lib并将其添加到项目的构建路径中。
我们可以从http://www.springsource.org/download下载并安装最新版本的Spring Framework。

  • org.springframework.asm-3.1.1.1.Release.
  • org.springframework.beans-3.1.1.Release.
  • org.springframework.context-3.1.1.Release.
  • org.springframework.core-3.1.1.Release.
  • org.springframework.expression-3.1.1.1.Release.
  • org.springframework.web-3.1.1.Release.
  • org.springframework.web.servlet-3.1.1.1.Release.

最后将Struts2-Spring-plugin-2.3.1.2复制到下载的Struts2罐中的Web-Inf/lib。

web.xml:

在web.xml中复制以下内容。

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 <display-name>Struts2Spring3IntegrationExample</display-name>
 <filter>
 <filter-name>struts2</filter-name>
 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
 </filter>
 <listener>
 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 <filter-mapping>
 <filter-name>struts2</filter-name>
 <url-pattern>/*</url-pattern>
 </filter-mapping>
 <welcome-file-list>
 <welcome-file>default.jsp</welcome-file>
 </welcome-file-list>
</web-app>

其中我们可以注意到我们已配置侦听器。
org.springframework.web.context.contextLoaderListner需要加载Spring配置文件.By默认值,ApplicationContext.xml文件将用于执行Spring Bean配置,它必须与Web.xml相同。

action类:

在SRC文件夹中的程序包org.igi.javapostsfor learign下创建名为helloworld.java的动作类。

将以下内容复制到HelloWorld.java。

package org.igi.javaPostsForLearning;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorld extends ActionSupport{
    String message;
    public String getMessage() {
  return message;
 }
 
    public void setMessage(String message) {
  this.message = message;
 }
    public String execute()
    {
   return SUCCESS;
    }
}

现在我们将在ApplicationContext.xml中配置上面的操作文件。

applicationcontext.xml.

在WebContent-> Web_Inf中创建ApplicationContext.xml。
将以下内容复制到ApplicationContext.xml。

<?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="helloWorldClass" class="org.igi.javaPostsForLearning.HelloWorld" >
        <property name="message" value="Hello World! from igi" 
    </bean>
</beans>

JSP:

我们将在WebContent文件夹下创建一个名为"welcome.jsp"的一个jsp。
将代码复制到欢迎.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Struts 2 Tutorial</title>
</head>
<body>
<s:property value="message" 
</body>
</html>

struts.xml:

在SRC文件夹中创建Struts.xml。
在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>
    <package name="default" extends="struts-default" namespace="/">
        <action name="HelloWorld" class="helloWorldClass">
            <result name="success">Welcome.jsp</result>
        </action>
    </package>
</struts>

现在我们可以在此处注意到,在标记中,我们将SET类属性与ApplicationContext.xml中配置的bean的ID相同。

运行应用程序:

右键单击"项目" - >在服务器上运行 - >运行。
复制http://localhost:8080/struts2spring3integrationexample/helloworld链接到浏览器,然后按Enter键。