Struts2 can't see exception and redirect to error page
up vote
0
down vote
favorite
I wrote this code all inside Action class (same code for exception handling) and it worked. Then we were asked to separate the code into Model class and Action class and use ModelDriven. Now the code works but it won't handle the exception anymore, it goes directly to 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>
java eclipse struts2
add a comment |
up vote
0
down vote
favorite
I wrote this code all inside Action class (same code for exception handling) and it worked. Then we were asked to separate the code into Model class and Action class and use ModelDriven. Now the code works but it won't handle the exception anymore, it goes directly to 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>
java eclipse struts2
What do you mean by "it doesn't handle the exception any more"? Your action always returnsSUCCESS
, you catch theInvalidTargetException
, and you throw away all your return values from the methods called byprocess
--what did you think would happen with this code?
– Dave Newton
Nov 22 at 17:26
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I wrote this code all inside Action class (same code for exception handling) and it worked. Then we were asked to separate the code into Model class and Action class and use ModelDriven. Now the code works but it won't handle the exception anymore, it goes directly to 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>
java eclipse struts2
I wrote this code all inside Action class (same code for exception handling) and it worked. Then we were asked to separate the code into Model class and Action class and use ModelDriven. Now the code works but it won't handle the exception anymore, it goes directly to 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>
java eclipse struts2
java eclipse struts2
asked Nov 22 at 4:29
JavaStudent
1
1
What do you mean by "it doesn't handle the exception any more"? Your action always returnsSUCCESS
, you catch theInvalidTargetException
, and you throw away all your return values from the methods called byprocess
--what did you think would happen with this code?
– Dave Newton
Nov 22 at 17:26
add a comment |
What do you mean by "it doesn't handle the exception any more"? Your action always returnsSUCCESS
, you catch theInvalidTargetException
, and you throw away all your return values from the methods called byprocess
--what did you think would happen with this code?
– Dave Newton
Nov 22 at 17:26
What do you mean by "it doesn't handle the exception any more"? Your action always returns
SUCCESS
, you catch the InvalidTargetException
, and you throw away all your return values from the methods called by process
--what did you think would happen with this code?– Dave Newton
Nov 22 at 17:26
What do you mean by "it doesn't handle the exception any more"? Your action always returns
SUCCESS
, you catch the InvalidTargetException
, and you throw away all your return values from the methods called by process
--what did you think would happen with this code?– Dave Newton
Nov 22 at 17:26
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423913%2fstruts2-cant-see-exception-and-redirect-to-error-page%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What do you mean by "it doesn't handle the exception any more"? Your action always returns
SUCCESS
, you catch theInvalidTargetException
, and you throw away all your return values from the methods called byprocess
--what did you think would happen with this code?– Dave Newton
Nov 22 at 17:26