第一个 Struts2 程序

下面主要介绍如何在 myEclipse 下使用 Struts2 搭建一个简单的 hello world 程序

1. Struts2 的环境需求

Servlet API 2.4
JSP API 2.0
Java 5

2. 第一个 struts2 项目的搭建

step1. 首先我们在 myeclipse 下新建一个 web 工程,假设命名为 hellostruts

step2. 鼠标放在项目名称上右键依次选择 MyEclipse –> Project Facets –> Install Apache Struts(2.x) Facet

step3. 这个时候已经配置好 struts2 了,下面我们来看看项目的变化。
(1)项目的 lib-INF 的 lib 目录下多了许多和 struts 相关的 jar 包。
(2)在 web.xml 下多了如下代码:

 <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>*.action</url-pattern>
  </filter-mapping>

(3) 在 src 目录下多了个 struts.xml 的文件,其初始内容如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">

<struts>

</struts>    

step4. 编写第一个 action 类,注意要继承 ActionSupport 这个类,代码如下:

package com.cighao.action;
import com.opensymphony.xwork2.ActionSupport;
public class HelloStruts extends ActionSupport {

    @Override
    public String execute() throws Exception{
        //System.out.println("start action");
        return "success";
    }
}

step5. 在 struts.xml 中写入配置信息。代码如下:

<struts>

    <package name="default" namespace="" extends="struts-default">
        <action name="hello" class="com.cighao.action.HelloStruts" >
            <result name="success">/index.jsp</result>
        </action>
    </package>

</struts>    

step6. 添加视图层的内容,我们在 index.jsp 中加入 hello struts2 这段文字。

3. 项目演示

我们部署项目后,在浏览器中输入 localhost:8080/hellostruts/hello.action 就可以看到 index.jsp 中的内容,这里显示的是:

hello struts2


想更多的了解 Struts2 的其他内容可以参见本博客的其他关于 Struts 的文章。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值