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

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我叫小八

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值