整合Spring,换句话说,也就是让spring的IOC功能为我们的struts action注入逻辑组件
首先需要加载struts2-spring-plugin-2.0.6.jar这个包,这个是关键,他可以帮我们把struts2和spring仅仅整合在一起
首先是web.xml
<?
xmlversion="1.0"encoding="utf-8"
?>
<
web-app
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/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
>

<
context-param
>
<
param-name
>
contextConfigLocation
</
param-name
>
<
param-value
>
/WEB-INF/bean.xml
</
param-value
>
</
context-param
>

<
listener
>
<
listener-class
>
org.springframework.web.context.ContextLoaderListener
</
listener-class
>
</
listener
>
<
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
>

<
filter
>
<
filter-name
>
aaa
</
filter-name
>
<
filter-class
>
org.apache.struts2.dispatcher.ActionContextCleanUp
</
filter-class
>
</
filter
>


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


</
web-app
>
其次是struts2.xml
<?
xmlversion="1.0"encoding="GBK"
?>
<!
DOCTYPEstrutsPUBLIC
"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"
>
<
struts
>

<
package
name
="lee"
extends
="struts-default"
>
<
action
name
="login"
class
="loginAction"
>
<
result
name
="error"
>
/error.jsp
</
result
>
<
result
name
="success"
>
/welcome.jsp
</
result
>
</
action
>
</
package
>

</
struts
>
最关键的是bean.xml,完成为struts2 action的注入
<?
xmlversion="1.0"encoding="GBK"
?>
<!--
定义Spring配置文件的根元素,并指定Schema信息
-->
<
beans
xmlns
="http://www.springframework.org/schema/beans"
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation
="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"
>

<
bean
id
="myService"
class
="lee.MyServiceImpl"
/>
<
bean
id
="loginAction"
class
="lee.LoginAction"
scope
="prototype"
>
<
property
name
="ms"
ref
="myService"
/>
</
bean
>
</
beans
>
LoginAction
package
lee;
import
com.opensymphony.xwork2.Action;

public
class
LoginAction
implements
Action
...
{
privateStringusername;
privateStringpassword;
privateStringtip;
privateMyServicems;
publicStringgetUsername()...{
returnusername;
}

publicvoidsetUsername(Stringusername)...{
this.username=username;
}

publicStringgetPassword()...{
returnpassword;
}

publicvoidsetPassword(Stringpassword)...{
this.password=password;
}

publicStringgetTip()...{
returntip;
}

publicvoidsetTip(Stringtip)...{
this.tip=tip;
}

publicStringexecute()throwsException...{
if(ms.valid(this.getUsername(),this.getPassword()))...{
returnSUCCESS;
}
else...{
returnERROR;
}
}

publicMyServicegetMs()...{
returnms;
}

publicvoidsetMs(MyServicems)...{
this.ms=ms;
}
}
MyService及实现
package
lee;

public
interface
MyService
...
{
booleanvalid(Stringusername,Stringpass);
}


package
lee;
public
class
MyServiceImpl
implements
MyService
...
{
publicbooleanvalid(Stringusername,Stringpass)
...{System.out.println(username);
if(username.equals("高")&&pass.equals("1234"))
...{
returntrue;
}
returnfalse;
}
}
struts.properties
struts.i18n.encoding=gb2312login.jsp
<%@ page contentType="text/html; charset=gb2312"%>
<html>
<head>
<title>登录页面</title>
</head>
<body>
<form action="login.action" method="post">
<table align="center">
<caption><h3>用户登录</h3></caption>
<tr>
<td>用户名:<input type="text" name="username"/></td>
</tr>
<tr>
<td>密 码:<input type="text" name="password"/></td>
</tr>
<tr align="center">
<td colspan="2"><input type="submit" value="登录"/><input type="reset" value="重填" /></td>
</tr>
</table>
</form>
</body>
</html>
welcome.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>成功页面</title>
</head>
<body>
您已经登录!
<s:property value="username"/>
<s:property value="tip"/>
</body>
</html>
error.jsp
<%@ page language="java" contentType="text/html; charset=GBK"%>
<html>
<head>
<title>错误页面</title>
</head>
<body>
您不能登录!
</body>
</html>
版权声明:本文为博主原创文章,未经博主允许不得转载。
本文介绍如何通过struts2-spring-plugin将Struts2与Spring框架进行整合,实现Struts2 Action的依赖注入。文章详细展示了配置过程,包括web.xml、struts2.xml和bean.xml等文件的具体设置。
979

被折叠的 条评论
为什么被折叠?



