gpt4 book ai didi

java - spring web项目失败

转载 作者:行者123 更新时间:2023-11-28 22:41:25 26 4
gpt4 key购买 nike

我有一个由我开发的 Spring HelloWorld 项目,我在我的项目中使用 Maven 进行库管理。我的目的是看看 Spring 如何处理 url 请求。

我认为我正确地开发了它,但是没有实现结果。

我已经在此处输入了我的Controller 类、web.xml 文件和mvc-dispatcher-servlet.xml 文件。

Controller 类

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


@Controller
@RequestMapping("/welcome")
public class AccessController {

//@RequestMapping(value = {"/welcome"}, method = RequestMethod.GET)
@RequestMapping(method=RequestMethod.GET)
public String printWelcome(ModelMap model){
System.out.println("There in an access atempt");

model.addAttribute("message","Hello I am spring web MVC !!!");

return "hello";

}
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>SpringLMS</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>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

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

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


</web-app>

mvc-dispatcher-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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:component-scan base-package="com.vigamage.controller" />

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

</beans>

根据我的编码方式,当我输入 URL http://localhost:8080/SpringLMS/welcome 时,应该会显示 jsp 页面。

但是我得到的输出是 HTTP Status 404, the requested resource is not available

我认为它不会进入 Controller 类。(因为它不会在控制台上打印我放在 Controller 类中的句子。)

我无法弄清楚我在这里犯的错误。如果你能找出我做错的地方,请指出。

这是我的文件夹结构。

enter image description here

编辑

服务器输出

Oct 03, 2015 1:52:56 PM org.apache.tomcat.util.digester.SetPropertiesRule begin WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:SpringLMS' did not find a matching property. Oct 03, 2015 1:52:56 PM org.apache.catalina.startup.VersionLoggerListener log INFO: Server version: Apache Tomcat/7.0.64 Oct 03, 2015 1:52:56 PM org.apache.catalina.startup.VersionLoggerListener log INFO: Server built: Aug 19 2015 17:18:06 UTC Oct 03, 2015 1:52:56 PM org.apache.catalina.startup.VersionLoggerListener log INFO: Server number: 7.0.64.0 Oct 03, 2015 1:52:56 PM org.apache.catalina.startup.VersionLoggerListener log INFO: OS Name:
Windows 8.1 Oct 03, 2015 1:52:56 PM org.apache.catalina.startup.VersionLoggerListener log INFO: OS Version: 6.3 Oct 03, 2015 1:52:56 PM org.apache.catalina.startup.VersionLoggerListener log INFO: Architecture: amd64 Oct 03, 2015 1:52:56 PM org.apache.catalina.startup.VersionLoggerListener log INFO: Java Home: C:\Program Files\Java\jre7 Oct 03, 2015 1:52:56 PM org.apache.catalina.startup.VersionLoggerListener log INFO: JVM Version: 1.7.0_79-b15 Oct 03, 2015 1:52:56 PM org.apache.catalina.startup.VersionLoggerListener log INFO: JVM Vendor: Oracle Corporation Oct 03, 2015 1:52:56 PM org.apache.catalina.startup.VersionLoggerListener log INFO: CATALINA_BASE: C:\Users\Viraj Gamage\Documents\EclipseMars.metadata.plugins\org.eclipse.wst.server.core\tmp1 Oct 03, 2015 1:52:56 PM org.apache.catalina.startup.VersionLoggerListener log INFO: CATALINA_HOME: C:\Users\Viraj Gamage\Documents\EclipseMars\Tomcat7\apache-tomcat-7.0.64 Oct 03, 2015 1:52:56 PM org.apache.catalina.startup.VersionLoggerListener log INFO: Command line argument: -Dcatalina.base=C:\Users\Viraj Gamage\Documents\EclipseMars.metadata.plugins\org.eclipse.wst.server.core\tmp1 Oct 03, 2015 1:52:56 PM org.apache.catalina.startup.VersionLoggerListener log INFO: Command line argument: -Dcatalina.home=C:\Users\Viraj Gamage\Documents\EclipseMars\Tomcat7\apache-tomcat-7.0.64 Oct 03, 2015 1:52:56 PM org.apache.catalina.startup.VersionLoggerListener log INFO: Command line argument: -Dwtp.deploy=C:\Users\Viraj Gamage\Documents\EclipseMars.metadata.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps Oct 03, 2015 1:52:56 PM org.apache.catalina.startup.VersionLoggerListener log INFO: Command line argument: -Djava.endorsed.dirs=C:\Users\Viraj Gamage\Documents\EclipseMars\Tomcat7\apache-tomcat-7.0.64\endorsed Oct 03, 2015 1:52:56 PM org.apache.catalina.startup.VersionLoggerListener log INFO: Command line argument: -Dfile.encoding=Cp1252 Oct 03, 2015 1:52:56 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre7\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Java\jdk1.7.0_79\bin;C:\Program Files\MATLAB\R2013a\runtime\win64;C:\Program Files\MATLAB\R2013a\bin;C:\Apache\apache-maven-3.3.3\bin;C:\Program Files\Java\jdk1.7.0_79\bin;C:\Program Files (x86)\sox-14-4-2;;. Oct 03, 2015 1:52:56 PM org.apache.coyote.AbstractProtocol init INFO: Initializing ProtocolHandler ["http-bio-8080"] Oct 03, 2015 1:52:56 PM org.apache.coyote.AbstractProtocol init INFO: Initializing ProtocolHandler ["ajp-bio-8009"] Oct 03, 2015 1:52:56 PM org.apache.catalina.startup.Catalina load INFO: Initialization processed in 775 ms Oct 03, 2015 1:52:56 PM org.apache.catalina.core.StandardService startInternal INFO: Starting service Catalina Oct 03, 2015 1:52:56 PM org.apache.catalina.core.StandardEngine startInternal INFO: Starting Servlet Engine: Apache Tomcat/7.0.64 Oct 03, 2015 1:52:57 PM org.apache.catalina.util.SessionIdGeneratorBase createSecureRandom INFO: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [102] milliseconds. Oct 03, 2015 1:52:58 PM org.apache.catalina.startup.TldConfig execute 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. Oct 03, 2015 1:52:58 PM org.apache.catalina.core.ApplicationContext log INFO: No Spring WebApplicationInitializer types detected on classpath Oct 03, 2015 1:52:58 PM org.apache.coyote.AbstractProtocol start INFO: Starting ProtocolHandler ["http-bio-8080"] Oct 03, 2015 1:52:58 PM org.apache.coyote.AbstractProtocol start INFO: Starting ProtocolHandler ["ajp-bio-8009"] Oct 03, 2015 1:52:58 PM org.apache.catalina.startup.Catalina start INFO: Server startup in 1829 ms

最佳答案

1) 您忘记声明您的 Spring 上下文加载器,请在您的 web.xml 中添加:

    <listener>
<display-name>Spring Context Loader</display-name>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

2) 为了在您的 DispatcherServlet 上下文中添加 mvc:annotation-driven 元素,它声明了对注释驱动的 MVC Controller 的明确支持(即 @RequestMapping@Controller, ...)

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

<mvc:annotation-driven />

[...]

</beans>

关于java - spring web项目失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32920484/

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