【问题标题】:Spring MVC redirect to jsp for specific urlSpring MVC 重定向到特定 url 的 jsp
【发布时间】:2016-02-03 08:30:04
【问题描述】:

我正在为 AngularJS 项目使用 Spring MVC。

我从前缀为 "/rest/*" 的 url 提供 JSON。所有的jsp文件都是直接访问的,路由是用angular-js处理的。

我需要在访问 jsp 文件之前进行自定义验证。对于休息网址(以“/rest/*”为前缀的网址),我已经有了过滤器。

如何配置 dispatcher-servlet 以便在 spring 控制器完成验证后访问所有 jsp 文件。

web.xml

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

调度程序-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>

【问题讨论】:

    标签: spring spring-mvc


    【解决方案1】:

    您始终可以将多个 url 映射到您的调度程序 servlet。

     <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>
    

    用于验证 您可以为不同的 url 模式配置 mvc 拦截器。例如

    <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>
    

    并为您的角度资源配置一个通用控制器:

    @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);
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2015-05-22
      • 2014-06-20
      • 1970-01-01
      • 2015-05-04
      • 1970-01-01
      • 1970-01-01
      • 2013-08-06
      • 2012-03-07
      相关资源
      最近更新 更多