您的位置:首页 > 编程语言 > Java开发

struts2整合spring应用实例

2013-02-11 17:18 531 查看
   我们知道struts1与spring整合是靠org.springframework.web.struts.DelegatingActionProxy来实现的,以下通过具体一个用户登录实现来说明struts2整合spring的相关内容.

一、准备工作
1.实例分析我们在这不与数据库打交道,所有就是当用登录的时候判断用户名是否为指定值,密码是否为指定值,以及相关的异常处理、

2.为什么我们要说struts2整合spring呢?相信在家都知道,我也不用多说了....

4.在 http://struts.apache.org/download.cgi#struts212下载struts2的jar包,源码,API文档.

5.在 http://static.springframework.org/downloads/nightly/release-download.php下载不同版本的spring的jar包,源码,API文档.
6.配置开发环境:MyEclipse6.0+Eclipse3.3+JDK6.0+Tomcat6.0+Struts 2.0.11+spring2.0。

7.新建web项目,导入相应的jar包,如以下所示:

a.由于现在IDE开发工具还没有对struts2.0有很好的支持,所有我们需要手功配置,首先将我们刚下下来的struts2.0的lib里面的commons-logging-1.0.4.jar、ognl-2.6.11.jar、xwork-2.0.4.jar、freemarker-2.3.8.jar、struts2-core-2.0.11.1.jar以及struts2.0里面所需要的插件包struts2-spring-plugin-2.0.11.1.jar添加的WEB-INF/lib下面
b.通过通过IDE开发工具项目对spring2.0的支持

7.在src下建立struts.xml文件(具体的写法在后面呈现)

8.配置web.xml,如下:


<?xml version="1.0" encoding="UTF-8"?>


<web-appversion="2.5"
xmlns="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">





<!-- 加载struts2核心
-->


<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>




<!-- 指明spring配置文件在何处-->


<context-param>


<param-name>contextConfigLocation</param-name>


<param-value>classpath*:applicationContext.xml</param-value>


</context-param>




<!-- 加载spring配置文件applicationContext.xml-->


<listener>


<listener-class>


org.springframework.web.context.ContextLoaderListener


</listener-class>


</listener>


</web-app>



二、建立前台页面
1.用户登录肯定有一个用户登录页面login.jsp,如下:




<%

@
page language="java"
pageEncoding="GB2312"%>




<%

@
taglib prefix="s"
uri="/struts-tags"%>




<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//CN">


<html>


<head>


<title>login2</title>


</head>




<body>


<s:form
name="login" action="login" method="post">


<s:textfield
name="username" label="帐号"></s:textfield>


<s:password
name="password" label="密码"></s:password>


<s:submit></s:submit>


</s:form>


</body>


</html>



2.若登录成功的index.jsp文件,如下:




<%

@
page language="java"
pageEncoding="GB2312"%>




<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//CN">


<html>


<head>


<title>login2</title>


</head>




<body>


<div>


<h4>欢迎你!</h4><fontcolor="red">${username}</font>


<br/>


<h4>你登录的密码是<h4><fontcolor="red">${password}</font>;


</div>


</body>


</html>



3、用户名非法提示页面.usernameInvalid.jsp(通过el表达示得到异常信息)




<%

@
page language="java"
contentType="text/html; charset=GB18030"


pageEncoding="GB18030"%>




<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">


<html>


<head>


<metahttp-equiv="Content-Type"
content="text/html; charset=GB18030">


<title>Insert title here</title>


</head>


<body>


用户名非法:<fontcolor="red">${exception.message}</font>


</body>


</html>
4、密码非法提示页面.passwordInvalid.jsp(struts2标签得到异常信息);




<%

@
page language="java"
contentType="text/html; charset=GB18030"


pageEncoding="GB18030"%>




<%

@
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>


<metahttp-equiv="Content-Type"
content="text/html; charset=GB18030">


<title>Insert title here</title>


</head>


<body>


密码非法:<FONTcolor="red"><s:propertyvalue="exception.message"/></FONT>


