批处理管理控制台 - 使用InternalResourceViewResolver而不是FreeMarkerViewResolver的DispatcherServlet

时间:2014-05-27 02:56:50

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

将弹出批处理管理控制台集成到我的应用程序中时遇到问题。

好消息是,当我转到http://mymachine.com:8080/ReportingManager/batch/configuration时,我可以进入管理控制台。

但是当我去http://mymachine.com:8080/ReportingManager/batch/job-configuration时,我得到了404,并且错误地说无法找到/ batch / jobs。

我刚刚比较了我的应用程序日志和我知道正在运行的spring-batch-admin-sample应用程序之间org.springframework.web日志输出的差异。

查看日志后,可以找到弹出批处理管理控制台的URL映射,但DispatcherServlet正在尝试使用InternalResourceViewResolver而不是FreeMarkerViewResolver

下面的我的应用程序日志显示错误的ViewResolver用于/ ReportingManager / batch / jobs ...

    2014-05-27 20:16:22,770 DEBUG [DispatcherServlet] - DispatcherServlet with name 'Batch Servlet' processing GET request for [/ReportingManager/batch/jobs]
    2014-05-27 20:16:22,770 DEBUG [RequestMappingHandlerMapping] - Looking up handler method for path /jobs
    2014-05-27 20:16:22,770 DEBUG [RequestMappingHandlerMapping] - Returning handler method [public void org.springframework.batch.admin.web.JobController.jobs(org.springframework.ui.ModelMap,int,int)]
    2014-05-27 20:16:22,770 DEBUG [DispatcherServlet] - Last-Modified value for [/ReportingManager/batch/jobs] is: -1
    2014-05-27 20:16:23,598 DEBUG [DispatcherServlet] - Rendering view [org.springframework.web.servlet.view.JstlView: name 'jobs'; URL [/WEB-INF/pages/jobs.jsp]] in DispatcherServlet with name 'Batch Servlet'
    2014-05-27 20:16:23,598 DEBUG [JstlView] - Added model object 'endJob' of type [java.lang.Integer] to request in view with name 'jobs'
    2014-05-27 20:16:23,598 DEBUG [JstlView] - Added model object 'jobs' of type [java.util.ArrayList] to request in view with name 'jobs'
    2014-05-27 20:16:23,598 DEBUG [JstlView] - Added model object 'startJob' of type [java.lang.Integer] to request in view with name 'jobs'
    2014-05-27 20:16:23,598 DEBUG [JstlView] - Added model object 'totalJobs' of type [java.lang.Integer] to request in view with name 'jobs'
    2014-05-27 20:16:23,598 DEBUG [JstlView] - Added model object 'jobName' of type [java.lang.String] to request in view with name 'jobs'
    2014-05-27 20:16:23,598 DEBUG [JstlView] - Forwarding to resource [/WEB-INF/pages/jobs.jsp] in InternalResourceView 'jobs'
    2014-05-27 20:16:23,598 DEBUG [DispatcherServlet] - Successfully completed request

然而,当我查看spring-batch-admin-sample的长篇文章时,我可以看到/ spring_admin_console / jobs正在使用AjaxFreeMarkerView ......

[5/27/14 19:57:13:962 EST] 00000023 SystemOut     O 19:57:13,950 DEBUG WebContainer : 0 servlet.DispatcherServlet:693 - DispatcherServlet with name 'Batch Servlet' processing GET request for [/s
pring_admin_console/jobs]
[5/27/14 19:57:13:978 EST] 00000023 SystemOut     O 19:57:13,965 DEBUG WebContainer : 0 annotation.DefaultAnnotationHandlerMapping:221 - Mapping [/jobs] to HandlerExecutionChain with handler [or
g.springframework.batch.admin.web.JobController@52e052e0] and 3 interceptors
[5/27/14 19:57:14:000 EST] 00000023 SystemOut     O 19:57:13,988 DEBUG WebContainer : 0 servlet.DispatcherServlet:769 - Last-Modified value for [/spring_admin_console/jobs] is: -1
[5/27/14 19:57:14:045 EST] 00000023 SystemOut     O 19:57:14,031 DEBUG WebContainer : 0 support.HandlerMethodInvoker:155 - Invoking model attribute method: public java.lang.String org.spring
framework.batch.admin.web.JobController.getJobName(javax.servlet.http.HttpServletRequest)
[5/27/14 19:57:14:091 EST] 00000023 SystemOut     O 19:57:14,078 DEBUG WebContainer : 0 support.HandlerMethodInvoker:173 - Invoking request handler method: public void org.springframework.batch.admin.web.JobController.jobs(org.springframework.ui.ModelMap,int,int)
[5/27/14 19:57:14:125 EST] 00000023 SystemOut     O 19:57:14,113 DEBUG WebContainer : 0 servlet.DispatcherServlet:1045 - Rendering view org.springframework.batch.admin.web.freemarker.AjaxFreeMarkerView: name 'jobs'; URL [/layouts/html/standard.ftl]] in DispatcherServlet with name 'Batch Servlet'
[5/27/14 19:57:14:144 EST] 00000023 SystemOut     O 19:57:14,130 DEBUG WebContainer : 0 freemarker.AjaxFreeMarkerView:328 - Added model object 'springMacroRequestContext' of type [org.springframework.web.servlet.support.RequestContext] to request in view with name 'jobs'
[5/27/14 19:57:14:158 EST] 00000023 SystemOut     O 19:57:14,144 DEBUG WebContainer : 0 freemarker.AjaxFreeMarkerView:328 - Added model object 'startJob' of type [java.lang.Integer] to request i
n view with name 'jobs'
[5/27/14 19:57:14:172 EST] 00000023 SystemOut     O 19:57:14,159 DEBUG WebContainer : 0 freemarker.AjaxFreeMarkerView:328 - Added model object 'totalJobs' of type [java.lang.Integer] to request
in view with name 'jobs'
[5/27/14 19:57:14:186 EST] 00000023 SystemOut     O 19:57:14,172 DEBUG WebContainer : 0 freemarker.AjaxFreeMarkerView:328 - Added model object 'servletPath' of type [java.lang.String] to request
 in view with name 'jobs'
