**index.jsp**
<html>
<body>
<form action="add">
<input type="text" name="t1"><br>
<input type="text" name="t2"><br>
<input type="submit">
</form>
</body>
</html>
**web.xml**
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
**Display.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>
hi there
</body>
</html>
**dispatcher-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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.juzar.controllers"></context:component-scan>
</beans>
**AddController.java**
package com.juzar.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class AddController {
@RequestMapping("/add")
public String add() {
System.out.println("hi there");
return "Display.jsp";
}
}
问题: 我无法理解为什么会出现图像链接中的此错误。我尝试了很多,但无法找到它的根本原因。在我看来,我的项目结构有问题是吗?
**Console output:**
Jun 09, 2021 4:18:27 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version name: Apache Tomcat/9.0.43
Jun 09, 2021 4:18:27 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built: Jan 28 2021 20:25:45 UTC
Jun 09, 2021 4:18:27 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version number: 9.0.43.0
Jun 09, 2021 4:18:27 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name: Windows 10
Jun 09, 2021 4:18:27 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version: 10.0
Jun 09, 2021 4:18:27 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture: amd64
Jun 09, 2021 4:18:27 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home: C:\Users\Juzar\.p2\pool\plugins\org.eclipse.justj.openjdk.hotspot.jre.full.win32.x86_64_14.0.2.v20200815-0932\jre
Jun 09, 2021 4:18:27 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version: 14.0.2+12-46
Jun 09, 2021 4:18:27 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor: Oracle Corporation
Jun 09, 2021 4:18:27 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE: D:\Workspace\Spring\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
Jun 09, 2021 4:18:27 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME: D:\apache-tomcat-9.0.43
Jun 09, 2021 4:18:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=D:\Workspace\Spring\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
Jun 09, 2021 4:18:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=D:\apache-tomcat-9.0.43
Jun 09, 2021 4:18:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=D:\Workspace\Spring\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps
Jun 09, 2021 4:18:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=Cp1252
Jun 09, 2021 4:18:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -XX:+ShowCodeDetailsInExceptionMessages
Jun 09, 2021 4:18:28 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: The Apache Tomcat Native library which allows using OpenSSL was not found on the java.library.path: [C:\Users\Juzar\.p2\pool\plugins\org.eclipse.justj.openjdk.hotspot.jre.full.win32.x86_64_14.0.2.v20200815-0932\jre\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Users/Juzar/.p2/pool/plugins/org.eclipse.justj.openjdk.hotspot.jre.full.win32.x86_64_15.0.2.v20210201-0955/jre/bin/server;C:/Users/Juzar/.p2/pool/plugins/org.eclipse.justj.openjdk.hotspot.jre.full.win32.x86_64_15.0.2.v20210201-0955/jre/bin;C:\oraclexe\app\oracle\product\11.2.0\server\bin;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Users\Juzar\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\Scripts;C:\Users\Juzar\AppData\Local\Microsoft\WindowsApps;C:\Program Files\Java\jdk1.8.0_201\bin;C:\Users\Juzar\AppData\Local\Programs\Microsoft VS Code\bin;C:\MinGW\bin;C:\Users\Juzar\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\Scripts;C:\Users\Juzar\AppData\Local\atom\bin;;C:\WINDOWS\System32;;.]
Jun 09, 2021 4:18:28 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8084"]
Jun 09, 2021 4:18:28 PM org.apache.catalina.startup.Catalina load
INFO: Server initialization in [1411] milliseconds
Jun 09, 2021 4:18:28 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service [Catalina]
Jun 09, 2021 4:18:28 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet engine: [Apache Tomcat/9.0.43]
Jun 09, 2021 4:18:31 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.
Jun 09, 2021 4:18:31 PM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
Jun 09, 2021 4:18:31 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring DispatcherServlet 'dispatcher'
Jun 09, 2021 4:18:31 PM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: Initializing Servlet 'dispatcher'
Jun 09, 2021 4:18:32 PM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: Completed initialization in 1240 ms
Jun 09, 2021 4:18:32 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8084"]
Jun 09, 2021 4:18:32 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in [3806] milliseconds
Jun 09, 2021 4:18:34 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping for GET /DemoMVC/add
Jun 09, 2021 4:18:59 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping for GET /DemoMVC/add
index.jsp page
error
project structure
如果有人能找到我的根本原因,我会很高兴。我现在很沮丧,如果没有网络帮助,我将无法继续前进。请帮忙!!!
答案 0 :(得分:0)
您需要在容器中启用 Annotation-Driven 注入。在您的情况下,在 dispatcher-servlet.xml
处声明(我假设您不使用 JavaConfig)。
<context:annotation-config/>
顺便说一下,如何考虑使用InternalResourceViewResolver
并将jsp文件放在WEB-INF
文件夹下。
因为将jsp文件(直接)放在webapp
文件夹下会导致用户可以直接访问jsp。
例如,用户可以通过在浏览器中输入 hi there
url 来访问 t1
,而无需传递 t2
和 http://localhost:8080/DemoMVC/Display.jsp
。我们不想那样,对吧?
InternalResourceViewResolver
配置示例:
<!-- Spring MVC view resolver -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property
name="prefix"
value="/WEB-INF/view/" />
<property
name="suffix"
value=".jsp" />
</bean>
以上配置将作为 /WEB-INF/view/Display.jsp
使用以下代码 -
@RequestMapping("/add")
public String add() {
System.out.println("hi there");
return "Display";
}