`
lushuaiyin
  • 浏览: 674956 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

struts2整合spring应用实例

 
阅读更多

struts2整合spring应用实例

我们知道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,如下:

<?xmlversion="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,如下:

<%@pagelanguage="java"pageEncoding="GB2312"%>
<%@taglibprefix="s"uri="/struts-tags"%>

<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//CN">
<html>
<head>
<title>login2</title>
</head>

<body>
<s:formname="login"action="login"method="post">
<s:textfieldname="username"label="帐号"></s:textfield>
<s:passwordname="password"label="密码"></s:password>
<s:submit></s:submit>
</s:form>
</body>
</html>

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

<%@pagelanguage="java"pageEncoding="GB2312"%>

<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//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表达示得到异常信息)

<%@pagelanguage="java"contentType="text/html;charset=GB18030"
pageEncoding="GB18030"%>

<!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=GB18030">
<title>Inserttitlehere</title>
</head>
<body>
用户名非法:<fontcolor="red">${exception.message}</font>
</body>
</html>

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

<%@pagelanguage="java"contentType="text/html;charset=GB18030"
pageEncoding="GB18030"%>
<%@taglibprefix="s"uri="/struts-tags"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=GB18030">
<title>Inserttitlehere</title>
</head>
<body>
密码非法:<FONTcolor="red"><s:propertyvalue="exception.message"/></FONT>
</body>
</html>


三、建立对应的action

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

packageorg.topCSA.s2s.service;

importorg.topCSA.s2s.exception.PasswordException;
importorg.topCSA.s2s.exception.UsernameException;

publicclassLoginService
{
/*
*我们这只是一个小的例子,不与数据库打交到
*若有数据库操作,那么在这个LoginService就是操作具体Dao类实现登录的相关操作
*/
publicbooleanvalidate(Stringusername,Stringpassword)throwsException
{
booleanv=false;
if(!"xcp".equals(username))//如果用户名不等于xcp,就抛出一个异常
{
thrownewUsernameException("用户名不正确");
}
elseif(!"123".equals(password))))
//如果密码不等于123,就抛出一个异常

{
thrownewPasswordException("密码不正确");
}
else
{
v=true;
}
returnv;
}
}


2.接收用户请求的LoginAction类

packageorg.topCSA.s2s.action;

importorg.topCSA.s2s.service.LoginService;

importcom.opensymphony.xwork2.ActionSupport;

publicclassLoginActionextendsActionSupport
{

/**
*
*/
privatestaticfinallongserialVersionUID=1L;

privateStringusername;
privateStringpassword;

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

publicLoginServicegetLoginService()
{
returnloginService;
}
publicvoidsetLoginService(LoginServiceloginService)
{
this.loginService=loginService;
}
publicStringgetUsername()
{
returnusername;
}
publicvoidsetUsername(Stringusername)
{
this.username=username;
}
publicStringgetPassword()
{
returnpassword;
}
publicvoidsetPassword(Stringpassword)
{
this.password=password;
}


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

@Override
publicStringexecute()throwsException
{

booleanresult=loginService.validate(username,password);
if(result==true)
{
returnSUCCESS;
}
else
{
returnINPUT;
}
}
}


四、配置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

<?xmlversion="1.0"encoding="GB2312"?>
<!DOCTYPEstrutsPUBLIC
"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<packagename="struts2"extends="struts-default">
<actionname="login"class="LoginAction"><!--注意,这里的class写的是LoginAction
它不是实际对应的类,而是一个springbean,
具体对应的值,即哪个类在spring的xml文件定义了,
这样把class的值交给spring管理类,这是两者
整合的一个具体方面,我们也可以不这样做,
按照原来把class属性对应的包名.类名写上也可以-->

<exception-mappingresult="usernameInvalid"exception="org.topCSA.s2s.exception.UsernameException"/>
<exception-mappingresult="passwordInvalid"exception="org.topCSA.s2s.exception.PasswordException"/>
<resultname="success">/index.jsp</result>
<resultname="input">/login.jsp</result>
<resultname="usernameInvalid">/usernameInvalid.jsp</result>
<resultname="passwordInvalid">/passwordInvalid.jsp</result>
</action>
</package>
</struts>


3.配置applicationContext.xml

<?xmlversion="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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

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

<beanname="LoginAction"class="org.topCSA.s2s.action.LoginAction"><!-- 这里我们把struts的xml文件中
定义的某个action中的class属性
具体化了,这就是struts与spring
整合的具体表现。以前只用struts
时,action中的class直接写出哪个
包中的哪个类,现在定义到spring
中了,有人觉得麻烦了,有人觉得
更加清除了,也好管理。这只是两
者整合的一个表现,不一定非要这
样做,spring管理对象,它可以管
struts的对象,也可以不管。-->

<propertyname="loginService">
<refbean="loginService"/>
</property>
</bean>

</beans>


五、测试(全部成功)













分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics