1、struts221 + spring 305 配置与测试例子一、配置Struts1、本配置与测试例子,使用MyEclipse 6.0.1版本软件,创建web工程。2、把struts 2.2.1的相关jar添加到工程中,如图所示。1)选择Add External Archives2)选择jar包Struts 2.2.1核心所包含的文件有8个jar文件,如下图所示。3、添加配置文件配置文件包括struts.xml和web.xml,两者路径分别为:1)struts.xml:src根目录下。在没有配置之前文件内容如下:2)web.xml:WEB-INF目录下。在没有配置之前,文件内容如下:web-ap
2、p version=2.5 xmlns= xmlns:xsi=http:/www.w3.org/2001/XMLSchema-instance xsi:schemaLocation= index.jsp 该web.xml是创建web工程的时候产生的。在struts2.2中,该文件改为:web-app version=2.5 xmlns= xmlns:xsi=http:/www.w3.org/2001/XMLSchema-instance xsi:schemaLocation= struts2 org.apache.struts2.dispatcher.ng.filter.StrutsPrepa
3、reAndExecuteFilter struts2 /* index.jsp 4、添加处理业务流程1)添加业务页面,如添加信息页面 Login 这里的页面,使用了struts的标签。主要有username与password两个字段,以及form的名称为login。2)添加Action类添加的Action,继承了ActionSupport,也可以直接实现Action接口。这里的两个字段与页面使用的标签相对应。这里重载了execute函数,该函数作为默认的Action的处理函数。在实现Action接口,必须进行处理。package com.gsww.kingreturns.struts2.exc
4、ise;import com.opensymphony.xwork2.ActionSupport;public class LoginAction extends ActionSupport /该类继承了ActionSupport类。这样就可以直接使用SUCCESS, LOGIN等变量和重写execute等方法 private static final long serialVersionUID = 1L; private String username; private String password; public String getUsername() return username;
5、 public void setUsername(String username) this.username = username; public String getPassword() return password; public void setPassword(String password) this.password = password; Override public String execute() throws Exception if(haha.equals(username) & hehe.equals(password)/如果登录的用户名=haha并且密码=heh
6、e,就返回SUCCESS;否则,返回LOGIN return SUCCESS; return LOGIN; 3)配置struts.xml文件在xml文件中,添加package内容(如果已经存在,不用再添加)。接着添加action,这里action的name与页面上的form名称一致;class就是Action类;method为Action执行的函数,一般为execute方法。在execute中,使用到SUCCESS以及LOGIN常量,这些在Action基类中定义。这里需要说明下一步分别执行的页面。 /welcome.jsp /login.jsp 4)定义welcome.jsp和login.js
7、p页面(存放在WebRoot目录下)下面为welcome.jsp页面,直接使用了标签简化。 My JSP welcome.jsp starting page 欢迎$username ! 二、配置Spring1、添加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,具体
8、文件内容如下: org.springframework.web.context.ContextLoaderListener struts2 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter struts2 /* 注意:这里的其实是指向applicationContext.xml文件的,当之有一个该文件时候,把它放在WEB-INF下即可。如果存在多个文件的时候,需要说明文件路径。在监听器上面添加配置: contextConfigLocation /WEB-INF/classes/applicationCon
9、text.xml,/WEB-INF/daoContent.xml3、使用Spring方法一:1)添加Spring层处理函数添加接口package com.gsww.kingreturns.struts2.service;public interface TestService boolean ValidPassword(String name, String pwd);接口实现package com.gsww.kingreturns.struts2.service.impl;import com.gsww.kingreturns.struts2.service.TestService;publ
10、ic class TestServiceImp implements TestService public boolean ValidPassword(String name, String pwd) / TODO Auto-generated method stub if(name.equals(wendehai) & pwd.equals(123) return true; return false; 2)Action中添加逻辑处理添加字段ts,并且实现其setter函数。并在execute中,添加ts的调用。package com.gsww.kingreturns.struts2.exc
11、ise;import com.gsww.kingreturns.struts2.service.TestService;import com.opensymphony.xwork2.ActionSupport;public class LoginAction extends ActionSupport / 该类继承了ActionSupport类。这样就可以直接使用SUCCESS, / LOGIN等变量和重写execute等方法 private static final long serialVersionUID = 1L; private String username; private St
12、ring password; private TestService ts; public void setTs(TestService ts) this.ts = ts; 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 =
13、 password; Override public String execute() throws Exception if(ts.ValidPassword(this.username, this.password) return SUCCESS; else return LOGIN; 3)设置配置文件applicationContext.xml中添加对于Spring处理类的配置 这里说明了Action的名字loginAction,需要把struts.xml中的class名字改loginAction,而不是实际的类路径。 /welcome.jsp /login.jsp /info.jsp /error.jsp 方法二:该方法与方法一的区别在于配置文件设置不同。applicationContext.xml中,Spring处理类,配置为如下,这里的id值需要与Action中的变量名字一致,即为ts。 Struts.xml中,Action使用实际路径,其它不变
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1