eclipse搭建ssh框架(二)

這篇開始整合struts2+spring

1、加入spring的包,或者是之前你已經導入所有的包,現在只需加入那個刪掉的struts+spring包即可。

2、在src下複製struts.xml然後改名爲spring的配置文件applicationContext.xml,並加入對struts的管理

<?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans  
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

        <bean id="loginAction" class="com.action.LoginAction" scope="prototype">
        </bean>
    </beans>   

3、struts.xml改成

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
   "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="false" />
   <package name="struts2" extends="struts-default">
        <!-- 此處的class的內容要與Spring配置文件中的bean的id相同 -->
        <action name="login" class="loginAction">
            <result name="success">/result.jsp</result>
            <result name="input">/index.jsp</result>
        </action>
    </package>
</struts>

4、在web.xml加入spring相關支持

<!-- 用來定位Spring框架配置文件 ,最好放在struts配置的前面,防止出現意外錯誤。-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext*.xml,classpath*:applicationContext*.xml</param-value>
    </context-param>
    <!-- 配置Spring監聽 ,這也放在struts前面吧。-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

5、把index.jsp改成(其實只是添加“+spring”的字眼。。)

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
   pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
   <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Hello World</title>
</head>
<body>
   <h1>Hello World From Struts2 + Spring</h1>
   <form action="login">
      <label for="name">Please enter your name</label><br/>
      <input type="text" name="name"/>
      <input type="submit" value="Say Hello"/>
   </form>
</body>
</html>

6、重啓tomcat,打開頁面

這裏寫圖片描述

點擊登錄後彈出

這裏寫圖片描述

這裏便完成了struts2跟spring的整合,最後一篇整合hibernate

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章