struts221 + spring 305 配置与测试例子.docx
《struts221 + spring 305 配置与测试例子.docx》由会员分享,可在线阅读,更多相关《struts221 + spring 305 配置与测试例子.docx(14页珍藏版)》请在冰豆网上搜索。
![struts221 + spring 305 配置与测试例子.docx](https://file1.bdocx.com/fileroot1/2023-5/15/77d49c71-0781-4ca8-8e48-a04667536332/77d49c71-0781-4ca8-8e48-a046675363321.gif)
struts221+spring305配置与测试例子
一、配置Struts
1、本配置与测试例子,使用MyEclipse6.0.1版本软件,创建web工程。
2、把struts2.2.1的相关jar添加到工程中,如图所示。
1)选择AddExternalArchives…
2)选择jar包
Struts2.2.1核心所包含的文件有8个jar文件,如下图所示。
3、添加配置文件
配置文件包括struts.xml和web.xml,两者路径分别为:
1)struts.xml:
src根目录下。
在没有配置之前文件内容如下:
xmlversion="1.0"encoding="UTF-8"?
>
DOCTYPEstrutsPUBLIC
"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"
"http:
//struts.apache.org/dtds/struts-2.0.dtd">
2)web.xml:
WEB-INF目录下。
在没有配置之前,文件内容如下:
xmlversion="1.0"encoding="UTF-8"?
>
xmlns="
xmlns:
xsi="http:
//www.w3.org/2001/XMLSchema-instance"
xsi:
schemaLocation="
index.jsp
该web.xml是创建web工程的时候产生的。
在struts2.2中,该文件改为:
xmlversion="1.0"encoding="UTF-8"?
>
xmlns="
xmlns:
xsi="http:
//www.w3.org/2001/XMLSchema-instance"
xsi:
schemaLocation="
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
/*
index.jsp
4、添加处理业务流程
1)添加业务页面,如添加信息页面
<%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%>
<%@tagliburi="/struts-tags"prefix="s"%>
DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
Login
formaction="login"method="post"namespace="/">
labelvalue="系统登陆">
label>
textfieldname="username"label="账号"/>
passwordname="password"label="密码"/>
submitvalue="登录"/>
form>
这里的页面,使用了struts的标签。
主要有username与password两个字段,以及form的名称为login。
2)添加Action类
添加的Action,继承了ActionSupport,也可以直接实现Action接口。
这里的两个字段与页面使用的标签相对应。
这里重载了execute函数,该函数作为默认的Action的处理函数。
在实现Action接口,必须进行处理。
packagecom.gsww.kingreturns.struts2.excise;
importcom.opensymphony.xwork2.ActionSupport;
publicclassLoginActionextendsActionSupport{//该类继承了ActionSupport类。
这样就可以直接使用SUCCESS,LOGIN等变量和重写execute等方法
privatestaticfinallongserialVersionUID=1L;
privateStringusername;
privateStringpassword;
publicStringgetUsername(){
returnusername;
}
publicvoidsetUsername(Stringusername){
this.username=username;
}
publicStringgetPassword(){
returnpassword;
}
publicvoidsetPassword(Stringpassword){
this.password=password;
}
@Override
publicStringexecute()throwsException{
if("haha".equals(username)&&"hehe".equals(password))//如果登录的用户名=haha并且密码=hehe,就返回SUCCESS;否则,返回LOGIN
returnSUCCESS;
returnLOGIN;
}
}
3)配置struts.xml文件
在xml文件中,添加package内容(如果已经存在,不用再添加)。
接着添加action,这里action的name与页面上的form名称一致;class就是Action类;method为Action执行的函数,一般为execute方法。
在execute中,使用到SUCCESS以及LOGIN常量,这些在Action基类中定义。
这里需要说明下一步分别执行的页面。
xmlversion="1.0"encoding="UTF-8"?
>
DOCTYPEstrutsPUBLIC
"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"
"http:
//struts.apache.org/dtds/struts-2.0.dtd">
/welcome.jsp
/login.jsp
4)定义welcome.jsp和login.jsp页面(存放在WebRoot目录下)
下面为welcome.jsp页面,直接使用了标签简化。
<%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%>
DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
MyJSP'welcome.jsp'startingpage
欢迎${username}!
二、配置Spring
1、添加jar包
本次使用的Spring版本为3.0.5,因此,需要把spring-framework-3.0.5.RELEASE.zip中的jar全部添加,共20个jar。
另外,需要Struts和Spring整合的jar,这里使用struts2-spring-plugin-2.2.1.jar。
2、修改web.xml文件,添加Spring的监听器ContextLoaderListener,具体文件内容如下:
xmlversion="1.0"encoding="GBK"?
>
xmlns:
xsi="http:
//www.w3.org/2001/XMLSchema-instance"
xsi:
schemaLocation="
version="3.0">
--使用ContextLoaderListener初始化Spring容器-->
org.springframework.web.context.ContextLoaderListener
--定义Struts2的FilterDispathcer的Filter-->
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
--FilterDispatcher用来初始化Struts2并且处理所有的WEB请求。
-->
struts2
/*
注意:
这里的其实是指向applicationContext.xml文件的,当之有一个该文件时候,把它放在WEB-INF下即可。
如果存在多个文件的时候,需要说明文件路径。
在监听器上面添加配置:
contextConfigLocation
/WEB-INF/classes/applicationContext.xml,/WEB-INF/daoContent.xml
3、使用Spring
方法一:
1)添加Spring层处理函数
添加接口
packagecom.gsww.kingreturns.struts2.service;
publicinterfaceTestService{
booleanValidPassword(Stringname,Stringpwd);
}
接口实现
packagecom.gsww.kingreturns.struts2.service.impl;
importcom.gsww.kingreturns.struts2.service.TestService;
publicclassTestServiceImpimplementsTestService{
publicbooleanValidPassword(Stringname,Stringpwd){
//TODOAuto-generatedmethodstub
if(name.equals("wendehai")&&pwd.equals("123")){
returntrue;
}
returnfalse;
}
}
2)Action中添加逻辑处理
添加字段ts,并且实现其setter函数。
并在execute中,添加ts的调用。
packagecom.gsww.kingreturns.struts2.excise;
importcom.gsww.kingreturns.struts2.service.TestService;
importcom.opensymphony.xwork2.ActionSupport;
publicclassLoginActionextendsActionSupport{//该类继承了ActionSupport类。
这样就可以直接使用SUCCESS,
//LOGIN等变量和重写execute等方法
privatestaticfinallongserialVersionUID=1L;
privateStringusername;
privateStringpassword;
privateTestServicets;
publicvoidsetTs(TestServicets){
this.ts=ts;
}
publicStringgetUsername(){
returnusername;
}
publicvoidsetUsername(Stringusername){
this.username=username;
}
publicStringgetPassword(){
returnpassword;
}
publicvoidsetPassword(Stringpassword){
this.password=password;
}
@Override
publicStringexecute()throwsException{
if(ts.ValidPassword(this.username,this.password)){
returnSUCCESS;
}else{
returnLOGIN;
}
}
}
3)设置配置文件
applicationContext.xml中添加对于Spring处理类的配置
xmlversion="1.0"encoding="GBK"?
>
--Spring配置文件的根元素,使用spring-beans-3.0.xsd语义约束-->
xsi="http:
//www.w3.org/2001/XMLSchema-instance"
xmlns="http:
//www.springframework.org/schema/beans"
xsi:
schemaLocation="http:
//www.springframework.org/schema/beans
http:
//www.springframework.org/schema/beans/spring-beans-3.0.xsd">
--定义一个业务逻辑组件,实现类为MyServiceImp-->
class="com.gsww.kingreturns.struts2.service.impl.TestServiceImp"/>
--让Spring管理的Action实例-->
scope="prototype">
--依赖注入业务逻辑组件-->
这里说明了Action的名字loginAction,需要把struts.xml中的class名字改loginAction,而不是实际的类路径。
xmlversion="1.0"encoding="UTF-8"?
>
DOCTYPEstrutsPUBLIC
"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"
"http:
//struts.apache.org/dtds/struts-2.0.dtd">
/welcome.jsp
/login.jsp
/info.jsp
/error.jsp
方法二:
该方法与方法一的区别在于配置文件设置不同。
applicationContext.xml中,Spring处理类,配置为如下,这里的id值需要与Action中的变量名字一致,即为ts。
xmlversion="1.0"encoding="GBK"?
>
--Spring配置文件的根元素,使用spring-beans-3.0.xsd语义约束-->
xsi="http:
//www.w3.org/2001/XMLSchema-instance"
xmlns="http:
//www.springframework.org/schema/beans"
xsi:
schemaLocation="http:
//www.springframework.org/schema/beans
http:
//www.springframework.org/schema/beans/spring-beans-3.0.xsd">
--定义一个业务逻辑组件,实现类为MyServiceImp-->
class="com.gsww.kingreturns.struts2.service.impl.TestServiceImp"/>
Struts.xml中,Action使用实际路径,其它不变