英文:
How to add dispatcherServlet and other Spring Bean configuration in Web.xml
问题
以下是你提供的内容的中文翻译:
我正在处理这个示例。我之前一直收到相同的错误消息。根据答案中提到的,我已经修改了代码,我不再收到下面的错误消息:
Caused by: java.io.FileNotFoundException: 无法打开 ServletContext 资源 [/WEB-INF/applicationContext.xml]
然而,我无法重定向到 jsp。请在下面找到代码详情:
项目结构:
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>
Offers-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>
Hello.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>插入标题</title>
</head>
<body>
exSpringJDBC 成功。
</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>
控制台输出:
... (输出部分省略) ...
WARNING: No mapping for GET /exSpringJDBC/
谢谢,
Sanjay Chauhan
解决方案:
问题在于 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">
... (后续部分省略) ...
</web-app>
英文:
I am working on the this example. I was receiving the same error message. I have modified the code as mentioned the answer, I am not receiving the below error message anymore:
> Caused by: java.io.FileNotFoundException: Could not open
> ServletContext resource [/WEB-INF/applicationContext.xml]
However, I am not able to redirect to the jsp. Please find code details below:
Project Structure:
<?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>
Offers-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>
Hello.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 -> I have to configure to Dao but it is not yet done as I am still working on the error.
<?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>
Console Output:
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/**
Thanks,<br>
Sanjay Chauhan
Solution:
Issue was with the web.xml. Updated web.xml as mentioned in the answer and issue got resolved. I have paste final web.xml for reference.
<?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>
答案1
得分: 1
你正在尝试加载名为“home”的JSP页面,但是您没有这个页面。您只有名为“hello”的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>
英文:
You are trying to load a jsp with name "home", but you don't have this page. You have only "hello" jsp
Change this
@RequestMapping("/")
public String home() {
return "Home";
}
On this
@RequestMapping("/")
public String home() {
return "Hello";
}
Also change your path to the configuration file
Change this
<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>
On this
<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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论