学习Struts2_0300

来源:互联网 发布:如何屏蔽监控软件 编辑:程序博客网 时间:2024/04/30 15:14

Action 用法
我们先配置一下 struts.xml 文件

<constant name="struts.devMode" value="true" />    <package name="default" namespace="/" extends="struts-default">        <action name="hello" class="com.struts2.front.IndexAction1">            <result name="success">                /index.jsp            </result>        </action>    </package>

当访问到action中时 会调用com.struts2.front.IndexAction1类的execute方法 action中的class 可以不配置。 会调用默认的execute.
struts1和struts2 的区别就是 action 在struts1中 会生成一个类。 而在struts2中 会每次调用action方法都会生成一个 不会造成线程同步的问题
我们在看一下IndexAction。

package com.struts2.front;public class IndexAction1 {    public String execute(){        return "success";           }}
package com.struts2.front;import com.opensymphony.xwork2.Action;public class IndexAction2 implements Action{    @Override    public String execute() throws Exception {        // TODO Auto-generated method stub        return "success";    }}
package com.struts2.front;import com.opensymphony.xwork2.ActionSupport;public class IndexAction3 extends ActionSupport{    @Override    public String execute(){        return "success";    }}

在开发的时候我们会调用第三种方法 因为ActionSupport中 已经为我们封装了一些方法。 不需要我们自己重新写

0 0