</body>


</html>

三、建立对应的action

1.提供用户请求服务的LoginService类


package org.topCSA.s2s.service;




import org.topCSA.s2s.exception.PasswordException;


import org.topCSA.s2s.exception.UsernameException;




publicclass LoginService






{




/**//*


* 我们这只是一个小的例子,不与数据库打交到


* 若有数据库操作,那么在这个LoginService就是操作具体Dao类实现登录的相关操作


*/


public
boolean validate(String username,String password)throws Exception






{


boolean v =
false;


if(!"xcp".equals(username))//如果用户名不等于xcp,就抛出一个异常






{


throw
new UsernameException("用户名不正确");


}


else
if(!"123".equals(password))))//如果密码不等于123,就抛出一个异常






{


throw
new PasswordException("密码不正确");


}


else






{


v =true;


}


return v;


}


}



2.接收用户请求的LoginAction类


package org.topCSA.s2s.action;




import org.topCSA.s2s.service.LoginService;




import com.opensymphony.xwork2.ActionSupport;




publicclass LoginAction
extends ActionSupport






{






/**
*//**


*


*/


private
staticfinal
long serialVersionUID = 1L;




private String username;


private String password;






/**//*


* 我们通过Spring的IOC容器注入LoginService,从而减少类之间的依赖关系


*/


private LoginService loginService;




public LoginService getLoginService()






{


return loginService;


}


public
void setLoginService(LoginService loginService)






{


this.loginService = loginService;


}


public String getUsername()






{


return username;


}


public
void setUsername(String username)






{


this.username = username;


}


public String getPassword()






{


return password;


}


public
void setPassword(String password)






{


this.password = password;


}






@Override


public
void validate()






{




/**//*


* 我们可以在这个方法类加一些输入验证 但是为了体现后面我们写的业务逻辑方法这就不验证


*/


}




@Override


public String execute()
throws Exception






{




boolean result = loginService.validate(username, password);


if(result ==
true)






{


return SUCCESS;


}


else






{


return INPUT;


}


}


}



四、配置struts.xml与applicationContext.xml


1.配置struts.properties,为了解决中文问题,具体用法参照struts2的用法如下:里面加上struts.i18n.encoding = gb2312,当然也可以直接加到struts.xml里面写法为<constant name="struts.i18n.encoding" value="gbk"></constant>
2.配置struts.xml


<?xml version="1.0" encoding="GB2312" ?>


<!DOCTYPE struts PUBLIC


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


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


<struts>


<package name="struts2"extends="struts-default">


<action name="login"class="LoginAction">


<exception-mapping result="usernameInvalid" exception="org.topCSA.s2s.exception.UsernameException" />


<exception-mapping result="passwordInvalid" exception="org.topCSA.s2s.exception.PasswordException" />


<result name="success">/index.jsp</result>


<result name="input">/login.jsp</result>


<result name="usernameInvalid">/usernameInvalid.jsp</result>


<result name="passwordInvalid">/passwordInvalid.jsp</result>


</action>


</package>


</struts>

3.配置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-2.0.xsd">



<bean name="loginService"class="org.topCSA.s2s.service.LoginService" />




<bean name="LoginAction"class="org.topCSA.s2s.action.LoginAction">


<property name="loginService">


<ref bean="loginService"/>


</property>


</bean>




</beans>

***********************************************************************************************************************************************************************************

一、
Struts 2框架整合Spring步骤

