spring之整合struts2

首先将以下jar包加入到lib文件夹中:

基础目录:

Person.java

package com.gong.spring.struts2.beans;

public class Person {

    private String username;

    public void setUsername(String username) {
        this.username = username;
    }

    public void hello(){
        System.out.println("My name is " + username);
    }

}

PersonService.java

package com.gong.spring.struts2.services;

public class PersonService {

    public void save(){
        System.out.println("PersonService‘s save....");
    }

}

PersonAction.java

package com.gong.spring.struts2.actions;

import com.gong.spring.struts2.services.PersonService;

public class PersonAction {

    private PersonService personService;

    public void setPersonService(PersonService personService) {
        this.personService = personService;
    }

    public String execute(){
        System.out.println("execute....");
        personService.save();
        return "success";
    }

}

基本流程如下:在PersonAction装配PersonService,在execute方法中打印相关信息并调用personService的save方法,最后返回"success"。在PersonService中的save方法输出一句话。

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="person"
        class="com.gong.spring.struts2.beans.Person">
        <property name="username" value="spring"></property>
    </bean>

    <bean id="personService"
        class="com.gong.spring.struts2.services.PersonService"></bean>

    <!-- 注意: 在 IOC 容器中配置 Struts2 的 Action 时, 需要配置 scope 属性, 其值必须为 prototype -->
    <bean id="personAction"
        class="com.gong.spring.struts2.actions.PersonAction"
        scope="prototype">
        <property name="personService" ref="personService"></property>
    </bean>

</beans>

在applicationContext中配置相关bean。

stuts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />

    <package name="default" namespace="/" extends="struts-default">

        <!--
            Spring 整合 Struts2 时, 在 Struts2 中配置的 Spring 的 Action 的 class 需要指向 IOC 容器中该 bean 的 id
        -->
        <action name="person-save" class="personAction">
            <result>/success.jsp</result>
        </action>

    </package>

</struts>

在struts.xml中配置action时,class需要使用applicationContext.xml中bean的id。结果返回给success.jsp。

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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

    <!-- 配置 Spring 配置文件的名称和位置 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    <!-- 启动 IOC 容器的 ServletContextListener -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- 配置 Struts2 的 Filter -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

在web.xml中需要两个部分:一个是让springIOC容器在web应用服务加载时就进行创建。另一个就是配置struts2的过滤器。

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

    <a href="person-save">Person Save</a>

</body>
</html>

sucess.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

    <h4>Success Page</h4>

</body>
</html>

test.jsp

<%@page import="com.gong.spring.struts2.beans.Person"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="org.springframework.context.ApplicationContext"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

    <%
        //1. 从 appication 域对象中得到 IOC 容器的实例
        ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(application);

        //2. 从 IOC 容器中得到 bean
        Person person = ctx.getBean(Person.class);

        //3. 使用 bean
        person.hello();
    %>

</body>
</html>

启动tomacat服务器之后:

点击Person Save:

会跳转到succes.jsp,并在控制台输出相应的语句。

说明spring整合struts2基本是成功的了。

原文地址:https://www.cnblogs.com/xiximayou/p/12173239.html

时间: 2024-08-02 13:56:06

spring之整合struts2的相关文章

Spring框架整合Struts2使用Validation框架验证表单用户输入数据的详细教程

原创整理不易,转载请注明出处:Spring框架整合Struts2使用Validation框架验证表单用户输入数据的详细教程 代码下载地址:http://www.zuidaima.com/share/1778685765291008.htm 在<Struts2教程4:使用validate方法验证数据>中曾讲到使用validate方法来验证客户端提交的数据,但如果使用validate方法就会将验证代码和正常的逻辑代码混在一起,但这样做并不利于代码维护,而且也很难将过些代码用于其他程序的验证.在St

Spring笔记⑥--整合struts2

Spring如何在web应用里面用 需要额外加入的jar包 Spring-web-4.0.0 Spring-webmvc-4.0.0 Spring的配置文件,没什么不同 ? 需要在web.xml下配置,使用myeclipse2014可自动生成 ? <!-- 启动ioc容器的servletcontextLin --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListen

Spring框架整合Struts2

1,用Spring架构,及Struts2-spring-plugin插件 导入Spring的dist全部所需的jar包 Struts2的spring插件 struts2-spring-plugin.XX.jar struts2的核心包 struts2-core.XX.jar xwork- core.XX.jar commons.logging.XX.jar ...根据需要选择导入 2,配置web.xml中applicationContext.xml的参数路径及侦听器 <context-param

Spring整合Struts2

Spring整合Struts21整合目的:让Spring的IOC容器去管理Struts2的Action, 2Struts2是web开源框架,Spring要整合Struts2,也就是说要在web应用使用Spring①. 需要额外加入的 jar 包:spring-web-4.0.0.RELEASE.jarspring-webmvc-4.0.0.RELEASE.jar ②. Spring 的配置文件, 和非 WEB 环境没有什么不同 ③. 需要在 web.xml 文件中加入如下配置: <!-- 配置

struts2+hibernate+spring简单整合且java.sql.SQLException: No suitable driver 问题解决

最近上j2ee的课,老师要求整合struts2+hibernate+spring,我自己其实早早地有准备弄的,现在都第9个项目了,无奈自己的思路和头绪把自己带坑了,当然也是经验问题,其实只是用myeclipse进行整合的,本来也没那么多问题,看视频吧居然好多要手打,我不喜欢看不下去放弃了,教程把就是一堆坑,最最让人不解的是明明有一个冲突是需要解决的,但我看到的教程居然都没有提到,还有一个错误居然好多人都好像自动忽略一样,能解决我问题的都是要漫长的找,所以我一定一定要把这个过程记录下来,给第一次搞

Spring与Struts2整合VS Spring与Spring MVC整合

Spring与Struts2整合,struts.xml在src目录下 1.在web.xml配置监听器 web.xml <!-- 配置Spring的用于初始化ApplicationContext的监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <contex

Spring学习(八)spring整合struts2

一.spring框架对struts等表现层框架的整合原理 : 使用spring的ioc容器管理struts中用于处理请求的Action 将Action配置成ioc容器中的bean 延伸:spring对持久层框架/技术的整合原理 (封装) : 提供模板类封装对应技术/框架的开发流程 通过对模板类的使用,实现对传统开发流程的"代替". 二.整合方式: 插件方式 struts2为了实现对spring框架整合,也提供了一个插件的配置文件struts-plugin.xml struts2-spr

struts2+hibernate-jpa+Spring+maven 整合(2)

之前的一篇已经讲到了 spring 与struts2 的整合, 其实对于struts2+hibernate-jpa+Spring 之间的整合的文章已经相当多了, 也相当成熟了,只要不是各自的版本不兼容之外,其他的几乎没啥问题, 不行mybatis那样是不是的冒点让人头疼的事情来 下面修改pom.xml ,把hibernate 的jar 关联进来; <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="

整合Struts2框架和Spring框架

-----------------------siwuxie095 整合 Struts2 框架和 Spring 框架 1.导入相关 jar 包(共 27 个) (1)导入 Struts2 的基本 jar 包(13 个) 其中: 在 Struts2 和 Hibernate 中,都有 javassist,会产生冲突, 选择高版本,删除低版本即可 (2)导入 Spring 的核心 jar 包和日志相关的 jar 包(6 个) Commons Logging 下载链接: http://commons.a