【Java EE】Struts2.1.6与Spring2.5.6框架整合

1、在MyEclipse中新建项目(test)
2、Struts包导入
   暂时导入所必需的包,其他包将在用到时导入:
   commons-fileupload-1.2.1.jar
   commons-logging-1.0.4.jar
   freemarker-2.3.13.jar
   ognl-2.6.11.jar
   struts2-core-2.1.6.jar
   xwork-2.1.2.jar
 3、复制在Struts目录的例子程序中WEB-INF\classes\struts.xml文件,粘贴到项目的src目录下,主要保留其文件头:
 1<?xml version="1.0" encoding="GBK" ?>
 2<!DOCTYPE struts PUBLIC
 3    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
 4    "http://struts.apache.org/dtds/struts-2.0.dtd">
 5
 6<struts>
 7    <package name="struts2" extends="struts-default">
 8
 9    </package>
10</struts>

4、配置web.xml
 1<?xml version="1.0" encoding="UTF-8"?>
 2<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
 3    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 5    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 6
 7    <filter>
 8        <filter-name>struts2</filter-name>
 9        <filter-class>
10            org.apache.struts2.dispatcher.FilterDispatcher
11        </filter-class>
12    </filter>
13    <filter-mapping>
14        <filter-name>struts2</filter-name>
15        <url-pattern>/*</url-pattern>
16    </filter-mapping>
17
18    <welcome-file-list>
19        <welcome-file>index.jsp</welcome-file>
20    </welcome-file-list>
21</web-app>
5、引入Spring包,在dist目录下
   spring.jar
6、在src目录下建立三个文件
   applicationContext-actions.xml
   applicationContext-beans.xml
   applicationContext-common.xml
   这三个文件其实是applicationContext.xml的分解,为的是避免所有配置放在同一文件,造成混乱。
   结构均如下:
 1<?xml version="1.0" encoding="GBK"?>
 2
 3<beans xmlns="http://www.springframework.org/schema/beans"
 4    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5    xmlns:context="http://www.springframework.org/schema/context"
 6    xmlns:tx="http://www.springframework.org/schema/tx"
 7    xmlns:aop="http://www.springframework.org/schema/aop"
 8    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 9                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
10                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 
11                http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
12
13</beans>

7、需要在web.xml进行配置
    <context-param>
        
<param-name>contextConfigLocation</param-name>
        
<param-value>classpath*:applicationContext-*.xml</param-value>
    
</context-param>

    <listener>
        
<listener-class>
            org.springframework.web.context.ContextLoaderListener
        
</listener-class>
    
</listener>
   前一段代码的配置是因为我们配置了后一段代码的配置后它默认是到WEB-INF下查找applicationContext.xml文件,我们现在改到src目录下并进行文件分解。

8、需要引入Struts2中的另一个包
   struts2-spring-plugin-2.1.6.jar
9、测试是否整合成功
(1)建立页面login.jsp、welcome.jsp、error.jsp分别为登录页面、登录成功页面、出错页面
login.jsp
 1<%@ page language="java" contentType="text/html; charset=GB18030"
 2    pageEncoding="GB18030"
%>
 3<%@ taglib prefix="s" uri="/struts-tags"%>
 4<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5<html>
 6    <head>
 7        <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
 8        <title>登录页面</title>
 9    </head>
10    <body>
11        <s:form action="login" method="post">
12            <s:textfield name="username" label="username" />
13            <s:password name="password" label="password" />
14            <s:submit value="submit" />
15        </s:form>
16    </body>
17</html>
welcome.jsp
 1<%@ page language="java" contentType="text/html; charset=GB18030"
 2    pageEncoding="GB18030"
%>
 3<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4<html>
 5    <head>
 6        <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
 7        <title>登录成功</title>
 8    </head>
 9    <body>
10        用户名:${username }
11        <br>
12        密码:${password }
13        <br>
14    </body>
15</html>

(2)建包com.test.manager和com.test.manager.impl分别存放业务逻辑处理的接口和其实现,分别建立接口LoginManager.java和其实现LoginManagerImpl.java
LoginManager.java
1package com.test.manager;
2
3public interface LoginManager {
4    public boolean isLogin(String username, String password);
5}
LoginManagerImpl.java,只是测试用,判断用户名密码是否为intrl、intrl,若是则登录成功
 1package com.test.manager.impl;
 2
 3import com.test.manager.LoginManager;
 4
 5public class LoginManagerImpl implements LoginManager{
 6    public boolean isLogin(String username, String password)
 7    {
 8        if(null!=username&&null!=password&&"intrl".equals(username.trim())&&"intrl".equals(password.trim()))
 9        {
10            return true;
11        }

12        return false;
13    }

14}
(3)在applicationContext-beans.xml把实现类配置上,以让Spring进行管理
    <bean id="loginManager"
        class
="com.test.manager.impl.LoginManagerImpl">
    
</bean>
(4)创建包com.test.action用于存放action,并新建LoginAction.java,继承ActionSupport类
包含从页面所接收参数username、password,以及业务逻辑处理类LoginManager类型的loginManager,给username和password设置get、set,给loginManager设置set方法,以让Spring为我们自动注入;overwrite父类中的
 1package com.test.action;
 2
 3import com.opensymphony.xwork2.ActionSupport;
 4import com.test.manager.LoginManager;
 5
 6@SuppressWarnings("serial")
 7public class LoginAction extends ActionSupport {
 8    private LoginManager loginManager;
 9    private String username;
10    private String password;
11
12    public String getUsername() {
13        return username;
14    }

15
16    public void setUsername(String username) {
17        this.username = username;
18    }

19
20    public String getPassword() {
21        return password;
22    }

23
24    public void setPassword(String password) {
25        this.password = password;
26    }

27
28    public void setLoginManager(LoginManager loginManager) {
29        this.loginManager = loginManager;
30    }

31    
32    @Override
33    public String execute() throws Exception {
34
35        if(loginManager.isLogin(username, password))
36        {
37            return SUCCESS;
38        }

39        return INPUT;
40    }

41}
(5)在applicationContext-actions.xml中进行配置,也让Spring来管理LoginAction
    <bean id="loginAction" class="com.test.action.LoginAction"
        scope
="prototype">
        
<property name="loginManager" ref="loginManager"></property>
    
</bean>
(6)在struts.xml中进行配置,处理页面提交的请求,配置action:login,login一定要和login.jsp中form的action属性名匹配。此时struts.xml文件如下:
 1<?xml version="1.0" encoding="GBK" ?>
 2<!DOCTYPE struts PUBLIC
 3    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
 4    "http://struts.apache.org/dtds/struts-2.0.dtd">
 5
 6<struts>
 7    <package name="struts2" extends="struts-default">
 8        <action name="login" class="loginAction">
 9            <result name="success">welcome.jsp</result>
10            <result name="input">login.jsp</result>
11            <result name="error">error.jsp</result>
12        </action>
13    </package>
14</struts>

(7)此时目录结构如下:

(8)部署项目Tomcat服务器,启动服务器,进入登录页面,进行登录测试:
若用户名密码不为intrl/intrl,则登录失败,返回登录页面,注意地址栏的变化

若输入intrl/intrl则登录成功,跳至welcome.jsp页面,显示用户名和密码

10、以上结果证明,Struts2.1.6与Spring2.5.6框架整合成功

11、源代码下载
http://www.rayfile.com/files/b82cd5e3-66b2-11de-836f-0014221b798a/

12、其他资源
struts-2.1.6-all.zip:http://www.rayfile.com/files/d71417ae-66dd-11de-9d35-0014221b798a/
spring-framework-2.5.6-with-dependencies.zip:http://www.rayfile.com/files/6819bd23-66e2-11de-ad79-0014221b798a/
jdk-6u12-windows-i586-p.exe:http://www.rayfile.com/files/2879e173-66f0-11de-9cd8-0014221b798a/
apache-tomcat-6.0.16.exe:http://www.rayfile.com/files/918febc7-66ed-11de-ab58-0014221b798a/
mysql\MYSQL123 5 5.0.zip:http://www.rayfile.com/files/dee8bc17-66f1-11de-a255-0014221b798a/

posted on 2009-04-12 16:38 intrl 阅读(6963) 评论(27)  编辑 收藏 引用 所属分类: JavaStruts2Spring

评论

# re: 【Java EE】Struts2.1.6与Spring2.5.6框架整合 2009-06-02 11:44 王强

能告诉我index.jsp文件中的代码吗?  回复  更多评论   

# re: 【Java EE】Struts2.1.6与Spring2.5.6框架整合 2009-06-02 12:35 王强

踏着先辈的足迹,想整合成功Struts2.1.6与Spring2.5.6框架.遗憾的是高了好久问题还是解决不了.请您帮帮忙看看.

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"">http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
<init-param>
<param-name>actionPackages</param-name>
<param-value>com.cn.ps.pcsell.action</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>/WEB-INF/jsp/login.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
  回复  更多评论   

# re: 【Java EE】Struts2.1.6与Spring2.5.6框架整合 2009-06-02 12:36 王强

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.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" />
<constant name="struts.codebehind.pathPrefix" value="/WEB-INF/jsp/" />
<package name="struts2" extends="struts-default">
<action name="login" class="loginAction">
<result name="success">welcome.jsp</result>
<result name="input">login.jsp</result>
<result name="error">error.jsp</result>
</action>
</package>
</struts>  回复  更多评论   

# re: 【Java EE】Struts2.1.6与Spring2.5.6框架整合 2009-06-02 12:37 王强

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

<bean id="loginAction" class="com.cn.ps.pcsell.action.LoginAction"
scope="prototype">
<property name="loginLogic" ref="loginLogic"></property>
</bean>

</beans>  回复  更多评论   

# re: 【Java EE】Struts2.1.6与Spring2.5.6框架整合 2009-06-02 12:39 王强

2009-6-2 12:08:36 com.opensymphony.xwork2.util.logging.commons.CommonsLogger error
严重: Could not open template
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginAction' defined in file [D:\peace\workspace\PCSell\WebRoot\WEB-INF\classes\applicationContext-actions.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'loginLogic' of bean class [com.cn.ps.pcsell.action.LoginAction]: Bean property 'loginLogic' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1279)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1010)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native Method)  回复  更多评论   

# re: 【Java EE】Struts2.1.6与Spring2.5.6框架整合 2009-06-02 12:47 王强

login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ 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>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>登录页面</title>
</head>
<body>
<s:form action="login" method="post">
<s:textfield name="username" label="username" />
<s:password name="password" label="password" />
<s:submit value="submit" />
</s:form>
</body>
</html>  回复  更多评论   

# re: 【Java EE】Struts2.1.6与Spring2.5.6框架整合 2009-06-02 21:59 intrl

index.jsp里面没有内容,显示登陆页面时直接用地址栏访问(http://localhost:8888/SSTest/login.jsp)@王强
  回复  更多评论   

# re: 【Java EE】Struts2.1.6与Spring2.5.6框架整合 2009-06-02 22:05 intrl

您的代码都没错误,可能是环境问题
我给你一下我的环境配置:
MyEclipse版本:Version: 6.6.0
Struts2.1.6
Spring2.5.6

您可以给下邮箱吗?我可以把这个整合的源代码发给你,你试运行下!
如果一步步跟着做下来是没问题的。
@王强
  回复  更多评论   

# re: 【Java EE】Struts2.1.6与Spring2.5.6框架整合 2009-06-02 22:07 intrl

Tomcat服务器都没问题是吧?@王强
  回复  更多评论   

# re: 【Java EE】Struts2.1.6与Spring2.5.6框架整合 2009-06-08 05:51 王强

能得到源代码太好了.我的邮箱是:peace_c@live.cn
麻烦您了。
  回复  更多评论   

# re: 【Java EE】Struts2.1.6与Spring2.5.6框架整合 2009-06-08 12:50 intrl

不客气,邮件已发送!@王强
  回复  更多评论   

# re: 【Java EE】Struts2.1.6与Spring2.5.6框架整合 2009-06-08 20:05 王强

您的代码已经运行成功了,谢谢了。
对了,您用从哪里得到的资料,完成了这个框架整合。
我想试着做一个Struts2.1.6、Spring2.5.6与mysql5.1的一个框架,正愁没有太好的资料。  回复  更多评论   

# re: 【Java EE】Struts2.1.6与Spring2.5.6框架整合 2009-06-08 22:11 intrl

看过一些书籍
浪曦网(上面有挺多免费视频,有时间可以看看)
您找到代码问题所在后,盼分享!谢谢!
@王强
  回复  更多评论   

# re: 【Java EE】Struts2.1.6与Spring2.5.6框架整合 2009-06-24 15:39 wsmp3

目前初学这些东西,能不能给我一份代码,谢谢!!!wsmp3@126.com  回复  更多评论   

# re: 【Java EE】Struts2.1.6与Spring2.5.6框架整合 2009-06-24 15:51 wsmp3

看了一些这方面的视频和图书,下了一个strusts2.1.6按照视频居然没配成功,视频里面就是缺了你文章了的那个jcommons-fileupload-1.2.1.jar。感觉还是自己看错误的能力差,以后最笨的方法就是把struts所有的jar都引入。  回复  更多评论   

# re: 【Java EE】Struts2.1.6与Spring2.5.6框架整合 2009-06-24 21:45 intrl

strusts2.1.6里面有commons-fileupload-1.2.1.jar包的吧!
我已经把源码发到你邮箱了,请注意查收!

@wsmp3
  回复  更多评论   

# re: 【Java EE】Struts2.1.6与Spring2.5.6框架整合 2009-06-25 07:28 wsmp3

收到,谢谢!!!还没看,就上来留言了。Struts2我已经调试通过了,这几天看看怎么整合的。以前看过strus1+spring的,1种在Action中取得BeanFactory对象,然后通过BeanFactory获取业务逻辑对象。1种将业务逻辑对象通过spring注入到Action中,从而避免了在Action类中的直接代码查询。配置都是很多,慢慢看,不明白的地方再请教前辈。  回复  更多评论   

# re: 【Java EE】Struts2.1.6与Spring2.5.6框架整合 2009-06-25 09:57 wsmp3

自己按文章弄了一下,缺什么到前辈给的代码里面找,好使了。主要就是没有error.jsp,还有就是配置return ERROR。下一步要进一步研究一下,struts1用动态代理,这里面还是比较迷糊,直接就注入。下去慢慢理解一下了。  回复  更多评论   

# re: 【Java EE】Struts2.1.6与Spring2.5.6框架整合 2009-06-25 23:40 intrl

别这么说,我也不是前辈,呵呵,也刚学不久!有问题可以一起探讨!

@wsmp3
  回复  更多评论   

# re: 【Java EE】Struts2.1.6与Spring2.5.6框架整合 2009-06-28 16:33 xufan

老兄,也给我一份源码,我配了好些天了都不行,郁闷啊,我邮箱
:xufan8@live.cn  回复  更多评论   

# re: 【Java EE】Struts2.1.6与Spring2.5.6框架整合 2009-09-04 08:22 qiwang06

刚按以上步骤做了一下挺好的,出现预期的结果,谢谢分享!  回复  更多评论   

# re: 【Java EE】Struts2.1.6与Spring2.5.6框架整合 2009-09-09 21:07 hhh

也给我发一份吧,要带包的喔。。。我弄了好长时间了,就是弄不出来,照着你的做的也不行,好像是少了什么包。郁闷死了。

谢谢啦。。。hiyinhh#163.com  回复  更多评论   

# re: 【Java EE】Struts2.1.6与Spring2.5.6框架整合 2009-09-09 21:31 hhh

我运行好了,我又照着做了一次,完全有用。源码就不要了吧。你写的真是太好了,怪不得下面有这么多的人支持。

这个文章绝对够资格上ibm developerworks

有图有真相,无图无真相。我喜欢,我要长期关注你的blog...

再次谢谢。  回复  更多评论   

# re: 【Java EE】Struts2.1.6与Spring2.5.6框架整合 2009-12-05 09:39 学习ing

我是初学者,已经照着做了一次了,运行成功,非常感谢;
还有就是在部署tomcat服务器的时候需要导出test项目到TOMCAT下的webapps文件夹里。
  回复  更多评论   

# re: 【Java EE】Struts2.1.6与Spring2.5.6框架整合 2009-12-08 17:20 王银龙

楼主也给我发一份源码呀,我都搞了一天,都不行,谢谢你了
641224261@qq.com  回复  更多评论   

# re: 【Java EE】Struts2.1.6与Spring2.5.6框架整合 2009-12-11 16:02 旦旦面

整合过程中出现一个问题
The requested resource (No result defined for action com.test.action.LoginAction and result input) is not available
后面把struts.xml 里的ACTION 名字 login 改成别的名字就可以了。这个问题不知道是什么原因产生的。  回复  更多评论   

# re: 【Java EE】Struts2.1.6与Spring2.5.6框架整合 2009-12-15 11:13 intrl

applicationContext-actions.xml配置文件中,loginAction所指向的资源不存在,应该是你这个文件配置了不存在的Action类或方法
@旦旦面
  回复  更多评论   

只有注册用户登录后才能发表评论。
<2009年4月>
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789

导航

统计

随笔分类(55)

随笔档案(34)

网址收藏

资源下载

随笔导航

搜索

最新评论

阅读排行榜

评论排行榜