gpt4 book ai didi

java - 未在请求的 DispatcherServlet 中找到任何 WebApplicationContext,并且未注册 ContextLoaderListener

转载 作者:行者123 更新时间:2023-11-28 22:52:30 27 4
gpt4 key购买 nike

除了我使用的是嵌入式 tomcat 之外,我发现了几个具有相同问题的问题。

我尝试了我找到的所有答案,其中大部分与 ContextLoaderListener 和依赖项相关。

我注意到,当我在“mvn clean package”之后构建应用程序时,有几个关于重复的警告,但老实说,我认为这不是问题所在。我相信嵌入式 Tomcat 配置和上下文之间存在一些问题。

构建期间的日志

C:\STS\wsRestTemplate\TestDeployment>mvn clean package
...
[INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ TestDeployment ---
[INFO] Building jar: C:\STS\wsRestTemplate\TestDeployment\target\embeddedApp.jar

[INFO]
[INFO] --- appassembler-maven-plugin:1.10:assemble (default) @ TestDeployment --
-
[WARNING] The usage of program name (App) is deprecated. Please use program.id i
nstead.
[INFO] Installing artifact C:\Users\e049447\.m2\repository\org\springframework\s
pring-web\4.2.5.RELEASE\spring-web-4.2.5.RELEASE.jar to C:\STS\wsRestTemplate\Te
stDeployment\target\repo\org\springframework\spring-web\4.2.5.RELEASE\spring-web
-4.2.5.RELEASE.jar
[INFO] Installing artifact C:\Users\e049447\.m2\repository\org\springframework\s
pring-aop\4.2.5.RELEASE\spring-aop-4.2.5.RELEASE.jar to C:\STS\wsRestTemplate\Te
stDeployment\target\repo\org\springframework\spring-aop\4.2.5.RELEASE\spring-aop
-4.2.5.RELEASE.jar
...
[INFO] Installing artifact C:\STS\wsRestTemplate\TestDeployment\target\embeddedA
pp.jar to C:\STS\wsRestTemplate\TestDeployment\target\repo\com\mastercard\ictt\b
atchs\TestDeployment\0.0.1-SNAPSHOT\TestDeployment-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- maven-shade-plugin:1.3.3:shade (default) @ TestDeployment ---
[INFO] Including org.springframework:spring-web:jar:4.2.5.RELEASE in the shaded
jar.
[INFO] Including org.springframework:spring-aop:jar:4.2.5.RELEASE in the shaded
...[INFO] Including commons-io:commons-io:jar:2.2 in the shaded jar.
[INFO] Including javax.servlet:javax.servlet-api:jar:3.0.1 in the shaded jar.
[WARNING] We have a duplicate org/apache/jasper/Constants.class in C:\Users\e049
447\.m2\repository\org\apache\tomcat\tomcat-jasper\8.0.32\tomcat-jasper-8.0.32.j
ar
[WARNING] We have a duplicate org/apache/jasper/EmbeddedServletOptions.class in
C:\Users\e049447\.m2\repository\org\apache\tomcat\tomcat-jasper\8.0.32\tomcat-ja
sper-8.0.32.jar
[WARNING] We have a duplicate org/apache/jasper/JasperException.class in C:\User
s\e049447\.m2\repository\org\apache\tomcat\tomcat-jasper\8.0.32\tomcat-jasper-8.
0.32.jar
[WARNING] We have a duplicate org/apache/jasper/JspC.class in C:\Users\e049447\.
m2\repository\org\apache\tomcat\tomcat-jasper\8.0.32\tomcat-jasper-8.0.32.jar
[WARNING] We have a duplicate org/apache/jasper/JspCompilationContext.class in C
:\Users\e049447\.m2\repository\org\apache\tomcat\tomcat-jasper\8.0.32\tomcat-jas
per-8.0.32.jar

Project Structure and Libs Dependencies

The error

App-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">

<context:component-scan base-package="com.mycomp.myapp.batchs.AuthFileUpload" />

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".jsp" />
</bean>

<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
</beans>

网络.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

<display-name>Spring MVC Application</display-name>

<!-- I tried add COntextLoader Listener and explicitly point to App-servlet.xml but I don~t think it is necessary
By the way, same error with it or without -->
<!-- listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/App-servlet.xml</param-value>
</context-param -->

<servlet>
<servlet-name>App</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>App</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

</web-app>

静态主体

import org.apache.catalina.LifecycleException;
import org.apache.catalina.startup.Tomcat;


public class App
{
public static void main( String[] args ) throws LifecycleException
{
Tomcat tomcat = new Tomcat();

tomcat.setPort(8080);
tomcat.setBaseDir("C\\temp");
tomcat.addWebapp(tomcat.getHost(), "/", "C:\\temp\\");//
tomcat.start();
tomcat.getServer().await();
}
}

Index.jsp(如果我注释下面的所有行并添加一个简单的 Hello Word 字符串,它将作为欢迎页面使用)

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>
<body>

<h2>Uploading a file.. </h2>
<form:form method="POST" action="uploadFile" enctype="multipart/form-data">
Upload your file please:
<input type="file" name="file" />
<input type="text" name="name">
<input type="submit" value="Upload" />
<form:errors path="file" cssStyle="color: #ff0000;" />
</form:form>

</body>
</html>

Controller

@Controller
public class FileUploadController {


@RequestMapping(value="/upload",method = RequestMethod.GET)
public String printHello(ModelMap model) {
return "index";
}

最佳答案

问题出在 web.xml 中的 servlet 映射。根据 servlet 规范,映射/覆盖了容器的默认 servlet。这意味着您的 DispatcherServlet 映射到所有没有显式映射的请求。但是,.jsp 扩展名映射到容器的 jsp servlet。因此 index.jsp 不会通过 DispatcherServlet,因此您不能在那里使用 spring form taglib。

改为为您的 index.jsp 实现一个 Controller ,以便它通过调度程序 servlet

@Controller
public class WelcomeController{

@RequestMapping(value = "/")
public String welcome(){
return "index";
}
}

然后将您的 index.jsp 移动到 WEB-INF 文件夹以匹配您的 ViewResolver 设置。然后删除

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

在 web.xml 中

关于java - 未在请求的 DispatcherServlet 中找到任何 WebApplicationContext,并且未注册 ContextLoaderListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35875044/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com