MyEclipse6.5下struts2+spring2+hibernate3 整合

MyEclipse6.5下struts2+spring2+hibernate3 整合

今天开始正式学习ssh开发了,想用ssh框架开发当然先的搭建开发平台,我用的相关软件版本如下:

1、MyEclipse6.5

2、struts2.0.11.2,现在官方网站上的最高版本为Struts2.1.6,我之所以没有用最新版本是因为最新版本在网上的资料少一些,万一出问题解决起来有难度,网上能够咨询的地方也不多。下面的版本没有用最新也是基于这个道理。下载地址:http://www.apache.org

3、spring2.5.5,最新版为2.5.6,下载地址为http://www.springsource.org/download

4、hibernate3.2 最新版为3.3.2,下载地址为https://www.hibernate.org

     软件准备好了就可以开始搭建环境了,由于是新学习,因此没有使用myeclipse提供的自动引入spring和hibernate支持。

1、在myeclipse6.5下新建一个web project项目

2、先来引入struts2的支持:把下载回来的struts2的压缩包解开,把解压目录下lib目录下的struts-core-2.0.11.2.jar,xwork-2.0.5.jar,freemarker-2.3.8.jar,ognl-2.6.11.jar,struts2-spring-plugin-2.0.11.2.jar五个jar文件拷贝到刚才新建项目的WebRoot\WEB-INF\lib目录下。

3、引入spring2支持:把spring解压目录下dist下的spring.jar拷贝到WebRoot\WEB-INF\lib目录下

4、hibernate3支持:把hibernate解压目录下的hibernate3.jar复制到WebRoot\WEB-INF\lib目录下。

5、一些基础支持的引入:如日志、数据库驱动(我用的是mysql数据库,所以引入的是mysql数据库支持包)、连接池、以及一些基础的公用的支持包,这些包都能在struts2、spring、hibernate解压目录下找到,有些包三个目录下都有,我一般找版本比较高的。具体的包名如下:c3p0-0.9.1.2.jar , dom4j-1.6.1.jar , commons-collection.jar , commons-dbcp.jar , commons-io.jar, commons-pool.jar, jta.jar , cglib-nodep-2.1.3.jar , mysql-connector-java-5.0.8.jar , commons-logging.jar.

注:以上只是必须基础的包,真正开发不会只有这些包,我的原则是需要了再加,而不是一开始把所有的包都放进去.

     接下来要做的是配置工作了

1、配置web.xml,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”>
<!– 配置struts2的拦截器(过滤器) –>
  <filter>
       <filter-name>struts2</filter-name>
       <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>

<filter-mapping>
      <filter-name>struts2</filter-name>
       <url-pattern>/*</url-pattern>
</filter-mapping>

<!–配置spring的监听器–>
<listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>

2、在src目录下建立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.i18n.encoding” value=”UTF-8″></constant>

<package name=”struts2″ extends=”struts-default”>

<!–这个action用于测试配置是否正确,没有实际用处–>
     <action name=”testAction” class=”testAction”>

            <result name=”success”>/test.jsp</result>

     </action>

</package>
</struts>

3、配置applicationContext.xml,在WEB-INF下建立此文件,或从其他地方复制,内容如下

<?xml version=”1.0″ encoding=”UTF-8″?>
<!–
  – Middle tier application context definition for the image database.
  –>
<beans xmlns=”http://www.springframework.org/schema/beans”
  xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
  xmlns:context=”http://www.springframework.org/schema/context”
  xmlns:tx=”http://www.springframework.org/schema/tx”
  xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd”>

<!– Configurer that replaces ${…} placeholders with values from a properties file –>
<!– (in this case, JDBC-related settings for the dataSource definition below) –>

<bean id=”testAction” class=”com.test.action.Test”>

</bean>
</beans>

4、在scr目录下建立com.test.action包并在该包下建立Test.java,内容如下

package com.test.action;

import com.opensymphony.xwork2.ActionSupport;

public class Test extends ActionSupport {

@Override
public String execute() throws Exception {

  return SUCCESS;
}

}

5、在webroot目录下建立test.jsp文件,内容如下:

<%@ page language=”java” import=”java.util.*” pageEncoding=”ISO-8859-1″%>
<%
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>My JSP ‘test.jsp’ starting page</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>
  </body>
</html>

5、配置tomcat在tomcat的server.xml的host标签内加入  <Context path=”/test” docBase=”D:\test\WebRoot”  reloadable=”true” />

6、在myeclipse 中启动tomcat后在浏览器地址栏中输入http://localhost:8080/test/testAction.action 如果能够正确地显示“这是一个测试”就表示大功告成了,接下来就可以做一些实际的工作了。 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/110452.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • getmethod方法什么意思_getstring方法

    getmethod方法什么意思_getstring方法getMethod方法要注意的地方:类对象.getMethod(mName,HttpServletRequest.class,HttpServletResponse.class);第一个参数是mName“方法名称”第二个参数是“方法参数的类对象”//这个就是为什么他是.class的原因,为什么参数是类对象?因为这个是Java规定的举个例子  这是一个抽取出的通用的servlet(页面请求经过的第…

    2025年11月24日
    6
  • Python3字符串替换replace(),translate(),re.sub()

    Python3字符串替换replace(),translate(),re.sub()Python3的字符串替换,这里总结了三个函数,和`translate()re.sub()`replace()python中的方法把字符串中的old(旧字符串)替换成new(新字符串

    2022年7月5日
    21
  • python matplotlib 画图刻度、图例等字体、字体大小、刻度密度、线条样式设置

    python matplotlib 画图刻度、图例等字体、字体大小、刻度密度、线条样式设置设置输出的图片大小:figsize=11,9figure,ax=plt.subplots(figsize=figsize)画简单的折线图,同时标注线的形状、名称、粗细:A,=plt.plot(x1,y1,’-r’,label=’A’,linewidth=5.0,ms=10)其中线条样式以及颜色设置可参考:https://blog….

    2022年6月11日
    107
  • win7 boot设置_重装系统boot missing

    win7 boot设置_重装系统boot missing转自 http://blog.wsdd.org/安装linux,vista/win7双系统后,怎么引导是个问题理论上,可以从windows的bootloader引导linux,也可以linux的grub引导windows但windows更霸道,经常霸占MBR,所以最好是linux不放MBR,然后从windows的bootloader引导linux把linux装在自己的分区,不要

    2022年10月12日
    6
  • 【树莓派】一根网线将树莓派4B直连笔记本电脑的方法

    【树莓派】一根网线将树莓派4B直连笔记本电脑的方法当我们为新买来的树莓派刷上系统之后,如何将树莓派和笔记本电脑连接到同一网络使用SSH工具登录树莓派呢?相信大多数人的做法是将树莓派连接到路由器上,然后通过SSH工具(putty,SecureCRT等)远程登录,但如果你没有显示器和HDMI线,那么,这里介绍如何使用一根网线来连接树莓派。准备材料1、树莓派2、已经刷入系统的SD卡3、一根网线4、一个已经连接互联网的笔记本5、usb网口…

    2022年6月5日
    43
  • swoole手册

    swoole手册

    2021年11月3日
    52

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号