Spring框架学习

文章40 |   阅读 14381 |   点赞0

来源:https://blog.csdn.net/yerenyuan_pku/category_6457009.html

Spring4.2.5+Hibernate4.3.11+Struts1.3.8集成方案一

x33g5p2x  于2021-12-18 转载在 其他  
字(4.0k)|赞(0)|评价(0)|浏览(359)

前面我们已经集成了Spring4.2.5+Hibernate4.3.11这两个框架,现在是时候集成web层框架——Struts1.3.8了。本文是是建立在Spring4.2.5+Hibernate4.3.11组合开发基础之上的。
我们首先向SSH项目中导入Struts1.3.8框架所需的jar文件,如图所示:

注意:Hibernate4.3.11中已经存在一个antlr-2.7.7.jar,所以把Struts中的antlr-2.7.2.jar删除,避免jar冲突
除此之外,千万不要忘了向SSH项目中导入如下jar文件:

这样,总共需要向SSH项目中导入的jar文件有46个:

接下来,我们在web层中要做两项工作:
第一项工作,原来Spring容器的实例化由我们手工编码实例化,现在不再需要我们手工编码去实例化Spring容器了,可以使用Spring给我们提供的监听器——org.springframework.web.context.ContextLoaderListener来实例化Spring容器。这个类实例化Spring容器后,它会把Spring容器实例放到什么范围里面去呢?答案是它将把Spring容器实例放在servletContext对象里面(即application范围内),往这个对象里面放进去的实例,随着web应用启动时它就被放进去了,一直到web应用关闭时它才被销毁。若以后我们要得到Spring容器的实例,可以从application范围内根据名称WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE来得到Spring容器实例。
第一项工作,说白了就是在在web容器中实例化Spring容器,所以需要在WEB-INF/web.xml文件中添加如下配置:

<!-- 指定Spring的配置文件,默认从web根目录寻找配置文件,我们可以通过Spring提供的classpath:前缀指定从类路径下寻找 -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:beans.xml</param-value>
</context-param>
<!-- 对Spring容器进行实例化 -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

第二项工作,在web容器中配置Struts,即需要在WEB-INF/web.xml文件中添加如下配置:

<servlet>
    <servlet-name>struts</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
        <param-name>config</param-name>
        <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>struts</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

接下来,我们在src目录下新建一个cn.itcast.web.action包,并在该包下新建一个Action——PersonAction.java,用于处理客户端请求。如果action没有交给Spring管理,那么我们可通过下面语句获取Spring容器实例:

WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(
                this.getServlet().getServletContext());

这样,PersonAction的代码就应该为:

public class PersonAction extends Action {

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        // 如何得到Spring容器实例
        WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(
                this.getServlet().getServletContext());
        PersonService personService = (PersonService) ctx.getBean("personService");
        request.setAttribute("persons", personService.getPersons());
        return mapping.findForward("list");
    }

}

紧接着,我们在WEB-INF目录下新建Struts的配置文件——struts-config.xml,其内容就应为:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">

<struts-config>
    <action-mappings>         
        <action path="/person/list" type="cn.itcast.web.action.PersonAction" validate="false">
            <forward name="list" path="/WEB-INF/page/personlist.jsp"></forward>
        </action>
    </action-mappings>
</struts-config>

我们还要在WEB-INF目录下新建一个名为page的目录,并在page目录下新建一个jsp页面——personlist.jsp。

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>人员列表</title>
</head>
<body>
    <c:forEach items="${persons }" var="person">
        ID=${person.id }--------name=${person.name }<br/>
    </c:forEach>
</body>
</html>

查看数据库person表,可以看到person表有如下记录:

这时,我们通过浏览器访问url地址:http://localhost:8080/SSH/person/list.do,可以看到如下结果:

如须查看源码,可点击**Spring4.2.5+Hibernate4.3.11+Struts1.3.8集成方案一**进行下载。

相关文章