85-002-6 SSH开发环境的整合

来源:互联网 发布:绩效软件 编辑:程序博客网 时间:2024/06/08 03:05

图文版:http://note.youdao.com/yws/public/redirect/share?id=abc20790e48da5130e227d9390e4df92&type=false
资源文件下载 https://yunpan.cn/OcRaTMHR3NWIIf  访问密码 4b2c


23. 整合spring与struts框架(通过spring来管理action,这样action的依赖就能通过spring注入的方式提供);

24. 首先做的是添加jar包:spring-3.2-web\spring-web-3.2.0.RELEASE.jar、struts2-spring-plugin-2.1.6.jar(上一步排查异常时已经把此jar包放到了lib目录下),这样就可以把action和他的依赖交给spring管理。

25. 然后配置一个单独的xml配置文件可以通过拷贝applicationContext-service.xml文件修改得来

    applicationContext-action.xml

<bean id="categoryAction" class="cn.it.shop.action.CategoryAction" scope="prototype">
  <!--ref="categoryService"与applicationContext-service.xml的bean节点一一对应的-->
  <property name="categoryService" ref="categoryService"></property>
 </bean> 

26. 因为已经与spring整合了,所以在struts.xml中class对应的是spring中配置的action的id值,而不再是class的路径,

    struts.xml

<action name="category_*" class="categoryAction" method="{1}">
            <result name="index">/index.jsp</result>
</action>

27. 然后在web.xml文件中配置监听器,这样在tomcat启动的时候可以加载spring的配置文件(这一步同样在上次排除异常时已经搞定了)

    web.xml

<!--在tomcat启动的时候,监听器永远都是优先于filter启动,所以在xml中的编写顺序可以不用关心,反正它先启动监听器-->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext-*.xml</param-value>
  </context-param>

28. 此时就可以通过之前action中的update、save方法来建立一个categoryService对象了

29. 整合完毕后,现在对这个ssh进行一个小小的实验,首先定义
    CategoryAction.java
private Category category ;
    public Category getCategory() {
        return category;
    }
    public void setCategory(Category category) {
        this.category = category;
    }
30. 然后定义页面
    index.jsp
<a href="${pageContext.request.contextPath}/category_update.action?category.id=1&category.type=儿童休闲&category.hot=false">update</a>

31. 最后如果没有问题的话,就回过头来用JUnit测试一下SSHTest中的方法,最后就完成了SSH的搭建。

0 0