[5/27/14 19:57:14:200 EST] 00000023 SystemOut     O 19:57:14,186 DEBUG WebContainer : 0 freemarker.AjaxFreeMarkerView:328 - Added model object 'jobName' of type [java.lang.String] to request in
view with name 'jobs'
[5/27/14 19:57:14:214 EST] 00000023 SystemOut     O 19:57:14,200 DEBUG WebContainer : 0 freemarker.AjaxFreeMarkerView:328 - Added model object 'jobs' of type [java.util.ArrayList] to request in
view with name 'jobs'
[5/27/14 19:57:14:228 EST] 00000023 SystemOut     O 19:57:14,214 DEBUG WebContainer : 0 freemarker.AjaxFreeMarkerView:328 - Added model object 'titleText' of type [java.lang.String] to request i
n view with name 'jobs'
[5/27/14 19:57:14:242 EST] 00000023 SystemOut     O 19:57:14,228 DEBUG WebContainer : 0 freemarker.AjaxFreeMarkerView:328 - Added model object 'titleCode' of type [java.lang.String] to request i
n view with name 'jobs'
[5/27/14 19:57:14:255 EST] 00000023 SystemOut     O 19:57:14,242 DEBUG WebContainer : 0 freemarker.AjaxFreeMarkerView:328 - Added model object 'endJob' of type [java.lang.Integer] to request in
view with name 'jobs'
[5/27/14 19:57:14:269 EST] 00000023 SystemOut     O 19:57:14,256 DEBUG WebContainer : 0 freemarker.AjaxFreeMarkerView:328 - Added model object 'body' of type [java.lang.String] to request in vie
w with name 'jobs'
[5/27/14 19:57:14:287 EST] 00000023 SystemOut     O 19:57:14,274 DEBUG WebContainer : 0 freemarker.AjaxFreeMarkerView:279 - Rendering FreeMarker template [/layouts/html/standard.ftl] in FreeMark
erView 'jobs'
[5/27/14 19:57:14:909 EST] 00000023 SystemOut     O 19:57:14,895 DEBUG WebContainer : 0 servlet.DispatcherServlet:674 - Successfully completed request

我花了一段时间试图找出正在发生的事情并且不确定。我怀疑我的web.xml中可能出错了。也许与servlet的顺序有关。

我将从我的web.xml发布一段摘录,希望有人会看到我做错了。

先谢谢你为我看一下。

我把我的web.xml放在这里。

http://pastebin.com/eBkHMe8g

1 个答案:

答案 0 :(得分:2)

在您的网络项目中,请确保您拥有此文件夹结构:WEB-INF\classes\META-INF\spring\batch\override。并在此文件夹下创建一个新的.xml文件。将其命名为您想要的任何名称。文件的内容应该是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="resourceService" class="org.springframework.batch.admin.web.resources.DefaultResourceService">
        <property name="servletPath" value="/batch" />
    </bean>
</beans>

servletPath的值需要与servlet-mapping中的web.xml相匹配。我使用了/batch,因为这就是你的内容。

有关此here的更多信息。此外,它有助于查看Spring Batch Admin source code。在resources-context.xml中,您可以看到某个SPeL表达式用于各种servletPath资源:<prop key="servletPath">#{resourceService.servletPath}</prop>。您需要搜索名为resourceService的bean,查看its source code,然后您可以在那里为自定义.xml文件中的DefaultResourceService确定要更改的属性。

LATER EDIT :作为一般建议,您的mvc-dispatcher DispatcherServlet会自动启动,默认情况下会找到位于/WEB-INF/mvc-dispatcher-servlet.xml的文件。您将在mvc-dispatcher-servlet.xml下的bean被选中并在两个不同的应用程序上下文中加载两次,因为您将此文件指定为contextConfigLocation值之一。那里有一个上下文层次结构,第一个(由contextConfigLocation定义)是父类。我们的想法是在根上下文中放置非Web(或将由Web使用)的bean,以便能够“使用”这些bean。其他方式没有多大意义,因为DAO或Service类不需要访问Web。