在struts2的基础上使用Spring,
1、首先需要导入Spring相关的包,在pom.xml中加入以下内容:
org.springframework spring-core 3.1.2.RELEASE org.springframework spring-context 3.1.2.RELEASE org.springframework spring-jdbc 3.1.2.RELEASE org.springframework spring-beans 3.1.2.RELEASE org.springframework spring-web 3.1.2.RELEASE org.springframework spring-expression 3.1.2.RELEASE org.springframework spring-orm 3.1.2.RELEASE
2、使用spring
(1)使用spring:首先在web.xml中配置spring相关项,第一项设置spring配置文件路径,后面配置spring的监听,当采用spring创建实例方式时,将按照配置文件中类的依赖关系进行依赖注入。
contextConfigLocation classpath:contextConfig.xml org.springframework.web.context.ContextLoaderListener
(2)配置类的依赖关系:创建contextConfig.xml文件,将文件放置在上面指定的路径下,文件内容如下(loginAction依赖于loginService,其中loginService用来做逻辑判断是否登录成功):
(3)创建ILoginService接口和创建LoginServiceImpl类:类实现接口,在LoginAction中包含ILoginService类型的属性,通过set方法设置属性所指向的具体的对象(依赖注入),这样可以通过修改配置文件修改具体实现:
LoginAction类:
package com.mz.action; import com.mz.service.ILoginService; import com.opensymphony.xwork2.ActionSupport; import org.apache.struts2.ServletActionContext; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.UnsupportedEncodingException; /** * Created by hadoop on 15-9-7. */ public class LoginAction extends ActionSupport { private static final long serialVersionUID = 1L; private ILoginService loginService; public void setLoginService(ILoginService loginService) { this.loginService = loginService; } public ILoginService getLoginService(){ return this.loginService; } public String execute(){ return SUCCESS; } public String login() throws IOException { try { HttpServletRequest request = ServletActionContext.getRequest(); HttpServletResponse response = ServletActionContext.getResponse(); request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=utf-8"); String username = request.getParameter("username"); String password = request.getParameter("password"); System.out.println(username + ":" + password); boolean login = loginService.userLogin(username, password); if(login){ response.getWriter().write("login success!"); response.getWriter().flush(); return SUCCESS; } else { response.getWriter().write("login failed!"); response.getWriter().flush(); return "login"; } } catch (UnsupportedEncodingException e) { e.printStackTrace(); return "login"; } } }
ILoginService接口:
package com.mz.service; /** * Created by hadoop on 15-9-8. */ public interface ILoginService { boolean userLogin(String username, String password); }
LoginServiceImple类:
package com.mz.service; import com.mz.dao.IUserDao; /** * Created by hadoop on 15-9-8. */ public class LoginServiceImpl implements ILoginService { public boolean userLogin(String username, String password) { if("admin".equals(username)&&"123456".equals(password)){ return true; }else{ return false; } } }
(这个时候rebuild工程,发现网页访问时报错,nullPointException,因为在前面的配置中action类是通过struts2生成的实例,它不会使用依赖注入创建loginService)
(4)让struts2按照spring的规则来生成对象:修改strut.xml配置文件,将class换成Spring配置文件中的id
/index.jsp /login.jsp
(5)最后还需要使用struts2与Spring结合的包,所以在pom.xml中导入包,注意版本对应,否在可能出错:
org.apache.struts struts2-spring-plugin 2.3.4.1
然后告诉struts2需要使用Spring,在Struts.xml中加入
这个时候struts2碰到loginAction时会去采用Spring的规则生成对象,rebuild工程,测试一般可以通过。
相关内容
(1)Web开发:Struts2 Spring Hibernate整合(一)——Struts2的使用
(2)Web开发:Struts2 Spring Hibernate整合(三)上——Hibernate的使用
(3)Web开发:Struts2 Spring Hibernate整合(三)下——Hibernate的使用