我在Action类中都编写了此代码(用于异常处理的代码相同),并且可以正常工作。然后,我们被要求将代码分为Model类和Action类,并使用ModelDriven。现在,代码可以工作了,但不再处理该异常,它直接进入display.jsp。
SaleBean.java
package sale.compute.model;
import java.io.IOException;
import exception.InvalidTargetException;
public class SaleBean {
//input
private String itemName;
private double SRP; //Sales Retail Price
private double TSP; //Target Sales Price
//computed
private double SPM; //Sales Percent Markup
public void process() throws InvalidTargetException, IOException {
computeSPM();
checkSPM();
}
//Computes Sales Percent Markup
public void computeSPM() {
SPM = Math.round((((TSP-SRP)/SRP) * 100));
}
//Checks if SPM is above 15%
public String checkSPM() throws InvalidTargetException, IOException {
try {
computeSPM();
if(SPM > 15)
{
throw new InvalidTargetException();
}
else
{
return "success";
}
}
catch (InvalidTargetException ite)
{
return "error";
}
}
//Getters-setters
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public double getSRP() {
return SRP;
}
public void setSRP(double sRP) {
SRP = sRP;
}
public double getTSP() {
return TSP;
}
public void setTSP(double tSP) {
TSP = tSP;
}
public double getSPM() {
return SPM;
}
public void setSPM(double sPM) {
SPM = sPM;
}
}
//DB methods
InvalidTargetException.java
package exception;
public class InvalidTargetException extends Exception {
public InvalidTargetException() {
super("invalid SPM");
}
}
ComputeSaleAction.java
package sale.compute.action;
import java.io.IOException;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import exception.InvalidTargetException;
import sale.compute.model.*;
public class ComputeSaleAction extends ActionSupport implements ModelDriven<SaleBean>{
private SaleBean saleBeanObj = new SaleBean();
public String execute() throws InvalidTargetException, IOException {
saleBeanObj.process();
//saleBean.insertRecord();
return SUCCESS;
}
public SaleBean getSaleBeanObj() {
return saleBeanObj;
}
public void setSaleBeanObj(SaleBean saleBeanObj) {
this.saleBeanObj = saleBeanObj;
}
@Override
public SaleBean getModel() {
return saleBeanObj;
}
}
struts.xml
<struts>
<package name="default" extends="struts-default">
<action name="compute" class="sale.compute.action.ComputeSaleAction">
<!-- added this acc to codejava.net/how to handle exceptions in Struts2 -->
<exception-mapping result="error" exception="InvalidTargetException"></exception-mapping>
<result name="success">/display.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
error.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page isErrorPage="true"%>
<!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=ISO-8859-1">
<!-- html code here -->
<form action = "index.jsp">
<input type = 'submit' value = "<< GO BACK>>">
</form>
</body>
</html>