首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Struts温习(9)-关于错误的处理

2012-09-19 
Struts温习(9)--关于异常的处理本示例是以一个计算器的例子来写的,为了方便,这里处理的是算术异常index.js

Struts温习(9)--关于异常的处理

本示例是以一个计算器的例子来写的,为了方便,这里处理的是算术异常

index.jsp

?

<%@ page pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <title>Struts计算器</title>  </head>    <body>    <h3>Struts应用:简单计算器</h3><hr/>    <form action="${pageContext.request.contextPath}/manage/calculate.do" method="post">    <table border="1" width='500px'>    <tr>    <td>输入第一个数</td>    <td><input type="text" name="num1"/></td>    </tr>    <tr>    <td>选择运算符</td>    <td>    <select name="opt">    <option value="+">&nbsp;+&nbsp;</option>    <option value="-">&nbsp;-&nbsp;</option>    <option value="*">&nbsp;*&nbsp;</option>    <option value="/">&nbsp;/&nbsp;</option>    </select>    </td>    </tr>    <tr>    <td>输入第二个数</td>    <td><input type="text" name="num2"/></td>    </tr>    <tr>    <td colspan="2" align="center">    <input type="submit" value="执行计算"/></td>    </tr>    </table>    </form>      </body></html>

?CalculateForm.java

?

package com.javacrazyer.web.formbean;import org.apache.struts.action.ActionForm;public class CalculateForm extends ActionForm {private static final long serialVersionUID = 7273418667333097670L;private int num1;private int num2;private String opt;public int getNum1() {return num1;}public int getNum2() {return num2;}public void setNum2(int num2) {this.num2 = num2;}public void setNum1(int num1) {this.num1 = num1;}public String getOpt() {return opt;}public void setOpt(String opt) {this.opt = opt;}}

?CalculateService.java

?

package com.javacrazyer.web.action;public class CalculateService {public int calculate(int num1, int num2, String opt){int result = 0;if("+".equals(opt)){result = num1 + num2;}else if("-".equals(opt)){result = num1 - num2;}else if("*".equals(opt)){result = num1 * num2;}else if("/".equals(opt)){result = num1 / num2;}return result;}}

?CalculateAction.java

?

package com.javacrazyer.web.action;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.Action;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;import com.javacrazyer.web.formbean.CalculateForm;public class CalculateAction extends Action {public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception {//取数据CalculateForm cForm = (CalculateForm)form;int num1 = cForm.getNum1();int num2 = cForm.getNum2();String opt = cForm.getOpt();//执行业务逻辑CalculateService service = new CalculateService();//try{int result = service.calculate(num1, num2, opt);request.setAttribute("result", Integer.valueOf(result));//}catch(ArithmeticException ae){//ActionMessages errors = new ActionMessages();//ActionMessage err = new ActionMessage("出现算术异常情况了", false);//errors.add(ActionMessages.GLOBAL_MESSAGE, err);////放置到请求//this.saveErrors(request, errors);//return mapping.findForward("error_page");//}return mapping.findForward("result");}}

?msg.properties

?

arithmeticException.info=\u51FA\u73B0\u7B97\u672F\u5F02\u5E38\u60C5\u51B5\u4E86

?


Struts温习(9)-关于错误的处理

?WEB-INF/struts-config.xml

?

<?xml version="1.0" encoding="ISO-8859-1" ?><!DOCTYPE struts-config PUBLIC          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"          "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd"><struts-config><form-beans><form-bean name="calForm" type="com.javacrazyer.web.formbean.CalculateForm"/></form-beans><global-exceptions><exception key="xx.xx" type="xxx.DAOExcpetion" path="/innererror.jsp"/></global-exceptions><global-forwards><forward name="index" path="/index.jsp"/><forward name="error_page" path="/error.jsp"/></global-forwards><action-mappings><action path="/manage/calculate"name="calForm"type="com.javacrazyer.web.action.CalculateAction"scope="request"validate="false"><exception key="arithmeticException.info" type="java.lang.ArithmeticException" path="/error.jsp"/><forward name="result" path="/result.jsp"/><forward name="mysucc" path="/user_detail.jsp"/></action></action-mappings><message-resources parameter="msgs"/></struts-config>

上面action配置中就是专门配置的为算术异常处理的代码



?result.jsp

?

<%@ page pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <title>Struts计算器</title>  </head>    <body>    <h3>Struts应用:计算结果</h3><hr/>    <table border="1" width='500px'>    <tr>    <td>您输入第一个数是: ${param.num1}</td>    </tr>    <tr>    <td>您选择运算符是: ${param.opt}</td>    </tr>    <tr>    <td>您输入第二个数是: ${param.num2}</td>    </tr>    <tr>    <td>计算结果为: ${result}</td>    </tr>    <tr>    <td align="center">    <input type="button" onclick="history.go(-1)" value=" 返回 "/></td>    </tr>    </table>    </form>      </body></html>

?error.jsp

?

<%@ page pageEncoding="UTF-8"%><%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %><%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <title>服务器内部出现异常</title>  </head>    <body>    <h3>服务器内部出现异常</h3><hr/>    <html:messages id="err">    <li><bean:write name="err"/></li>    </html:messages>  </body></html>

?

计算正确的效果


Struts温习(9)-关于错误的处理
?


Struts温习(9)-关于错误的处理

?计算错误的效果


Struts温习(9)-关于错误的处理
?


Struts温习(9)-关于错误的处理

?

热点排行