Sum*_*mit 5 spring-mvc http-status-code-404 requestdispatcher
我经历了很多论坛和博客以获得答案,但无法得到任何有用的提示或建议.所以,如果有人可以在下面的问题上提供帮助,那将是一个很大的帮助.
尝试连接时,我收到以下警告和错误http://localhost:8080/SpringApp/hello:
INFO: Server startup in 6935 ms
Jul 19, 2014 11:15:42 AM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/SpringApp/] in DispatcherServlet with name 'HelloWeb'
Jul 19, 2014 11:16:29 AM com.example.java.HelloController printHelloWorld
INFO: HelloController : printHelloWorld : ENTER
Jul 19, 2014 11:16:29 AM com.example.java.HelloController printHelloWorld
INFO: HelloController : printHelloWorld : EXIT
Jul 19, 2014 11:16:29 AM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/SpringApp/WEB-INF/jsp/hello.jsp] in DispatcherServlet with name 'HelloWeb'
Run Code Online (Sandbox Code Playgroud)
因此我在Tomcat中收到HTTP Status 404错误.
整个数据如下:
web.xml文件是:
<display-name>Spring MVC Application</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>HelloWeb</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/HelloWeb-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>HelloWeb</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)
HelloWeb-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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<context:component-scan base-package="com.example.java"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<context:annotation-config/>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>
</beans>
Run Code Online (Sandbox Code Playgroud)
HelloController.java文件是:
package com.example.java;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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("/hello")
public class HelloController {
protected final Log logger = LogFactory.getLog(getClass());
@RequestMapping(method=RequestMethod.GET)
public String printHelloWorld(ModelMap model){
logger.info("HelloController : printHelloWorld : ENTER");
model.addAttribute("message", "Hello Sumit");
logger.info("HelloController : printHelloWorld : EXIT");
return "hello";
}
}
Run Code Online (Sandbox Code Playgroud)
首先,文件名应该是HelloWeb-servlet.xml(servlet的'小写).如果你还有一些问题,请尝试添加
<mvc:default-servlet-handler/>
Run Code Online (Sandbox Code Playgroud)
在HelloWeb-servlet.xml中
在这里查看我的答案= https://www.youtube.com/watch?v=-AtPAeSWz-o
以下内容对我有用。正如您从上面的 YouTube 视频中看到的,我已经测试过它并且它有效。
目录结构:
确保您的目录结构与此匹配。
网络.xml:
需要注意的一些事情:这<url-pattern>应该是<url-pattern>/<url-pattern>因为当应用程序首次启动时,它会尝试连接到 localhost:8080/YourAppName/ 而不是 localhost:8080/YourAppName/homePage。
你好控制器.java:
| 归档时间: |
|
| 查看次数: |
33991 次 |
| 最近记录: |