gpt4 book ai didi

java - Spring MVC : Tomcat 404 The requested resource is not available

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

我是 Spring 框架的新手,并且在 HelloSpring 项目上工作,我仍然收到 HTTP Status 404 错误。我现在很绝望。谁能告诉我,请问这是什么问题?

网络.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>

调度器-servlet.xml

<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

<context:component-scan base-package="controllers" />
<mvc:annotation-driven />

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.htm">indexController</prop>
</props>
</property>
</bean>

<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />

<!--
The index controller.
-->
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />

IndexController.java

@Controller
public class IndexController{
@RequestMapping("welcome")
public String index(ModelMap map, HttpServletRequest r){
System.out.println("Kontrola do konzole");
map.put("msg", "This is index page");
return "index";
}
}

重定向.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%><% response.sendRedirect("welcome"); %>

Tomcat 日志

05-Feb-2017 14:14:00.583 WARNING [http-nio-8084-exec-495] org.springframework.web.servlet.DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI [/MavenHello/welcome] in DispatcherServlet with name 'dispatcher'

当我运行应用程序时,我总是在 URL http://localhost:8084/MavenHello/welcome 上收到 404 错误

最佳答案

让我们重新开始吧:

1# 在您的 web.xml 中添加此代码段(去掉 redirect.jsp,我们不再需要它了)

<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>

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

<load-on-startup>1</load-on-startup>
</servlet>

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

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

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

2# 在 dispatcher-servlet.xml 中放入这个片段(获取所有其他配置的突袭):
请务必将 xx.yy.zz.controllers 更改为您的真实包(您放置 Controller 的位置。)

<context:component-scan base-package="xx.yy.zz.controllers" />
<mvc:annotation-driven />

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

3# 确保你的 IndexController 看起来像这样:

package xx.yy.zz.controllers;

//imports here...

@Controller
public class IndexController {

@RequestMapping(value={"/", "/index", "/welcome"}, method = RequestMethod.GET)
public String index(Map<String, Object> model) {

System.out.println("Kontrola do konzole");

map.put("msg", "This is index page");

//it will be redirected to /WEB-INF/jsp/index.jsp (make sure that have this page)
return "index";
}
}

4# 最后添加一个/WEB-INF/jsp/index.jsp

<html>
<body>
<c:if test="${not empty msg}">
Hello ${msg}
</c:if>
</body>
</html>

就是这样,现在您可以使用以下网址之一测试您的应用:

如果出现问题请告诉我,祝你好运。

关于java - Spring MVC : Tomcat 404 The requested resource is not available,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42052753/

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