我是 Spring 新手,正在 MVC 中开发 Hello world 应用程序 当我从控制器返回 ModelAndView 对象时,URL 映射被搜索为相应的视图。我已经包含了所有正确的 jars。这是代码。/web -inf/sample-servlet.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"
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/context/spring-context-3.0.xsd%27%3E)
<context:component-scan base-package="com.tcs.laks.sample.controller" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean> </beans>
这是web.xml---
<?xml version="1.0" encoding="UTF-8"?>
<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"> (http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd%27%3E)
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>sample</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>sample</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
HelloWorldController 是..
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.portlet.ModelAndView;
@Controller
@RequestMapping("/welcome")
public class HelloWorldController {
@RequestMapping(method = RequestMethod.GET)
public ModelAndView helloWorld(){
String message = "Hello";
return new ModelAndView("hello", "message", message);
}
}
url 给出为 //localhost:8080/sample/welcome 它给出了 404,因为它试图找到 welcome.jsp 而不是 hello.jsp
HTTP 状态 404 - /sample/WEB-INF/jsp/welcome.jsp
类型状态报告
消息 /sample/WEB-INF/jsp/welcome.jsp
描述 请求的资源 (/sample/WEB-INF/jsp/welcome.jsp) 不可用。