java struct 简单案列_spring与struts简单整合案例

Spring,负责对象对象创建

Struts, 用Action处理请求

Spring与Struts框架整合,

关键点:让struts框架action对象的创建,交给spring完成!

步骤:

1)引入struts .jar相关文件

a、引入struts .jar相关文件

commons-fileupload-1.2.2.jar

commons-io-2.0.1.jar

commons-lang3-3.1.jar

freemarker-2.3.19.jar

javassist-3.11.0.GA.jar

ognl-3.0.5.jar

struts2-core-2.3.4.1.jar

xwork-core-2.3.4.1.jar

b、spring-core  相关jar文件

commons-logging-1.1.3.jar

spring-beans-3.2.5.RELEASE.jar

spring-context-3.2.5.RELEASE.jar

spring-core-3.2.5.RELEASE.jar

spring-expression-3.2.5.RELEASE.jar

c、spring-web 支持jar包

spring-web-3.2.5.RELEASE.jar                   【Spring源码】

struts2-spring-plugin-2.3.4.1.jar      【Struts源码】

2)配置:

struts.xml                   【struts路径与action映射配置】

/p>

"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"

"http://struts.apache.org/dtds/struts-2.3.dtd">

/index.jsp

bean.xml               【spring ioc容器配置】

a . bean-dao.xml

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:p="http://www.springframework.org/schema/p"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx.xsd">

b、bean-service.xml 同上,将对象加入IOC容器

c、bean-action.xml

web.xml

【核心过滤器: 引入struts功能】

【初始化spring的ioc容器】

具体配置可 以查看源码的api

struts2

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

struts2

/*

contextConfigLocation

WEB-INF/classes/bean-*.xml

org.springframework.web.context.ContextLoaderListener

3)、代码

项目截图

23e4ac91de3a2d93649d310c9102edd4.png

userDao

package com.gqx.dao;

public class UserDao {

public void save() {

System.out.println("DB:保存用户");

}

}

userService

public class UserService {

//IOC容器注入

private UserDao userDao;

public void setUserDao(UserDao userDao) {

this.userDao = userDao;

}

public void save() {

userDao.save();

}

}

userAction

public class UserAction extends ActionSupport {

// springIOC容器注入

private UserService userService;

public void setUserService(UserService userService) {

this.userService = userService;

}

public String execute() {

userService.save();

return SUCCESS;

}

}

4)、访问http://localhost:8080/struts-spring/user

就可以看到控制台如下的信息

2afe4fecc26a8707a3e3528385508d42.png

Bug

按照

java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener 错误解决方案

种错误意思很明确:找不到“ org.springframework.web.context.ContextLoaderListener”这个类,ContextLoaderListener这个类是在spring-web.jar包下,我仔细检查了项目jar环境,发现该jar包确实存在,而且也能找到编译后的ContextLoaderListener.class文件。

在网上找到的回答解决了问题,如下

如果你是maven项目,tomcat在发布项目的时候没有同时发布maven依赖所添加的jar包,

你需要设置一下eclipse:

项目 —> 属性 -> Deployment Assembly -> Add -> Java Build Path Entries -> 选择Maven Dependencies -> Finish -> OK

把对应的Maven依赖包也发布到tomcat,调试时会自动把那些jar发布到指定目录下,tomcat也能找到那些jar了。

网上有去查了一下原因,如下:

Java虚拟机是根据Java ClassLoader(类加载器)决定如何加载Class。

系统默认提供了3个ClassLoader

Root ClassLoader,ClassPath Loader,Ext ClassLoader

我们也可以编写自己的ClassLoader,去加载特定环境下的Jar文件。

能不能加载Jar,加载哪里的Jar,是由ClassLoader决定的。

问题可能是 导入的仅仅是jar包的引用,需要特殊的指定(类似快捷方式)

如果是通过正常的WEB-INF/lib下引入jar包,会正常的显示,默认会buildPath,

如果是通过项目->属性->java Build Path->Add library->user Library....这种方式导入包,会出现上面那种错误,需要按上面的方法去解决!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值