Spring MVC警告:没有找到带有URI 404的HTTP请求的映射

时间:2018-02-13 23:39:51

标签: spring spring-mvc http mapping

我是春天新手,我应该在没有Boot的情况下完成项目。也许你会看到我做错了什么。

我应该在控制台"工作"上得到一个注释。但是,当我的网址开启时,我只是收到错误:http://localhost:8081/UserService/working?。

我认为他们的servlet映射问题。 @RequestMapping无法看到。



web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
      http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

 <display-name>Archetype Created Web Application</display-name>
 
 <!-- Spring MVC - START -->
 <!-- Processes application requests -->
 <servlet>
  <servlet-name>alwaysUseFullPath</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>/WEB-INF/servlet-context.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>

 <servlet-mapping>
  <servlet-name>alwaysUseFullPath</servlet-name>
  <url-pattern>/</url-pattern>
 </servlet-mapping>
 
 
 <!-- Spring MVC - END -->
 
 <!-- The definition of the Root Spring Container shared by all Servlets 
  and Filters -->
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/servlet-context.xml</param-value>
 </context-param>
 
 <!-- Creates the Spring Container shared by all Servlets and Filters -->
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 
</web-app>
&#13;
&#13;
&#13;

&#13;
&#13;
servlet-context.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"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		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">
<mvc:annotation-driven />
<bean id="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
    <property name="alwaysUseFullPath" value="true"/>
</bean>  


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



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

&#13;
&#13;
package com.xdddd;

import org.springframework.stereotype.Controller;
NewUserController

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@EnableWebMvc
@Controller
public class NewUserController {
	
	@RequestMapping("/working", method = RequestMethod.GET)
	public void working() {
		System.out.println("Working");
	}

}
&#13;
&#13;
&#13;

&#13;
&#13;
html


<html>
<body>
<form action="working">
<input type="text">Email<br>
<input type="text">Name<br>
<input type="text">Password<br><br>
<button>Submit</button>
</form>
</body>
</html>
&#13;
&#13;
&#13;

最后我得到了:

警告:在DispatcherServlet中找不到带有URI [/ UserService / working]的HTTP请求的映射,其名称为&#39; alwaysUseFullPath&#39;

1 个答案:

答案 0 :(得分:0)

您可以映射更具体的网址格式,例如* .html, .do,/ pages / ,/ app / *等。 例如,在您的代码中:

<url-pattern>*.do</url-pattern> or <url-pattern>*.html</url-pattern>

<servlet>
  <servlet-name>alwaysUseFullPath</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>/WEB-INF/servlet-context.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>

 <servlet-mapping>
  <servlet-name>alwaysUseFullPath</servlet-name>
  <url-pattern>*.do</url-pattern>
 </servlet-mapping>

在Spring控制器中,方法请求映射模式如下所示:

/working.do or /working.html

@EnableWebMvc
@Controller
public class NewUserController {
	
	@RequestMapping("/working.do", method = RequestMethod.GET)
	public void working() {
		System.out.println("Working");
	}

}

这通常是为了隐藏最终用户的实际映射。