SSH整合终结版(三)

本文的前面两部分已对Spring和Hibernate两大框架进行了集成,下面继续对Struts进行整合。

第一步,原来我们对spring容器实例化要手工操作,现在我们通过在web.xml中配置监听器来对Spring容器进行实例化。代码如下:

文件名:web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
 xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

      <!-- 在web中实例化容器 -->
   <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-app>

第二步,在web.xml中配置struts。

<!-- Standard Action Servlet Configuration (with debugging) -->
   <servlet>
     <servlet-name>action</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>
     <init-param>
       <param-name>debug</param-name>
       <param-value>2</param-value>
     </init-param>
     <init-param>
       <param-name>detail</param-name>
       <param-value>2</param-value>
     </init-param>
     <load-on-startup>2</load-on-startup>
   </servlet>
        
   <!-- Standard Action Servlet Mapping -->
   <servlet-mapping>
     <servlet-name>action</servlet-name>
     <url-pattern>*.do</url-pattern>  
   </servlet-mapping>

 第三步,在WEB-INF目录下编写struts-config.xml文件。代码如下:

<?xml version="1.0" encoding="utf-8" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
 <action-mappings>
    <action path="/person/list" type="com.lyk.web.actions.PersonAction" validate="false">
       <forward name="list" path="/WEB-INF/page/personlist.jsp"/>
    </action>
     </action-mappings>

 <controller>
      <set-property property="processorClass"
               value="org.springframework.web.struts.DelegatingRequestProcessor"/>
 </controller>
</struts-config>

 其中已配置了一个action,下面我们对该action进行创建。代码如下:

package com.lyk.web.actions;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;


import com.lyk.service.PersonService;

public class PersonAction extends Action {
 @Resource PersonService personService;

 @Override
 public ActionForward execute(ActionMapping arg0, ActionForm arg1, HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
  // TODO 自动生成方法存根
  
 /* WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext
                        (this.getServlet().getServletContext());
   PersonService  personService = (PersonService)ctx.getBean("personService");*/
   arg2.setAttribute("persons", personService.getPersons());
   return arg0.findForward("list");
 }

}

第四步,编写View层JSP页面( <forward name="list" path="/WEB-INF/page/personlist.jsp"/>),在WEB-INF/page目录下创建personlist.jsp页面。代码如下:

文件名:personlist.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>人员列表</title>   
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->

  </head>
  <body>
   人员列表:<br>
   <c:forEach items="${persons}" var="person">
 id=${person.id},name= ${person.name} <br>
 </c:forEach>
     </body>
</html>
第五步,由于在Action的编写中我们采用了Spring的依赖注入功能对personService进行了注入,所以,我们需要在beans.xml文件中加入如下代码:

          <bean id="personService" class="com.lyk.service.imp.PersonServiceBean" />
           <bean id="smsService" class="com.lyk.service.imp.SMSServiceBean" />
          
           <bean name="/person/list" class="com.lyk.web.actions.PersonAction" />
           <bean name="/sms/list" class="com.lyk.web.actions.SMSAction" />

这样,实现了action类编写的简洁化。

最后,发布SSH项目,打开浏览器进行测试,输入http://localhost:8080/SSH/person/list.do,输出结果为(示例):

人员列表:
id=1,name= lyk
id=2,name= ii
id=3,name= 大P
id=4,name= 小S
如果看到此结果,则我们的SSH整合项目就成功了,哈哈,恭喜了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值