2015-03-25 11 views
7

PROBLEMA<error-page> impostazione non funziona in Spring MVC

voglio mostrare agli utenti una pagina di errore personalizzata. Simeply put, <error-page> su web.xml non sembra funzionare. Posso indovinare qual è il problema, ma ho bisogno di imparare perché esattamente non funziona.


mio setup

ho creato <error-page> su web.xml nella mia applicazione MVC primavera. Di seguito sono le impostazioni.

<servlet-mapping> 
    <servlet-name>appServlet</servlet-name> 
    <url-pattern>/</url-pattern> 
    <url-pattern>*.*</url-pattern> 
    <url-pattern>*.do</url-pattern>  
</servlet-mapping> 

<error-page> 
    <error-code>404</error-code> 
    <location>/error</location> 
</error-page> 

E la mia pagina di errore si trova proprio nel ...

WEB-INF 
└ views 
    └ Errorpages 
     └ ErrorPage.jsp 

Per il vostro infomation, ho provato queste cose qui di seguito pure, ma nessuna di queste cose ha funzionato.

<error-page> 
    <error-code>404</error-code> 
    <location>/WEB-INF/views/Errorpages/ErrorPage.jsp</location> 
</error-page> 

<error-page> 
    <error-code>404</error-code> 
    <location>/views/Errorpages/ErrorPage.jsp</location> 
</error-page> 

<error-page> 
    <error-code>404</error-code> 
    <location>/Errorpages/ErrorPage.jsp</location> 
</error-page> 

<error-page> 
    <error-code>404</error-code> 
    <location>/ErrorPage.jsp</location> 
</error-page> 

<!-- Or tried those above without the .jsp that comes after ErrorPage --> 
<!-- And also, tried those after moving the resources out of WEB-INF --> 

secondo tentativo

ho guardato il log del server e ho notato che la primavera prova a cercare le risorse da redirecion, così ho cambiato il mio piano e ho cercato di raggiungere questo obiettivo attraverso vista tipica ritorno Spring MVC.

ho fatto questa azione

@RequestMapping("/error") 
public String throwsErrorPage(Locale locale, Model model) { 
    logger.info("Errorpage action..." + locale.getLanguage());    
    return "/Errorpages/ErrorPage"; 
} 

Quello che mi aspettavo era in parte ragione, perché richiama con successo l'azione. Il mio log del server lo rende chiaro.

WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/notfound] in DispatcherServlet with name 'appServlet' 
INFO : com.company.web.controller.HomeController - Errorpage action...ko 

Come potete vedere, ho generato 404 errore digitando notfound su URL, e reindirizza a /errore come ho specificato il <location> in web.xml. Ma non restituisce la vista.

per assicurarsi che l'azione restituisce una visione, ho provato a cambiare il tipo di azione come ...

@RequestMapping("/error") 
public ModelAndView throwsErrorPage(Locale locale, ModelAndView mv) { 
    logger.info("Errorpage action..." + locale.getLanguage());    
    return mv.setViewName("/Errorpages/ErrorPage"); 
} 

Ma non funziona così ...


Strano cosa

è che se chiamo l'azione tramite localhost:8080/error sicuramente chiama l'azione e restituisce la pagina di errore atteso. Beh, ci dovrebbero essere alcune differenze tra reindirizzamento e richiesta tipica digitando qualcosa su url o ajax.


Quello che voglio sapere

  1. Come posso fare <error-page> lavoro di configurazione?
  2. Quali sono le differenze tra il reindirizzamento automatico di Spring e la richiesta tipica di un'azione utente in Spring MVC?

risposta

0
<error-page> 
<exception-type>java.lang.Exception</exception-type> 
<location>/error.jsp</location> 
</error-page> 

la pagina di errore può non mettere in WEB-INF, si muovono fuori e provare un sacco

+0

Spostare la pagina di errore non era la risposta, mi dispiace. E il tipo di eccezione non è rilevante per il mio problema .. – hina10531

0
<error-page> 
    <error-code>500</error-code> 
    <location>/error.jsp</location> 
</error-page> 
<error-page> 
    <error-code>400</error-code> 
    <location>/index.jsp</location> 
</error-page> 
<error-page> 
    <error-code>403</error-code> 
    <location>/403.jsp</location> 
</error-page> 
<error-page> 
    <error-code>404</error-code> 
    <location>/404.jsp</location> 
</error-page 

dovete mettere il vostro errorpages.jsp fuori del WEB_INF folde r e proprio inserisci il seguente codice nel tuo web.xml.

+0

Ho provato anche questo, ma non ha funzionato ... sfortunatamente: [ – hina10531

0

È possibile controllare la configurazione servlet-context.xml. Per me sta lavorando con la seguente configurazione.

<servlet-mapping> 
    <servlet-name>appServlet</servlet-name> 
    <url-pattern>/</url-pattern> 
    <url-pattern>*.*</url-pattern> 
    <url-pattern>*.do</url-pattern> 
</servlet-mapping> 
<error-page> 
    <error-code>404</error-code> 
    <location>/error404</location> 
</error-page> 

e il controller primavera metodo

@RequestMapping(value = "/error404", method = RequestMethod.GET) 
public String error() { 
    logger.info("in error method"); 
    return "/errorPages/error404"; 
} 

e se si desidera specificare la posizione del jsp in web.xml è possibile specificare lì o altro si può aggiungere url. Se aggiungi url DispatcherServlet si occuperà di dare una risposta al browser, se si specifica il jsp sarà come una normale risposta servlet.

verificare questo in servlet-context.xml

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --> 
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <beans:property name="prefix" value="/WEB-INF/views/" /> 
    <beans:property name="suffix" value=".jsp" /> 
</beans:bean> 
1

Errore pagina dimensione deve> 1024 byte.
Aggiungi qualcosa di inutile alla pagina di errore per un test.
Aggiungilo per assicurarti che la pagina di errore abbia questo attributo: [isErrorPage="true"]