我是Spring框架中的新手 处理HelloSpring项目,我仍然收到HTTP状态404错误。 我现在非常绝望。愿有人告诉我,问题是什么,拜托?
的web.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";
}
}
redirect.jsp中
<%@page contentType="text/html" pageEncoding="UTF-8"%><% response.sendRedirect("welcome"); %>
Tomcat Log
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错误答案 0 :(得分:1)
让我们重新开始吧:
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 。
<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>
就是这样,现在您可以使用以下网址之一测试您的应用:
让我知道,如果出了问题,祝你好运。