Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么必须使用SpringForm创建ContextLoaderListener?_Java_Spring_Jsp_Spring Mvc - Fatal编程技术网

Java 为什么必须使用SpringForm创建ContextLoaderListener?

Java 为什么必须使用SpringForm创建ContextLoaderListener?,java,spring,jsp,spring-mvc,Java,Spring,Jsp,Spring Mvc,我试图让spring窗体正常工作,我只使用spring mvc,当我尝试添加带有modelAttribute的spring窗体标记时,它失败了,并说找不到ContextLoaderListener 这是我创建表单的JSP代码: <sf:form method="POST" cssClass="form-horizontal" modelAttribute="companySearch"> 我也尝试了@modeldattribute注释,但仍然没有任何效果,我也在表单中添加了acti

我试图让spring窗体正常工作,我只使用spring mvc,当我尝试添加带有
modelAttribute
的spring窗体标记时,它失败了,并说找不到
ContextLoaderListener

这是我创建表单的JSP代码:

<sf:form method="POST" cssClass="form-horizontal" modelAttribute="companySearch">
我也尝试了@modeldattribute注释,但仍然没有任何效果,我也在表单中添加了action属性,但仍然没有成功,我的这个方法的根路径是“/”

为什么我必须在这里创建ContextLoaderListener?基本上,CompanySearch是一个模型,它是一个简单的POJO,如果我删除该表单或对其进行注释,一切都会正常工作。这里似乎有什么问题,我在这里检查了各种问题,但在我的案例中仍然不明白

引导侦听器启动Spring的根WebApplicationContext。 只需委托给ContextLoader即可


它是容器调用的servlet侦听器。它创建应用程序上下文并加载XMLBean定义文件。它意味着一切都从它开始,并且必须拥有它

你可以用

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

org.springframework.web.context.ContextLoaderListener
ContextLoaderListener Vs DispatcherServlet

在SpringMVC中,有两个上下文。一个是由ContextLoaderListener启动的应用程序上下文或全局上下文。它接受contextConfigLocation参数中提到的所有配置文件。
现在,如果您正在使用SpringMVC,则需要DispatcherServlet,这将启动另一个容器,也称为web应用程序容器。此容器将全局容器作为父容器。
如果您只在后端使用Spring,那么全局上下文就足够了。然而,如果前端也使用SpringMVC,那么还需要DispatcherServlet


(复制自,我太懒了,已经有很多东西了)

是的,我看到了,但我的意思是我已经有了一个用于dispatcher servlet的上下文xml文件,它按照我提到的方式加载bean,为什么spring表单需要它?如果没有
ContextLoaderListener
,spring容器永远不会启动。无论如何,它必须被触发启动;对于web应用程序,您可以通过配置当web应用程序启动或停止时由Servlet容器通知的
侦听器来实现这一点。@Developer106:for Spring MVC Dispatcher Servlet是必需的。我已经更新了我的答案。这是否意味着我必须有这两个上下文?因为就在我添加spring表单标记元素之前,只有dispatcher servlet上下文,一切都正常工作。@Developer106:是的。contextloaderlistenrer在您使用SpringMVC之类的表单之前就足够了。对于MVC,您需要DispatcherServlet。
@RequestMapping(method = RequestMethod.POST)
    public String showResultPage(Model model){
        model.addAttribute(new CompanySearch());
        return "result";
    }
<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>