栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

struts2实现加法运算

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

struts2实现加法运算

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
	
  

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/342912.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号