MVC分层

 

题目:客户端给出一个月薪数字过来,服务器进行计算,算一下个人所得税是多少钱,然后显示给客户端看。

 

垃圾代码

1、  先建一个index.jsp

a. 接受收入income 数字

b. 开始计算税费

  1. 显示结果

 

在url输出

//http://localhost:8080/test014/index.jsp?income=10000

 

jsp文件中打如下代码:

//第一步:接受客户端的参数

String income_str = request.getParameter("income");

float income = Float.parseFloat(income_str);

 

//第二步:处理业务逻辑

float tax_income = income - 3500;//应交所得税的金额

float tax = tax_income*0.2f-555;

//第三步:输出结果

%>

应交税款<%=tax %>

 

思考:

1、  实体是Tax

2、  分层 M ,V , C

 

第一步:先建立实体类Tax.java

 

第二步:做数据库的操作(这个例子没有,跳过)

 

第三步:写业务逻辑的代码

1. 先写接口(定义一下有什么方法)

2. 写实现类

 

第四步:写控制器

(接受客户端的请求参数,调用M,跳转到v)

建立一个TaxAction.java

(控制器用Servlet替代jsp,它没有任何html代码)

a. 控制器接受客户端的请求,获取客户端传递的参数

b. 调用业务逻辑的控件,进行计算,得到结果。

  1. 把结果放在request的属性中,然后把请求转发给view层

 

第五步:编写view层jsp页面,用于显示结果。

(view层没有任何的java代码)

 

 

 

 

jsp收税案例:

首先创建一个jsp文件

然后在src中创建四个包entity、action、dao、service

然后在entity中创建一个实体类TAX

public class Tax {
 private float income;//收入
 private float taxRate;//税率
 private float qcd;//速算扣除数
 private float taxDue;//应交税金
 
 
 public float getIncome() {
  return income;
 }
 public void setIncome(float income) {
  this.income = income;
 }
 public float getTaxRate() {
  return taxRate;
 }
 public void setTaxRate(float taxRate) {
  this.taxRate = taxRate;
 }
 public float getQcd() {
  return qcd;
 }
 public void setQcd(float qcd) {
  this.qcd = qcd;
 }
 public float getTaxDue() {
  return taxDue;
 }
 public void setTaxDue(float taxDue) {
  this.taxDue = taxDue;
 }
 
}    

 

然后在action中创建一个servlet类:TaxAction

public class TaxAction extends HttpServlet {

 
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  //第一步:获取参数
  String income_str=request.getParameter("income");
  float income=Float.parseFloat(income_str);
  
  //第二步:调用各种业务逻辑
  TaxService ts=new TaxServiceimpl();
  Tax tax=ts.calcTax(income, 0.2f, 555);
  
  //第三步:请求转发给tax.jsp显示结果
  request.setAttribute("tax", tax);
  request.getRequestDispatcher("tax.jsp").forward(request, response);
 }
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  doGet(request, response);
  
 }
}
 
 
然后在service类中创建一个接口TaxService:
public interface TaxService {
 public  Tax calcTax(float income,float taxRate,float qcd);
 
}

 

然后创建一个类:TaxServiceimpl:

 

public class TaxServiceimpl implements TaxService {
 @Override
 public Tax calcTax(float income, float taxRate, float qcd) {
  // TODO Auto-generated method stub
  Tax t=new Tax();
  t.setIncome(income);
  t.setTaxRate(taxRate);
  t.setQcd(qcd);
  
  if(income<=3500){
  t.setTaxDue(0);
   
  }else{
   t.setTaxDue(  (income-3500)*0.2f-qcd  );
   
  }
  return t;
 }
}

 

 最后在tax.jsp中打入如下代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
您的收入:${tax.income}<br>
税率:${tax.taxRate} <br>
速算和除数:${tax.qcd} <br>
应交税金:${tax.taxDue} <br>
</body>
</html>

 

 

然后运行,在url中打入如下代码:

http://localhost:8080/test015/TaxAction?income=10000

即可完成!

posted @ 2018-06-20 19:29  吴小帅  阅读(275)  评论(0编辑  收藏  举报