第一个struct2程序

【第1步】 安装Struts2

    这一步对于Struts1.x和Struts2都是必须的,只是安装的方法不同。Struts1的入口点是一个Servlet,而Struts2的入口点是一个过滤器(Filter)。因此,Struts2要按过滤器的方式配置。下面是在web.xml中配置Struts2的代码:

<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>

编写action类

package action;
  
import com.opensymphony.xwork2.ActionSupport;
  
public class FirstAction extends ActionSupport
{
    private int operand1;
    private int operand2;
  
    public String execute() throws Exception
    {
        if (getSum() >= 0)  // 如果代码数和是非负整数,跳到positive.jsp页面
        {
            return "positive";
        }
        else  // 如果代码数和是负整数,跳到negative.jsp页面
        {
            return "negative";
        }
    }
  
    public int getOperand1()
    {
        return operand1;
    }
  
    public void setOperand1(int operand1)
    {
        System.out.println(operand1);
          this.operand1 = operand1;
    }
  
    public int getOperand2()
    {
        return operand2;
    }  
    public void setOperand2(int operand2)
    {
        System.out.println(operand2);
        this.operand2 = operand2;
    }
    public int getSum()
    {
        return operand1 + operand2;  // 计算两个整数的代码数和
    }
}

  。。。

原文地址:https://www.cnblogs.com/frankzone/p/8068103.html