1、
复制文件。复制struts2-spring-plugin-x-x-x.jar和spring.jar到WEB-INF/lib目录下。其中的x对应了Spring的版本号。还需要复制commons-logging.jar文件到WEB-INF/lib目录下。
2、
配置struts.objectFactory属性值。在struts.properties中设置struts.objectFactory属性值:struts.objectFactory = spring
或者在XML文件中进行常量配置:
<struts>
<constant name="struts.objectFactory" value="spring" />
...
</struts>
3、
配置Spring监听器。在web.xml文件中增加如下内容:
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
4、
Spring配置文件。默认情况下,Spring配置文件为applicationContext.xml,该文件需要保存在Web应用的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 default-autowire="byName">
<bean id="personManager" class="com.acme.PersonManager"/>
</beans>
开发者实际上可以使用多个Spring配置文件,在web.xml中进行下列设置,从而使Spring的ApplicationContext通过匹配所给定模式的文件来初始化对象:
<!-- 用来定位Spring XML文件的上下文配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml
</param-value>
</context-param>
5、
修改Struts配置文件。Struts 2框架整合Spring框架,需要在Struts配置文件中有所改变,下面是一个示例:
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<include file="struts-default.xml"/>
<package name="default" extends="struts-default">
<action name="foo" class="com.acme.Foo">
<result>foo.ftl</result>
</action>
</package>
<package name="secure" namespace="/secure" extends="default">
<action name="bar" class="bar">
<result>bar.ftl</result>
</action>
</package>
</struts>
该配置文件中定义了两个Action配置:foo是一个标准的Struts 2框架Action配置,指定了Action实现类为com.acme.Foo;bar对应的class并不存在,那么框架将在Spring配置文件中查找id属性为“bar”的定义,该配置文件如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC
"-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans default-autowire="byName">
<bean id="bar" class="com.my.BarClass" singleton="false"/>
...
</beans>

二、整合原理
Struts2与Spring的集成要用到Spring插件包struts2-spring-plugin-x-x-x.jar,这个包是同Struts2一起发布的。Spring插件是通过覆盖(override)Struts2的ObjectFactory来增强核心框架对象的创建。当创建一个对象的时候,它会用Struts2配置文件中的class属性去和Spring配置文件中的id属性进行关联,如果能找到,则由Spring创建,否则由Struts
2框架自身创建
然后由Spring来装配。Spring插件具体有如下几个作用:

允许Spring创建Action、Interceptror和Result。

由Struts创建的对象能够被Spring装配。

如果没有使用Spring ObjectFactory,提供了2个拦截器来自动装配action。

开发者不必在Spring中去注册action,尽管可以这么去做,通常Struts框架会自动地从action mapping中创建action对象

struts2-spring-plugin-x-x-x.jar插件中有一个struts-plugin.xml文件,该文件内容如下所示:
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<bean type="com.opensymphony.xwork2.ObjectFactory" name="spring" class="org.apache.struts2.spring.StrutsSpringObjectFactory" />

<!-- 设置Spring对象工厂为自动 -->
<constant name="struts.objectFactory" value="spring" />
<package name="spring-default">
<interceptors>
<interceptor name="autowiring" class="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor"/>
<interceptor name="sessionAutowiring" class="org.apache.struts2.spring.interceptor.SessionContextAutowiringInterceptor"/>
</interceptors>
</package>
</struts>

其中设置了Struts 2框架常量struts.objectFactory的值为spring,实际上,spring是org.apache.struts2.spring.StrutsSpringObjectFactory类的缩写,默认情况下所有由Struts 2框架创建的对象都是由ObjectFactory实例化的,ObjectFactory提供了与其他IoC容器如Spring、Pico等集成的方法。覆盖这个ObjectFactory的类必须继承ObjectFactory类或者它的任何子类,并且要带有一个不带参数的构造方法。在这里用org.apache.struts2.spring.StrutsSpring
ObjectFactory代替了默认的ObjectFactory。
如果Action不是使用Spring ObjectFactory创建的话,插件提供了两个拦截器来自动装配Action,默认情况下框架使用的自动装配策略是name,也就是说框架会去Spring中寻找与Action属性名字相同的bean,可选的装配策略还有:type、auto、constructor,开发者可以通过常量struts.objectFactory.spring.autoWire来进行设置。

Struts 2框架整合Spring后,处理用户请求的Action并不是Struts框架创建的,而是由Spring插件创建的。创建实例时,不是利用配置Action时指定的class属性值,根据bean的配置id属性,从Spring容器中获得相应的实例。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: