我正在尝试从this tutorial学习如何编写REST API。它应该用${msg}
替换index.jsp
文件中的http://localhost:8080/MahlzeitServer/helloWorld/hello
。
问题是当我尝试在HTTP Status 404 - /MahlzeitServer/WEB-INF/helloWorld.jsp
type: Status report
message: /MahlzeitServer/WEB-INF/helloWorld.jsp
description: The requested resource is not available.
调用servlet时,我从tomcat得到这个响应:
localhost:8080/MahlzeitServer
我注意到为什么会这样。如果我转到${msg}
,则会显示该网站,但不会替换<html>
<body>
<h2>Hello World2!</h2>
<h3>Your Message : ${msg}</h3>
</body>
</html>
。这是 index.jsp 文件:
package com.mahlzeit.server;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/helloWorld")
public class HelloWorldController {
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String hello(ModelMap model) {
model.addAttribute("msg", "JCG Hello World!");
return "helloWorld";
}
@RequestMapping(value = "/displayMessage/{msg}", method = RequestMethod.GET)
public String displayMessage(@PathVariable String msg, ModelMap model) {
model.addAttribute("msg", msg);
return "helloWorld";
}
}
HelloWorldController.java
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=" 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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- CHANGED <context:component-scan base-package="com.javacodegeeks.snippets.enterprise" />-->
<context:component-scan base-package="com.mahlzeit.server" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
MVC-调度-servlet.xml中
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
的web.xml
{{1}}
答案 0 :(得分:0)
看起来像是个案问题。尝试
@Controller
@RequestMapping("/helloworld")
public class HelloWorldController {
...
而不是
@Controller
@RequestMapping("/helloWorld")
public class HelloWorldController {
或尝试点击
http://localhost:8080/MahlzeitServer/helloWorld/hello
而不是
http://localhost:8080/MahlzeitServer/helloworld/hello