我是Spring的新手。我正在尝试创建一个Spring MVC HelloWorld项目。代码中没有错误但由于某种原因,当我在localhost上运行时,我得到了#34;请求未找到"错误。我已经重新检查了程序但无法解决问题。任何人都告诉我什么错了?
ControllerClass.java(在src文件夹中)
@Controller
@RequestMapping("/hello")
public class ControllerClass {
@RequestMapping(method = RequestMethod.GET)
public String printHello(ModelMap model){
model.addAttribute("message", "Hello Spring MVC Framework");
return "hello";
}
}
hello.jsp(在web-inf中的jsp文件夹中)
<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>Spring MVC example</title>
</head>
<body>
<h1>${message}</h1>
</body>
</html>
HelloWeb-servlet.xml(在web-inf内)
<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"
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">
<context:component-scan base-package="com.springmvcexample.xyz" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
web.xml(在web-inf的lib文件夹中)
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>HelloWeb</servlet-name>
<servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>HelloWeb</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
</web-app>
答案 0 :(得分:0)
看起来您的配置位于错误的位置:默认情况下HelloWeb-servlet.xml
进入WEB-INF
的根目录而不是lib内部。
如果您想将其放在其他位置,则需要在web.xml中定义ContextLoaderListener。
答案 1 :(得分:0)
将web.xml文件中的url模式更改为/ *而不是* .jsp
<servlet-mapping>
<servlet-name>HelloWeb</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>