Struts2 can't see exception and redirect to error page












0















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>









share|improve this question























  • 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 '18 at 17:26
















0















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>









share|improve this question























  • 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 '18 at 17:26














0












0








0








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>









share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 4:29









JavaStudentJavaStudent

1




1













  • 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 '18 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 '18 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 '18 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 '18 at 17:26












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

SQL update select statement

'app-layout' is not a known element: how to share Component with different Modules