Spring MVC重定向到特定URL的jsp [英] Spring MVC redirect to jsp for specific url

查看:92
本文介绍了Spring MVC重定向到特定URL的jsp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Spring MVC用于AngularJS项目.

I am using Spring MVC for AngularJS project.

我正在从前缀为"/rest/*"的url提供JSON.可直接访问所有jsp文件,并使用angular-js处理路由.

I am serving JSON from the url prefixed with "/rest/*" . All the jsp files are accessed directly and routing is handled using angular-js.

在访问jsp文件之前,我需要进行自定义验证.对于其他网址(以"/rest/*"开头的网址),我已经安装了过滤器.

I need to do custom validations before jsp files are accessed. For rest urls (url prefixed with "/rest/*"), i already have filters in place.

我如何配置调度程序servlet,以便在spring控制器完成验证后访问所有的jsp文件.

How can i configure dispatcher-servlet such that all the jsp files are accessed once validation is done by spring controller.

web.xml

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

dispatcher-servlet.xml

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>

推荐答案

您始终可以将多个URL映射到调度程序servlet.

You can always map more than one url to your dispatcher 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拦截器.例如

For validations You can configure mvc interceptors for your different url patterns. for e.g.

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

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

And configure a common controller for your angular resources :

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

}

这篇关于Spring MVC重定向到特定URL的jsp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