struts2环境搭建和入门程序

来源:互联网 发布:python url解码 编辑:程序博客网 时间:2024/05/18 03:32

1:导入jar包
2:web.xml配置核心过滤器

    <filter>        <filter-name>struts2</filter-name>        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>    </filter>    <!-- 让Struts 2的核心Filter拦截所有请求 -->    <filter-mapping>        <filter-name>struts2</filter-name>        <url-pattern>/*</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.3//EN"    "http://struts.apache.org/dtds/struts-2.3.dtd"><struts>        <package name="struts2" namespace="/" extends="struts-default">        <action name="hello" class="com.Hello">            <!-- 定义三个逻辑视图和物理资源之间的映射 -->            <result name="success">/WEB-INF/welcome.jsp</result>        </action>    </package></struts>

4.创建action类

public class Hello extends ActionSupport{    // 定义处理用户请求的execute方法    public String execute() throws Exception{        System.out.println("hello");            return SUCCESS;    }}

最后访问:项目路径/hello,成功跳转至welcome.jsp页面,说明入门程序搭建成功

原创粉丝点击