spring mvc 无法映射 url
spring mvc unable to map url
我的dispatcher servlet如下
<?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:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<mvc:annotation-driven/>
<context:component-scan base-package="com.springimplant.mvc.controllers"/>
</beans>
我在命名空间 Home controller 下有一个 home controller 如下
package com.springimplant.mvc.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HomeController {
@RequestMapping("/home")
@ResponseBody
public String goHome()
{
return "Welcome Home";
}
}
Web.xml 文件是
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>course-project</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListner</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/applicationContext.xml</param-value>
</context-param>
<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/spring/*-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>
</web-app>
仍然当我 运行 本地主机中的应用程序时,我收到 404 错误
"The origin server did not find a current representation for the target resource or is not willing to disclose that one exists."
请指导我哪里出错了
原因是你在goHome()
方法中添加了@ResponseBody
,所以它不会return到一个页面而只是return一个纯字符串,但是在你的情况,我们没有地方显示字符串
所以删除 @ResponseBody
并使 return 到类似于下面的视图页面:
@Controller
public class HomeController {
@RequestMapping("/home")
public String goHome()
{
return "welcome";//if the suffix is jsp
//return "welcome.jsp"; // if do not have suffix in your spring configuration file
}
}
请删除@ResponseBody,因为调度程序 servlet 应该明白返回值不是 HTTP 响应,而是视图名称。并且请确保返回值与您的查看页面名称相同。
实际上我只是想 post 该字符串作为 ResponseBody。
这是对我有用的方法我从一个新项目开始,进行了以下更改
- 再次下载STS(Spring工具套件)(我的控制台调试日志没有显示)。
- 使用 Tomcat 8.5 而不是 pivotal TC 服务器 4.0 您可以尝试基于相同的 pivotal TC 3.2-3.5。
- Dynamic Web Module Facet 我使用的是 4.0,我将其降级为 3.1。(我在某处看到 4.0 支持 @RestController 注释而不是 @Controller 注释)
- 我正在使用 slf4j-log4j13 facet 和 log4j 实现,这给出了一个错误,比如 class not found "log.logger"。所以我添加了另一个 facet "log4j-simple"。
虽然我仍然知道其中哪些对我有用:)
感谢您的回复。
在 github 上
https://github.com/gauravmatta/springmvc/tree/master/my-project
我的dispatcher servlet如下
<?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:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<mvc:annotation-driven/>
<context:component-scan base-package="com.springimplant.mvc.controllers"/>
</beans>
我在命名空间 Home controller 下有一个 home controller 如下
package com.springimplant.mvc.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HomeController {
@RequestMapping("/home")
@ResponseBody
public String goHome()
{
return "Welcome Home";
}
}
Web.xml 文件是
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>course-project</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListner</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/applicationContext.xml</param-value>
</context-param>
<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/spring/*-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>
</web-app>
仍然当我 运行 本地主机中的应用程序时,我收到 404 错误 "The origin server did not find a current representation for the target resource or is not willing to disclose that one exists." 请指导我哪里出错了
原因是你在goHome()
方法中添加了@ResponseBody
,所以它不会return到一个页面而只是return一个纯字符串,但是在你的情况,我们没有地方显示字符串
所以删除 @ResponseBody
并使 return 到类似于下面的视图页面:
@Controller
public class HomeController {
@RequestMapping("/home")
public String goHome()
{
return "welcome";//if the suffix is jsp
//return "welcome.jsp"; // if do not have suffix in your spring configuration file
}
}
请删除@ResponseBody,因为调度程序 servlet 应该明白返回值不是 HTTP 响应,而是视图名称。并且请确保返回值与您的查看页面名称相同。
实际上我只是想 post 该字符串作为 ResponseBody。 这是对我有用的方法我从一个新项目开始,进行了以下更改
- 再次下载STS(Spring工具套件)(我的控制台调试日志没有显示)。
- 使用 Tomcat 8.5 而不是 pivotal TC 服务器 4.0 您可以尝试基于相同的 pivotal TC 3.2-3.5。
- Dynamic Web Module Facet 我使用的是 4.0,我将其降级为 3.1。(我在某处看到 4.0 支持 @RestController 注释而不是 @Controller 注释)
- 我正在使用 slf4j-log4j13 facet 和 log4j 实现,这给出了一个错误,比如 class not found "log.logger"。所以我添加了另一个 facet "log4j-simple"。
虽然我仍然知道其中哪些对我有用:) 感谢您的回复。 在 github 上 https://github.com/gauravmatta/springmvc/tree/master/my-project