Announcement Announcement Module
Collapse
No announcement yet.
Spring MVC hiding error messages Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring MVC hiding error messages

    I'm developing on an existing application, and I have the following problem: spring mvc (it seems) hides some of the error messages under debug statements.

    Here's an example:
    Code:
    DEBUG [http-8080-1] DispatcherServlet.processHandlerException(1117) | Handler execution resulted in exception - forwarding to resolved error view: ModelAndView: reference to view with name 'xmlError'; model is {exception=java.lang.IllegalArgumentException: org.hibernate.QueryParameterException: could not locate named parameter [subscriberList]}
    java.lang.IllegalArgumentException: org.hibernate.QueryParameterException: could not locate named parameter [subscriberList]
            at org.hibernate.ejb.QueryImpl.setParameter(QueryImpl.java:185)
    ....
    Any idea how I can stop this behavior? I'd like to have spring on ERROR mode in the log4j config, but still be able to see major application errors

  • #2
    What does the controller code look like that you call this hibernate function from?

    Comment


    • #3
      We're using Hibernate's implementation of JPA, the exception was just an example (it happens for most exceptions, even non-hibernate related)

      The controllers mostly extend SimpleFormController, with the usual methods, formBackingObject, onSubmit, initBinder, showForm. Some have handleRequest as well. The behaviour is the same everywhere.

      Code:
      public class ScheduleFormController  extends BaseFormController {
      	private transient final Log log = LogFactory.getLog(ScheduleFormController.class);
      
          public ScheduleFormController() {
      
              setCommandName("schedule");
              setCommandClass(SchedulePresentation.class);
          }
      	
          /**
           * First method called; removes form specific attributes
           * from the request if the cancel button has been pressed
           */
      	public ModelAndView handleRequest(HttpServletRequest request,
                  HttpServletResponse response)
      			throws Exception {		
      		// code which removes some attributes from the request
                      // if cancel was pressed and which sets some attributes otherwise
                     // simple stuff like days of week 
      		
      		return super.handleRequest(request, response);
      	}	
      	
          /**
           * Creates the object which contains the values for/from the form
           */
          protected Object formBackingObject(HttpServletRequest request)  throws Exception {
              String scheduleId = request.getParameter("schedule.id");
              String campaignId = request.getParameter("campaignId");
      
              // create a blank presentation
              SchedulePresentation presentation = new SchedulePresentation();
      
              // if there is a non empty schedule id 
              if (scheduleId != null && !scheduleId.isEmpty()) {
                  Long id = null;
      
                  try {
                      // try to parse the schedule id
                      id = Long.valueOf(scheduleId);
      
                      // get schedule and add it to presentation
                      Schedule schedule = scheduleMgr.getSchedule(id);
                      presentation.setSchedule(schedule);
      
                  } catch  (Exception e) {
                      // number format or schedule not found
                      request.setAttribute("exceptionHappened", true);
                  } 
              } else if (campaignId != null && !campaignId.isEmpty()) {
                  // if there is a non empty campaign id
      
                  try {
                      Long id = Long.valueOf(campaignId);    
                      Campaign campaign = campaignMgr.getCampaign(id);
                      
                      // if campaign schedule is not null, use the predefined schedule
                      if (campaign.getSchedule() != null) {
                          presentation.setSchedule(campaign.getSchedule());
                      } // otherwise, this is a new schedule
      
                  } catch (Exception e) {
                      // number format or campaign not found
                      request.setAttribute("exceptionHappened", true);
                  }
              }
              
              return presentation;
          }
          
          /**
           * Submit method; saves schedule in the database and adds it to the
           * corresponding camapign if necessary
           */
          public ModelAndView onSubmit(HttpServletRequest request,
                  HttpServletResponse response, Object command,
                  BindException errors) throws Exception {      
              SchedulePresentation presentation = (SchedulePresentation) command;
              Schedule schedule = presentation.getSchedule();
             
              schedule = scheduleMgr.saveSchedule(schedule);
              
              // get campaign id and campaign, set schedule in campaign and then save schedule
              String campaignId = request.getParameter("campaignId");
              
              if (campaignId != null && campaignId.trim().length() > 0) {
              
                  try {
                      Campaign campaign = campaignMgr.getCampaign(Long.parseLong(campaignId));
                      campaign.setSchedule(schedule);
                      campaignMgr.saveCampaign(campaign, false); // did not remove subscribers when setting schedule
                  } catch (Exception e) {
                      request.setAttribute("exceptionHappened", true);
                  }
              }
              
              presentation.setSchedule(schedule);
              
              // return to previous page
              return new ModelAndView(getRedirectView(request));
          }
      
          /**
           * Called when form is shown on screen; if there was an exception before
           * there will be a redirect to 403
           */
          protected ModelAndView showForm(HttpServletRequest request,
                  HttpServletResponse response,
                  BindException errors) throws Exception {
              // exceptionHappened attribute signals the need to redirect to 403.jsp
              Object o = request.getAttribute("exceptionHappened");
              
              // if such an attribute exists and is set to true
              if (o != null && ((Boolean) o) == true) {
                  
                  // the page the user is trying to access is forbidden
                  response.sendError(HttpServletResponse.SC_FORBIDDEN);
                  log.warn("Invalid access on blackout with id " + request.getParameter("blackout.id") + " made by user '" + request.getRemoteUser() + "'");
                  
                  // remove the attribute
                  request.removeAttribute("exceptionHappened");
                  
                  // throw exception - this will redirect the user to 403.jsp
                  throw new AccessDeniedException("You do not have permission to modify this blackout.");
              }
              
              return super.showForm(request, response, errors);
          }
          
          /**
           * Registers time/date editors for the web page
           */
          protected void initBinder(HttpServletRequest request,  ServletRequestDataBinder binder) {
              
              // register editors for webpage
              binder.registerCustomEditor(java.sql.Time.class, new CustomTimeEditor());
              binder.registerCustomEditor(java.sql.Date.class, new CustomSqlDateEditor(displayProperties));
              binder.registerCustomEditor(Short.class, "weeklyRecurrence.dowSelector", new CustomDaysOfWeekEditor());
              
              // the schedule id and campaign id cannot be changed
              String [] dissallowedFields = {"schedule.id", "campaignId"};
              binder.setDisallowedFields(dissallowedFields);
              
              super.initBinder(request, binder);
          }
      
      // some helper methods, non-hibernate
      // some setters
      The managers used to persist objects all contain dao objects, which in turn contain entity managers injected by spring.

      BaseFormController extends SimpleFormController and contains some methods used to display info texts in the web pages
      Last edited by laura; Sep 5th, 2008, 09:22 AM.

      Comment


      • #4
        From the log it seems like you have an ExceptionResolver in your DispatcherServlet configuration. The exception is being forwarded to a view called 'xmlError'.

        Comment

        Working...
        X