input.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
web.xml
Struts Blank002 struts2 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter struts2 /*
两个结果界面
positive.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
代数和为非负数,其值为:${x}+${y}=${sum}
negative.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
代数和为负数,其值为:(${x})+(${y})=(${sum})
Add类
package bean;
public class Add {
double x,y;
double sum;
public Add(double x, double y) {
super();
this.x = x;
this.y = y;
sum=x+y;
}
public Add() {
super();
this.x = 0;
this.y = 0;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public double getSum() {
return x+y;
}
public void setSum(double sum) {
this.sum = sum;
}
}
1.普通的Java类作为Action类与属性驱动传参
package com.action;
public class SumAction {
double x,y;
double sum;
public SumAction(double x, double y) {
super();
this.x = x;
this.y = y;
this.sum=this.x+this.y;
}
public SumAction() {
super();
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public double getSum() {
return sum;
}
public void setSum() {
this.sum = this.x+this.y;
}
public String qiuHe(){
sum=x+y;
if(sum<0)
return "-";
else
return "+";
}
}
对应的struts.xml:
为了能够开发更规范的Action类,struts2提供Action接口,并为Action接口提供了一个实现类ActionSupport
positive.jsp negative.jsp
2.继承ActionSupport实现Action与属性驱动传承
package com.action;
import com.opensymphony.xwork2.ActionSupport;
public class SumActionSupport extends ActionSupport{
double x;
double y;
double sum;
public String execute() throws Exception{
sum=x+y;
if(sum<0) return "-";
else return "+";
}
public SumActionSupport(double x, double y) {
super();
this.x = x;
this.y = y;
sum=x+y;
}
public SumActionSupport() {
super();
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public double getSum() {
return sum;
}
public void setSum(double sum) {
this.sum = sum;
}
}
对应的struts.xml:
positive.jsp negative.jsp
3.采用模式驱动设计Action
package com.action; import bean.Add; import com.opensymphony.xwork2.*; public class SumActionModelDriven extends ActionSupport implements ModelDriven{ private Add add; @Override public Add getModel() { // TODO Auto-generated method stub add=new Add(); return add; } public String execute() throws Exception{ String forward=null; if(add.getSum()<0) forward="-"; else forward="+"; return forward; } }
对应的struts.xml
positive.jsp negative.jsp



