构建一个Struts2项目

Struts2

Struts 2以WebWork为核心,采用拦截器的机制来处理用户的请求,这样的设计也使得业务逻辑控制器能够与ServletAPI完全脱离开

1、建项目
—–用MyEclipse建一个web项目
2、导包
这里写图片描述
3、修改web.xml文件
因为web项目一启动就会访问web.xml文件,得到初始化配置信息,所以你要在web.xml文件中配置Struts关于filter的信息

<?xml version="1.0" encoding="UTF-8"?>  
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  

        <display-name>Struts Blank</display-name>  

        <filter>  
            <!-- 配置Struts2核心Filter的名字 -->  
            <filter-name>struts2</filter-name>  
            <!-- 配置Struts2核心Filter的实现类 -->  
            <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>  
        </filter>  
        <!-- 配置Filter拦截的URL -->  
        <filter-mapping>  
            <!-- 配置Struts2的核心FilterDispatcher拦截所有用户请求 -->  
            <filter-name>struts2</filter-name>  
            <url-pattern>/*</url-pattern>  
        </filter-mapping>  

        <welcome-file-list>  
            <welcome-file>index.html</welcome-file>  
        </welcome-file-list>  
    </web-app>  

4、写相关action类

package com.xingyao.www.action;

import com.opensymphony.xwork2.ActionSupport;

//一个类实现ActionSupport就成为一个action类(还要在struts.xml里面配置信息)
public class HelloWorldAction extends ActionSupport {

    private static final long serialVersionUID = 1L;
    @Override
    //重写一个基本方法
    public String execute() throws Exception {
        System.out.println("执行action");
        return "SUCCESS";
    }
}

5、写Struts.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>

<!-- 规定Struts.xml的格式 -->
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <!-- 规定包,这是全部设为默认 -->
    <package name="default" namespace="/" extends="struts-default">
        <!-- 配置helloWorldAction的信息名为:HelloWorld(用于页面访问的时候用),配置它的类路径 -->
        <action name="HelloWorld" class="com.xingyao.www.action.HelloWorldAction">
            <!-- 配置返回结果名为SUCCESS,则服务器跳转到index.jsp -->
            <result name="SUCCESS">index.jsp</result>
        </action>
    </package>
</struts>

6、发布访问
localhost:8080/Struts2_20160213/HelloWorld.action
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值