ssh框架整合之从struts2开始

来源:互联网 发布:网络监控交换机连接 编辑:程序博客网 时间:2024/05/17 03:26

ssh框架整合之从struts2开始

在整合ssh框架之前先建立一个简单的struts2的HelloWord工程,再从HelloWord工程基础上整合spring以及hibernate。

下载struts2的jar包

从官网下在struts2所需要的jar包,官网下载慢的话可以百度其他下载方式。下载完整后在lib目录下可以看到所有的jar包。创建struts2所需要的jar包主要有:

  • freemarker-2.3.23.jar
  • commons-fileupload-1.3.2.jar
  • commons-io-2.4.jar
  • commons-lang3-3.4.jar
  • commons-logging-1.1.3.jar
  • struts2-core-2.5.2.jar
  • javassist-3.20.0-GA.jar
  • log4j-api-2.5.jar
  • ognl-3.1.10.jar

创建web工程并建好包结构

创建空的web项目。并创建好包结构,工程目录如下图所示。

这里写图片描述

导入jar包并配置web.xml

将需要的jar拷贝到lib目录下之后。配置web.xml.具体配置如下:

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns="http://xmlns.jcp.org/xml/ns/javaee"     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee     http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"      id="WebApp_ID" version="3.1">  <display-name>mySshProject</display-name>  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list>    <!-- 配置struts -->  <filter>    <filter-name>struts</filter-name>    <filter-class>        org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter    </filter-class>  </filter>  <filter-mapping>    <filter-name>struts</filter-name>    <url-pattern>/*</url-pattern>  </filter-mapping></web-app>

创建action

在action包下,创建hellowordAction:

package com.cd.action;import com.opensymphony.xwork2.ActionSupport;public class HelloWordAction extends ActionSupport{    private static final long serialVersionUID = 1L;    public String helloWord(){        System.out.println("helloWord");        return SUCCESS;    }}

创建struts.xml并完成配置

在src目录下创建struts并配置如下:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"        "http://struts.apache.org/dtds/struts-2.5.dtd"><struts>    <package name="default" namespace="/" extends="struts-default" abstract="false">        <action name="hello" class="com.cd.action.HelloWordAction" method="helloWord">            <result name="success">/success.jsp</result>        </action>    </package></struts>

创建编写index.jsp以及success.jsp

在index.jsp中创建一个form表单,用来请求测试struts是否配置成功。

    <form action="hello" method="post">        <input type="submit" value="hello">    </form>

如果点击hello按钮可以跳转到success界面则说明配置成功。

源代码(提取码:ckk4):ssh_struts