struts2框架的第一个hello world程序

先记下基本步骤吧,中间遇到很多莫名其妙的问题:

    步骤:

  1.创建web项目

  2.导入struts2的jar包,我新建了一个用户库,以后可以直接导入用户库

  3.在web.xml中配置FilterDispatcher

     struts2框架是基于mvc模式的,他提供了核心控制类,对所有请求统一处理,这个控制器是一个名为FilterDispatcher 的Servlet过滤器充当的

    2.1以前的版本配置模板如下:

     <?xml version="1.0" encoding="UTF-8"?>
<web-app 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">
 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>

 <filter>
  <filter-name>struts2</filter-name>
  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
 </filter>

 <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
</web-app>

4.编写Action类,实现Action接口,返回5中静态数据类型结果

 

5.在struts.xml文件中配置action:一个action创建完成后,还要为其配置一个URL,这样,FilterDispatcher才知道URL对应哪个action,当截止到URL时,FilterDispatcher就调用对应的action对请求进行处理

 

模板如下:

 

 <?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="HelloWorld" extends="struts-default">
  <action name="HelloWorld" class="org.sunxin.struts2.ch02.action.HelloWorldAction">
            <result name="success">/HelloWorld.jsp</result>
        </action>  
 </package>
</struts>

  

6.发布测试

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值