struts2的第一个例子

比hello world好得多

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;  // 计算两个整数的代码数和
    }
}

<?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="struts2" namespace="/mystruts" extends="struts-default">
          <action name="sum" class="action.FirstAction">
              <result name="positive">/positive.jsp</result>
              <result name="negative">/negative.jsp</result>
          </action>
      </package>
  </struts>

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
  <%@ taglib prefix="s" uri="/struts-tags" %>
  
  <html>
    <head>
      <title>显示代数和</title>
    </head>
    
    <body>
      代数和为负整数<h1><s:property value="sum" /></h1>
      
    </body>
  </html> 

注意第10行的sum对应着java action中的第42行,这个get没有对应的属性

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
  <%@ taglib prefix="s" uri="/struts-tags" %>
  
  <html>
    <head>
      <title>显示代数和</title>
    </head>
    
    <body>
      代数和为负整数<h1><s:property value="sum" /></h1>
      
    </body>
  </html> 

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
  <%@ taglib prefix="s" uri="/struts-tags" %>
  
  <html>
    <head>
      <title>显示代数和</title>
    </head>
    
    <body>    
      代数和为非负整数<h1><s:property value="sum" /></h1>
    </body>
  </html> 

李宁原文: http://www.blogjava.net/nokiaguy/archive/2008/04/15/193229.html

含库源代码:http://pan.baidu.com/share/link?shareid=441037&uk=3878681452

Hello World的例子:http://188029.net/struts2/HelloWorldExample.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值