struts2简单实例

在导入jar包和配置web.xml后,进行下面的操作
一、创建Action类

package action;
/** 
*类说明 :Struts2框架使用Action类处理用户请求
*@author :大湛湛
*/
public class HelloAction {
    /**
     * Action类中的方法必须用public修饰
     * 必须有返回值,且返回值类型为String
     * 方法名称没有要求,但是不能有参数列表
     */
    public String hello(){
    //访问方法时,在控制台打印hello struts2
        System.out.println("hello struts2");
        return null;
    }
}

二、配置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>
    <!-- 包结构 -->
    <package name="default" namespace="/" extends="struts-default">
        <!-- 配置Action -->
        <action name="hello" class="action.HelloAction" method="helloAction"/>
    </package>
</struts>

三、发布到服务器,在网页地址栏拼接hello.action进行访问测试
  地址栏输入http://localhost:8080/strutsTest/hello.action
控制台打印出hello struts2,说明访问action成功

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容