欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Web开发:Struts2 Spring Hibernate整合(二)——Spring的使用

程序员文章站 2022-06-13 21:51:17
...

    在struts2的基础上使用Spring,

1、首先需要导入Spring相关的包,在pom.xml中加入以下内容:

<!-- spring依赖包 -->
        <!-- spring3 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>3.1.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>3.1.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>3.1.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>3.1.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>3.1.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>3.1.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>3.1.2.RELEASE</version>
        </dependency>

 2、使用spring

(1)使用spring:首先在web.xml中配置spring相关项,第一项设置spring配置文件路径,后面配置spring的监听,当采用spring创建实例方式时,将按照配置文件中类的依赖关系进行依赖注入。

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:contextConfig.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

 (2)配置类的依赖关系:创建contextConfig.xml文件,将文件放置在上面指定的路径下,文件内容如下(loginAction依赖于loginService,其中loginService用来做逻辑判断是否登录成功):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <bean id="loginService" class="com.mz.service.LoginServiceImpl">
    </bean>

    <bean id="loginAction" class="com.mz.action.LoginAction">
        <property name="loginService" >
            <ref local="loginService"/>
        </property>
    </bean>
</beans>

 (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

<package name="front" namespace="/" extends="struts-default">

        <action name="login" class="loginAction" method="login">
            <result name="success">/index.jsp</result>
            <result name="login">/login.jsp</result>
        </action>

    </package>

(5)最后还需要使用struts2与Spring结合的包,所以在pom.xml中导入包,注意版本对应,否在可能出错:

<!-- spring3和struts2结合包 -->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-spring-plugin</artifactId>
            <version>2.3.4.1</version>
        </dependency>

然后告诉struts2需要使用Spring,在Struts.xml中加入

<constant name="struts.objectFactory" value="spring" />

这个时候struts2碰到loginAction时会去采用Spring的规则生成对象,rebuild工程,测试一般可以通过。

相关内容

(1)Web开发:Struts2 Spring Hibernate整合(一)——Struts2的使用

(2)Web开发:Struts2 Spring Hibernate整合(三)上——Hibernate的使用

(3)Web开发:Struts2 Spring Hibernate整合(三)下——Hibernate的使用