我在service层配置了事务管理,如果出现数据库操作的异常,确实可以做到事物回滚,但是,一旦出现操作异常,网页就会答应出错误来,可是我想要捕获这个异常,然后传一个特定值给Action,这样,可以给页面返回一个错误提示,这样该怎么做。我在service 里把调用Dao的地方用try catch包起来了,可以还是不可以

解决方案 »

  1.   

    写一个 捕获异常的 Controller  其他Controller  都继承它package co.jp.nec.controller.common;import java.io.EOFException;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.sql.SQLException;
    import java.text.SimpleDateFormat;
    import java.util.Date;import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.web.bind.annotation.ExceptionHandler;import co.jp.nec.model.common.ErrorModel;
    import co.jp.nec.serviceImp.CommonService;
    import co.jp.nec.util.SystemDateFormat;@Service
    public class BaseExceptionController {

    @Autowired
    private CommonService commonService;

    // 画面ID
    String errGamenId;

    String errProcessId;

    @ExceptionHandler(Exception.class)
    public String handleException(Exception ex, HttpServletRequest request,HttpServletResponse response) throws ServletException {
    // Session
    HttpSession session = request.getSession();
    if (session == null) {
    if ("XMLHttpRequest".equals(request.getHeader("X-Requested-With"))) {
    throw new ServletException();
    } else {
    return "common/error";
    }
    }
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
    String dateAll = sdf.format(new Date());
    String date = dateAll.split(" ")[0];
    String time = dateAll.split(" ")[1];
    String ipAdress = (String) session.getAttribute("pcIP"); ErrorModel errorModel = new ErrorModel();
    errorModel.setGamenId(errGamenId);
    errorModel.setModelId(errProcessId);
    errorModel.setStrDate(date);
    errorModel.setStrTime(time);
    errorModel.setStrIp(ipAdress); // 异常种类
    if (ex instanceof IOException) {
    // log出力
    errorModel.setStrError("入出力例外(IOException)");
    commonService.errorLogOutput(errorModel, request);
    } else if (ex instanceof EOFException) {
    // log出力
    errorModel.setStrError("EOFException");
    commonService.errorLogOutput(errorModel, request);
    } else if (ex instanceof FileNotFoundException) {
    // log出力
    errorModel.setStrError("FileNotFoundException");
    commonService.errorLogOutput(errorModel, request);
    } else if (ex instanceof SQLException) {
    // log出力
    errorModel.setStrError("SQLException");
    commonService.errorLogOutput(errorModel, request);
    } else if (ex instanceof NumberFormatException) {
    // log出力
    errorModel.setStrError("NumberFormatException");
    commonService.errorLogOutput(errorModel, request);
    } else if (ex instanceof RuntimeException) {
    // log出力
    errorModel.setStrError("RuntimeException");
    commonService.errorLogOutput(errorModel, request);
    } else {
    // log出力
    errorModel.setStrError("異常");
    commonService.errorLogOutput(errorModel, request);
    }

    if ("XMLHttpRequest".equals(request.getHeader("X-Requested-With"))) {
    throw new ServletException();
    } else {
    return "common/error";//跳到error画面
    }
    } public String getErrGamenId() {
    return errGamenId;
    } public void setErrGamenId(String errGamenId) {
    this.errGamenId = errGamenId;
    } public String getErrProcessId() {
    return errProcessId;
    } public void setErrProcessId(String errProcessId) {
    this.errProcessId = errProcessId;
    }}
      

  2.   

    楼主可以这样在service里面配置事务,在action中捕获比如说:
    try{
       方法
    }catch(''''){
        //捕捉到异常进行一些信息提示
         String errMsg = “错误信息”;
         return "";//把错误信息传到页面直接显示
    }
      

  3.   

    你的事务是放在service里面。
    如果你将service里面的代码try catch了 。 spring就不能够捕获到你抛出的异常了。 也就不会执行事务了。你在service里面应该继续抛  。 或则在service里面try catch ,然后在catch块里面抛出你自定义的异常,异常消息你可以自己定义。  然后在action层 ,调用service方法的时候用try catch 。如果catch到异常  就带一些提示消息给页面。
      

  4.   


    切记,你配置的是捕获service的异常,一旦你用try catch包起来的话就相当于是你自己捕获异常了,声明式事务就捕获不到异常就没法回滚咯。