2015-11-03 15 views
6

Sto utilizzando Spring MVC per il progetto AngularJS.Spring MVC reindirizzare a jsp per URL specifici

Sto servendo JSON dall'URL preceduto da "/ rest/*". Tutti i file jsp sono accessibili direttamente e il routing viene gestito utilizzando angular-js.

Devo eseguire convalide personalizzate prima di accedere ai file jsp. Per gli url di resto (url con prefisso "/ rest/*"), ho già dei filtri sul posto.

Come posso configurare il dispatcher-servlet in modo tale che tutti i file jsp siano accessibili una volta che la convalida viene eseguita dal controller a molla.

web.xml

<servlet-mapping> 
    <servlet-name>dispatcher</servlet-name> 
    <url-pattern>/rest/*</url-pattern> 
</servlet-mapping> 

dispatcher-servlet.xml

<?xml version='1.0' encoding='UTF-8' ?> 
<!-- was: <?xml version="1.0" encoding="UTF-8"?> --> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:p="http://www.springframework.org/schema/p" 
     xmlns:aop="http://www.springframework.org/schema/aop" 
     xmlns:tx="http://www.springframework.org/schema/tx" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:mvc="http://www.springframework.org/schema/mvc" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 

    <context:component-scan base-package="com.gauti" /> 
    <mvc:annotation-driven /> 
</beans> 

risposta

6

È sempre possibile mappare più di un URL al vostro servlet dispatcher.

<servlet-mapping> 
    <servlet-name>dispatcher</servlet-name> 
    <url-pattern>/rest/*</url-pattern> 
    <url-pattern>/non-rest/*</url-pattern> 
    </servlet-mapping> 

<servlet> 
     <servlet-name>dispatcher</servlet-name> 
     <servlet-class> 
     org.springframework.web.servlet.DispatcherServlet 
     </servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value> 
       classpath:spring-config/dispatcher-servlet.xml 
      </param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

per le convalide È possibile configurare intercettori MVC per la vostra diversi modelli di URL. per es.

<mvc:interceptors> 
     <mvc:interceptor> 
      <mvc:mapping path="/**"/> 
      <bean id="localeChangeInterceptor" 
      class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"> 
      <property name="paramName" value="j_lang" /> 
      </bean> 
     </mvc:interceptor> 
</mvc:interceptors> 

e configurare un controller comune per le risorse angolari:

@Controller 
@RequestMapping("/ng") 
public class AngularResourcesController { 

    @RequestMapping(value = "/**", method = RequestMethod.GET) 
    public ModelAndView process(HttpServletRequest request) { 
     String pageName = null; 
     //create your custom jsp URL, modify accordingly to your jsp location. 
     pageName =request.getRequestURL().toString(); 
     //forward to internal jsp. 
     return new ModelAndView(pageName); 
    } 

} 
Problemi correlati