java - 如何在 Web.xml 中添加 dispatcherServlet 和其他 Spring Bean 配置
问题描述
我正在研究这个例子。我收到了同样的错误信息。我已经修改了答案中提到的代码,我不再收到以下错误消息:
原因:java.io.FileNotFoundException:无法打开 ServletContext 资源 [/WEB-INF/applicationContext.xml]
但是,我无法重定向到 jsp。请在下面找到代码详细信息:
项目结构:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<display-name>exSpringJDBC</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>offers</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:org/exSpringJDBC/config/exSpringDao.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>offers</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
提供-servlet.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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<context:annotation-config></context:annotation-config>
<context:component-scan
base-package="org.exSpringJDBC.Controller">
</context:component-scan>
<mvc:annotation-driven></mvc:annotation-driven>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsps/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
你好.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
exSpringJDBC successful.
</body>
</html>
ExSpringController.java
package org.exSpringJDBC.Controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class ExSpringController {
@RequestMapping("/")
public String Hello() {
return "Hello";
}
}
exSpringDao.xml -> 我必须配置到 Dao,但它还没有完成,因为我仍在处理错误。
<?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">
</beans>
控制台输出:
Jul 26, 2020 6:05:59 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version name: Apache Tomcat/9.0.34
Jul 26, 2020 6:05:59 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built: Apr 3 2020 12:02:52 UTC
Jul 26, 2020 6:05:59 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version number: 9.0.34.0
Jul 26, 2020 6:05:59 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name: Windows 10
Jul 26, 2020 6:05:59 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version: 10.0
Jul 26, 2020 6:05:59 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture: amd64
Jul 26, 2020 6:05:59 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home: C:\Program Files\Java\jdk-14.0.1
Jul 26, 2020 6:05:59 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version: 14.0.1+7
Jul 26, 2020 6:05:59 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor: Oracle Corporation
Jul 26, 2020 6:05:59 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE: D:\new_Eclipse_Workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
Jul 26, 2020 6:05:59 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME: C:\Program Files\Apache Software Foundation\Tomcat 9.0
Jul 26, 2020 6:05:59 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=D:\new_Eclipse_Workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
Jul 26, 2020 6:05:59 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=C:\Program Files\Apache Software Foundation\Tomcat 9.0
Jul 26, 2020 6:05:59 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=D:\new_Eclipse_Workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps
Jul 26, 2020 6:05:59 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=Cp1252
Jul 26, 2020 6:05:59 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: Loaded APR based Apache Tomcat Native library [1.2.23] using APR version [1.7.0].
Jul 26, 2020 6:05:59 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
Jul 26, 2020 6:05:59 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
Jul 26, 2020 6:05:59 PM org.apache.catalina.core.AprLifecycleListener initializeSSL
INFO: OpenSSL successfully initialized [OpenSSL 1.1.1c 28 May 2019]
Jul 26, 2020 6:06:00 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-9591"]
Jul 26, 2020 6:06:00 PM org.apache.catalina.startup.Catalina load
INFO: Server initialization in [1,323] milliseconds
Jul 26, 2020 6:06:00 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service [Catalina]
Jul 26, 2020 6:06:00 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet engine: [Apache Tomcat/9.0.34]
Jul 26, 2020 6:06:02 PM org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory getObjectInstance
WARNING: Name = myoracle Property maxActive is not used in DBCP2, use maxTotal instead. maxTotal default value is 8. You have set value of "20" for "maxActive" property, which is being ignored.
Jul 26, 2020 6:06:02 PM org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory getObjectInstance
WARNING: Name = myoracle Property maxWait is not used in DBCP2 , use maxWaitMillis instead. maxWaitMillis default value is -1. You have set value of "-1" for "maxWait" property, which is being ignored.
Jul 26, 2020 6:06:02 PM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Jul 26, 2020 6:06:02 PM org.apache.catalina.util.SessionIdGeneratorBase createSecureRandom
WARNING: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [107] milliseconds.
Jul 26, 2020 6:06:06 PM org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory getObjectInstance
WARNING: Name = myoracle Property maxActive is not used in DBCP2, use maxTotal instead. maxTotal default value is 8. You have set value of "20" for "maxActive" property, which is being ignored.
Jul 26, 2020 6:06:06 PM org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory getObjectInstance
WARNING: Name = myoracle Property maxWait is not used in DBCP2 , use maxWaitMillis instead. maxWaitMillis default value is -1. You have set value of "-1" for "maxWait" property, which is being ignored.
Jul 26, 2020 6:06:06 PM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Jul 26, 2020 6:06:07 PM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
Jul 26, 2020 6:06:07 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring DispatcherServlet 'offers'
Jul 26, 2020 6:06:07 PM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: Initializing Servlet 'offers'
Jul 26, 2020 6:06:07 PM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: Completed initialization in 447 ms
Jul 26, 2020 6:06:09 PM org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory getObjectInstance
WARNING: Name = myoracle Property maxActive is not used in DBCP2, use maxTotal instead. maxTotal default value is 8. You have set value of "20" for "maxActive" property, which is being ignored.
Jul 26, 2020 6:06:09 PM org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory getObjectInstance
WARNING: Name = myoracle Property maxWait is not used in DBCP2 , use maxWaitMillis instead. maxWaitMillis default value is -1. You have set value of "-1" for "maxWait" property, which is being ignored.
Jul 26, 2020 6:06:09 PM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Jul 26, 2020 6:06:09 PM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
Jul 26, 2020 6:06:09 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-9591"]
Jul 26, 2020 6:06:09 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in [8,739] milliseconds
Jul 26, 2020 6:06:09 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
**WARNING: No mapping for GET /exSpringJDBC/**
谢谢,
桑杰乔汉
解决方案: 问题出在 web.xml 上。如答案中所述更新了 web.xml,问题得到解决。我粘贴了最终的 web.xml 以供参考。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<display-name>exSpringJDBC</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>offers</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/offers-servlet.xml
classpath:org/exSpringJDBC/config/exSpringDao.xml
classpath:org/exSpringJDBC/config/OfferService-Context.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>offers</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
解决方案
您正在尝试加载名称为“home”的 jsp,但您没有此页面。你只有“你好”的jsp
改变这个
@RequestMapping("/")
public String home() {
return "Home";
}
在这
@RequestMapping("/")
public String home() {
return "Hello";
}
还要更改配置文件的路径
改变这个
<servlet>
<servlet-name>offers</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:org/exSpringJDBC/config/exSpringDao.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
在这
<servlet>
<servlet-name>offers</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/offers-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
推荐阅读
- r - 那里真的没有适当的 R 消息框功能吗?
- javascript - 找不到 TimePicker 24 小时格式 javascript (CDN)
- visual-studio-code - 打开大文件后,VS Code 在重启时崩溃
- extjs - 控制器和视图之间的参数交换Extjs问题
- prolog - 月份与唯一值无关?
- django - get_object_or_404 最新 = True
- java - 如何在桶排序聚合期间在 Java Elasticsearch 客户端中指定 from 子句
- html - Bootstrap 4 - 在锚标签内包装卡片图像会在 IE 11 中创建空白
- php - 如何使用php中的单个函数参数从关联数组中获取特定的数组值
- java - JavaFX MediaPlayer:使用 setStopTime 时播放次数减半