学习 -- 使用Struts2实现第一个程序

Struts2的工作流程
    Struts与WebWork的工作方式类似,Struts2同样使用了拦截器作为其处理用户请求的控制器。在Struts2中有一个核心控制器FilterDispatcher,它负责处理用户的所有请求,如果遇到以.action结尾的请求URL,就会交给Struts2框架来处理,Struts2的工作流程可以用图表示。


可以把内容简化为下面的样式


Struts2框架程序的步骤大致如下:


知道了Struts 2框架程序的开发步骤,我们就可以开始Struts 2程序的开发了,同样,我们来看看如何用Struts 2输出最经典的“HelloWorld!”语句。

1. 配置web.xml
Struts2的web.xml 文件配置方法非常简单,即在web.xml中配置Struts2提供的过滤器,并设置为所有的请求(/*)都要通过这个过滤器
<filter>
  <filter-name>struts2</filter-name>    ----Struts2过滤器的名称
  <filter-class>
   org.apache.struts2.dispatcher.FilterDispatcher -----指定Struts2过滤器的类名
  </filter-class>
 </filter>
 <filter-mapping>        --配置Struts2过滤器要过滤的路径
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>


2. 编写JSP界面
使用Struts2标签库提供的“property”标签用来显示message的属性值,
在使用Struts2提供的标签库之前,需要在JSP中引入这个标签库,前缀定义为"s"
<%@ taglib uri="/struts-tags" prefix="s" %>

<!doctype html>
    <html>
        <head> 
   
  <title>HelloWorld</title>

         </head>   
<body>
 <h2>
  <s:property value="message" />
 </h2>
</body>
</html>

3. 编写Action
Action 类是最基本的逻辑处理单元,在MVC模式中分发器分发给不同的Action类来处理请求。在Struts2中Action类不必再实现Action接口,可以是任何类。但是一般还要继承ActionSupport类,因为其提供了大量的基本功能,如错误信息处理。
import com.opensymphony.xwork2.ActionSupport;

public class HelloWorld extends ActionSupport {

<span style="white-space:pre">	</span>public static final String MESSAGE = "Hello World! I'm from struts2";
 
<span style="white-space:pre">	</span>private String message;
 
<span style="white-space:pre">	</span>public String getMessage() {
<span style="white-space:pre">		</span>return message;
<span style="white-space:pre">	</span>}

<span style="white-space:pre">	</span>public void setMessage(String message) {
<span style="white-space:pre">		</span>this.message = message;
<span style="white-space:pre">	</span>}

<span style="white-space:pre">	</span>public String execute() throws Exception {
<span style="white-space:pre">		</span>setMessage(MESSAGE);
<span style="white-space:pre">		</span>return SUCCESS;
        }
}

4. 配置文件中增加映射
Struts2的配置文件是struts.xml,所有请求和分发以及其他配置都在这个文件中定义,struts.xml文件应该放在WEB-INF目录下的classes文件中。如下所示,配置了一个名称为HelloWorld的Action,处理类是struts2.HelloWorld,处理后的结果转到helloWorld.jsp页面上

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



启动Tomcat,并在浏览器中输入http://localhost:8080/Struts2/HelloWorld.action,就ok了
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值