我的调度程序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控制器下有一个home控制器,如下所示:
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错误,原因是 “原始服务器找不到目标资源的当前表示,或不愿意透露该资源的存在。” 请指导我要去哪里
答案 0 :(得分:0)
原因是您在1337
方法中添加了@ResponseBody
,因此它不会返回页面,而只会返回一个纯字符串,但是在您的情况下,我们没有地方显示字符串
因此,删除goHome()
并返回到类似于以下内容的视图页面:
@ResponseBody
答案 1 :(得分:0)
请删除@ResponseBody,因为调度程序Servlet应该了解返回值不是HTTP响应,而是视图名称。并且请确保返回值应与您的视图页面名称相同。
答案 2 :(得分:0)
实际上,我试图将该字符串仅作为ResponseBody发布。 这是我的工作,我从一个新项目开始,进行了以下更改
尽管我仍然知道其中有什么对我有用:) 感谢您的回复。 这是在github上 https://github.com/gauravmatta/springmvc/tree/master/my-project