为什么在spring中返回ModelAndView提示下载.json的视图名称?

时间:2017-08-18 08:06:29

标签: json spring spring-mvc spring-batch spring-batch-admin

我编写了一个控制器来导航到名为accessDenied.jsp的页面。 我使用的是Spring 4.3.6版本

@Controller
public class BatchAccessDeniedController {

static String ERRORPAGE = "accessDenied";

public static final Logger LOG = Logger.getLogger(BatchAccessDeniedController.class);

@RequestMapping(value = "/accessDenied" , method = RequestMethod.GET)
public ModelAndView accessDenied(Principal user, ModelAndView modelAndView) {

    //Log the user who tried to access the restricted resource
    if (user != null) {
        LOG.info(user.getName() + " you do not have permission to access this page!");
    } else {
        LOG.info("You do not have permission to access this page!");
    }
    modelAndView.setViewName(ERRORPAGE);
    return modelAndView;
}
}

使用上面的代码一切正常,直到我介绍Spring Batch Admin依赖关系和web.xml中的批处理管理配置,如下所示

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath:/root-context.xml,
        classpath*:/org/springframework/batch/admin/web/resources/webapp-config.xml,
        classpath:/config/spring-ldap-servlet.xml
    </param-value>
</context-param>

<!-- Filter for Spring Security LDAP -->
<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter>
    <filter-name>shallowEtagHeaderFilter</filter-name>
    <filter-class>org.springframework.web.filter.ShallowEtagHeaderFilter</filter-class>
</filter>

<filter>
    <filter-name>hiddenHttpMethodFilter</filter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<filter-mapping>
    <filter-name>shallowEtagHeaderFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<filter-mapping>
    <filter-name>hiddenHttpMethodFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

<servlet>
    <servlet-name>runlauncher</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:/config/application-web-context.xml,
            classpath*:/org/springframework/batch/admin/web/resources/servlet-config.xml
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>runlauncher</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping> 

<!-- Invalidate user session after 15 minutes of inactivity -->
<session-config>
    <session-timeout>15</session-timeout>
</session-config>

<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

批处理管理员依赖关系如下

<dependency>
        <groupId>org.springframework.batch</groupId>
        <artifactId>spring-batch-admin-manager</artifactId>
        <version>${spring.batch.admin}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.batch</groupId>
        <artifactId>spring-batch-admin-resources</artifactId>
        <version>${spring.batch.admin}</version>
    </dependency>

通过添加它,认为控件来到BatchAccessDeniedController类,它将提示下载index.json文件。我不明白为什么要下载index.json文件而不是重定向到accessDenied.jsp页面。我也有spring-ldap的配置,其中index.jsp是用户登录后的默认页面。

我怀疑批量管理界面有问题导致了这个问题。如果有人遇到过类似的问题,请帮我摆脱这个问题。

以下是显示的下载图标的快照链接。 index.json file

1 个答案:

答案 0 :(得分:1)

经过大量研究后发现了这个问题。配置的runlauncher servlet有两个servlet xml文件。

  1. 类路径*:/组织/ springframework的/批次/管理/网络/资源/ servlet的config.xml中
  2. 类路径:/config/application-web-context.xml,
  3. 第一个xml文件来自spring-batch-admin-domain jar,第二个xml文件是应用程序级别配置的文件

    Spring批处理管理UI内部在spring-batch-admin-manager.jar中配置了JSONViewResolver。由于JSONViewResolver优先于application-web-context.xml中配置的InternalResouceViewResolver,因此使用ModelAndVeiw提示下载ModelAndView中返回的视图名称的.json

    我通过为每个xml配置文件创建两个单独的servlet并具有不同的映射来解决此问题

    <servlet>
        <servlet-name>runlauncher</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                classpath:/config/application-web-context.xml
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet>
        <servlet-name>Batch Servlet</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                classpath*:/org/springframework/batch/admin/web/resources/servlet-config.xml
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>runlauncher</servlet-name>
        <url-pattern>/batch/*</url-pattern>
    </servlet-mapping> 
    
    <servlet-mapping>
        <servlet-name>Batch Servlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    

    有两个servlet,其中一个servlet用于spring批处理管理UI,url映射为&#34; /&#34;将使用&#34; /&#34;拦截所有传入的请求;然后是映射。另一个servlet将拦截所有匹配的请求&#34; / batch / *。

    通过这种方式,批处理servlet的请求使用JSONViewResolver和请求&#34; / batch / *&#34;为application-web-context.xml配置使用InternalResourceViewResolver。这解决了我的问题。