Struts2入门案例——基于Struts2任意两数据的代数和

//ch11_1_Input.jsp
<%
@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <head> <title>提交两数据页面</title> </head> <body> <form action="add" method="post"> 请输入两个整数:<br><br> 加数:<input name="x"/><br><br> 被加数:<input name="y"/><br><br> <input type="submit" value="求和"> </form> </body> </html>
//ch11_1_Positive.jsp
<%@ page language="java"  pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
  <head>
    <title>显示页面——代数和为非负整数</title>
  </head>
  
  <body>
        代数和为非负整数:<s:property value="sum"/>
  </body>
</html>
//ch11_1_Negative.jsp
<%@ page language="java"  pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
  <head>
    <title>显示页面——代数和为负整数</title>
  </head>
  
  <body>
        代数和为负整数:<s:property value="sum"/>
  </body>
</html>
//Ch11_1_Action.java
package Action;

public class Ch11_1_Action {
    private int x;
    private int y;
    private int sum;
    public int getX() {
        return x;
    }
    public int getY() {
        return y;
    }
    public void setY(int y) {
        this.y = y;
    }
    public int getSum() {
        return sum;
    }
    public void setX(int x) {
        this.x = x;
    }
    public String execute()
    {
        sum=x+y;
        if(sum>=0)
            return "+";
        else
            return "-";
    }    
}
<?xml version="1.0" encoding="UTF-8">
<!DOCTYPE struts PUBLIC
   "-//Apache Software Foundation//DTD Struts Configuration2.3//EN"
    "http://struts.apche.org/dtds/struts-2.3.dtd">
<struts>
         <package name="default"namespase="/" exetends="struts-default">
                <action name="add" calss="Action.Ch11_1_Action">
                <result name="+">/ch11_1_Positive.jsp</result>
                <result name="-">/ch11_1_Negative.jsp</result>
</struts>       

 

posted on 2017-12-19 23:12  小星_log  阅读(227)  评论(0编辑  收藏  举报